This was a question in my preparation exam:
int val = 0;
int x = 0;
int y = 1;
if (x < val < y)
printf(" true ");
else
printf(" false ");
Why is this true? I tried changing x and val and it ignored those changes, as long as y was larger than 0 (so 1, 2, 3...) the statement was true. So for example: if (3 < 9 < 2) will be true.
( 2 < 9 < 3 ) is evaluated as ( ( 2 < 9 ) < 3).
In the first step 2 < 9 is evaluated to be true, which is represented as integer value 1 and results in ((1) < 3) for the second step.
That is obviously true.
You probably wanted something like ((x < val) && ( val < y)).
The first is check whether x < value. If true, it return 1 so the next is check whether 1 < y.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With