Search code examples
cexpressionassociativity

a>b>c Evaluation When Each Has Numeric Values


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.


Solution

  • 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