Possible Duplicate:
why “++x || ++y && ++z” calculate “++x” firstly ? however,Operator “&&” is higher than “||”
If you look at C's precedence table, you'll see that && has higher precedence than ||.
But take a look at the following code:
a=b=c=1;
++a || ++b && ++c;
printf("%d %d %d\n",a,b,c);
It prints out "2 1 1", meaning that the "++a" is evaluated first, and once the program sees a TRUE there it stops right there, because what is on the other side of the || is not important.
But since && has higher precedence than ||, shouldn't "++b && ++c" be evaluated first, and then the result plugged back into "++a || result" ? (in this case the program would print "1 2 2").
Just try to imagine it with parentheses:
++a || ++b && ++c;
equals
(++a) || (++b && ++c);
which is evaluated from left to right.
if && and || would have the same precedence, it would look like
(++a || ++b) && (++c);
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