-1

I came upon a competitive C question which goes as follows:

Find the output of:

#include <stdio.h>
int main(void) {
    int a=5, b=10, c=5;
    int x;
    x = a>b>c;
    printf("%d\n", x);
    return 0;
}

The compiler responds 0.

What my explanation is that perhaps this is a side effect of right to left evaluation. My guess is, first b>c is evaluated (assuming its is pushed into the stack first, I am confused here as I do know >'s left to right associativity), which evaluates to true. The value of this true variable, which is a number > 0 (is unknown to us), to which a > that_value is evaluated, yielding the result. However I may be wrong!

Any pointers/insights on how the output is evaluated would be useful. Thanks.

EDIT: I tested in a very old compiler that gave 1, it was a mistake on my part, rectified it.

Jishan
  • 1,654
  • 4
  • 28
  • 62
  • Negative voter, please explain the reason of your negative vote? So I may rectify it! – Jishan Oct 09 '14 at 09:52
  • i dont get the downvote either as this looks strange and interesting. However, "some compiler"? What compiler give what output? – user2504380 Oct 09 '14 at 09:53
  • I doubt you will find a compiler that answers anything other than 0. The language specification has always been clear about the left-to-right associativity of the `>` operator. – Jens Oct 09 '14 at 09:55
  • Is this question primarily about why one compiler gives a different result as another or why you get `0`? If it's about the latter, it's a duplicate to already existing questions. – alk Oct 09 '14 at 10:04
  • @alk No I was confused with the part that doesn't C push expressions in stack and the evaluation internally should be right to left? – Jishan Oct 09 '14 at 10:06

2 Answers2

4

C compiler reads the code from top to bottom, left to right.
here,a>b>c => 5 > 10 > c => 0 > 5 (false is representated by 0) => 0


So, the answer should be 0 for most of the Compiler which follow this order of precedence

For more Detail on the Order of Operation

Asis
  • 683
  • 3
  • 23
  • 1
    +1, but it's not because the compiler reads the code from left to right, but because the associativity of `<` is left to right. – Yu Hao Oct 09 '14 at 10:04
  • @YuHao I am confused with the part that doesn't C push expressions in stack and the evaluation internally should be right to left? – Jishan Oct 09 '14 at 10:07
  • @YuHao that's right. With other operators e.g. `+` the associativity is _right to left_ – Baldrickk Oct 09 '14 at 10:10
  • there are some operator which are operated from right to left like assignment operator x = 5 the 5 is taken from right and kept to the left of the operater. Besides these sort of operator and some rare function all other on c are executed left to right. – Asis Oct 09 '14 at 10:12
  • @Debojyoti if `+` sign is executed right to left what will be the answer of 6+++x; let here x be 5 . – Asis Oct 09 '14 at 10:15
3

For relational operators the associativity is left to right so always you should get 0. I wonder how you got 1 on some compiler.

Gopi
  • 19,784
  • 4
  • 24
  • 36