int x=12;
int y=7;
int z=19;
int sum;
sum = ++x >= y * 2 || y % 2 && z++ % 2;
I am a little bit confused about the operator precedence?
what is the first condition the compiler will read in the above code?
Is it going to first evaluate y % 2 && z++ % 2
since &&
has precedence over ||
or is the compiler still going to go from left to right and short circuit if on the very left ++x >= y * 2
is true? i.e is the code going to be read in the following way by the compiler?
sum = (++x >= y * 2 || y % 2 )&& z++ % 2;
Your expression is grouped as
(++x >= (y * 2)) || ((y % 2) && (z++ % 2))
and this is assigned to sum
. This is specified by the grammar of C.
Note also that the right hand side of ||
is not evaluated if the left hand side is 1
: which will mean that z
is not incremented in that case.
For avoidance of doubt, ++x
is the new value of x
, and z++
is the old value of z
.
Note also that because ||
is a sequencing point, the expression would be well defined even had you written x++
on the right hand side, rather than z++
.
Calling the result of this sum
is an exercise in obfuscation.