Possible Duplicate:
why "++x || ++y && ++z" calculate "++x" firstly ? however,Operator "&&" is higher than "||"
The following program does not seem to work as expected. ‘&&’ is to have higher precendence than ‘||’, so the actual output is confusing. Can anyone explain the o/p please?
#include <stdio.h>
int main(int argc, char *argv[])
{
int x;
int y;
int z;
x = y = z = 1;
x++ || ++y && z++;
printf("%d %d %d\n", x, y, z);
return 0;
}
The actual output is: 2 1 1
TIA.
Precedence and order of evaluation have no relation whatsoever.
&&having higher precedence than||means simply that the expression is interpreted asThus, the left-hand operand of
||is evaluated first (required because there is a sequence point after it), and since it evaluates nonzero, the right-hand operand(++y && z++)is never evaluated.