I run the following C program
#include <stdio.h>
int main() {
int x = 5, y = 6, z = 3, i;
i = y > x > z;
printf("%d\n", i);
}
and get the output as 0
.
Again, when I run
#include <stdio.h>
int main() {
int x = 5, y = 6, z = 3, i;
i = y > x && x > z;
printf("%d\n", i);
}
I get output as 1
. Can anyone explain the logic behind this?
i = y > x > z;
In first example, associativity of >
operator left to right, So, First parsed y > x
and gives boolean result.
y > x = 6 > 5 = True
then,
1(True) > 3 = False
So, output 0
.
In Second,
i = y > x && x > z;
>
operator higher precedence then &&
operator. So, first parsed y > x
and if condition True, then check x > z
.