C precedence problem

main()
{
int x,y,z ;
x=y=z=1 ;
z=++x || ++y && ++z ;
printf(“x=%d y = %d z=%d”,x,y,z) ;
}
on running anser is - x=2 y=1 z=1

I 'm puzzled. ++ has highest precedence in this expression & associativity is right to left. So first z will become 2 , then y should be 2 & lastly x set at 2. Then && shud be evaluated. As y is 2 (non-zero) ++y && ++z shud give 1(truth) & hence z should be assigned 1 (truth). Pls. help.

The standard indicates that && and || guarantee left-to-right evaluation. They will evaluate the left hand side, and then will only evaluate the right hand side if that is necessary to determine the result (lazy evaluation).

&& has precedence over ||.

Therefore, the precedence would be as if your statement is
z = ((++x) || ((++y) && (++z)))

X will first become 2. The || determines that 2 is not 0. It will not evaluate the right hand side. Finally, z will become 1 (the result of your expression). Y was already 1 and remains unchanged.

In general, I think you should try to avoid complicated constructs like this. Even if you have tested the result thoroughly, the program will be difficult to read and maintain by (yourself and) others.

Side effects of ++ and – are not guaranteed to take place at the execution of the ++ or – itself. They will take place between the previous and the next sequence point. Fortunately, && and || are sequence points, so that helps with the predictability of the side effects. If you use other operators in the expression, the result may easily become undefined.