int main() {
int i = -3, j = 2, k = 0, m;
m = ++i || ++j && ++k;
printf("%d %d %d %d\n", i, j, k, m);
return 0;
}
i thought that && has more precedence that || as per this logic ++j
should execute, but it never does and the program outputs -2 2 0 1
. What is going on here? What are the intermediate steps?
C does short-circuiting of logical expressions, so evaluation of
++i
is enough to figure out thatm
should be true.||
operator forces left-to-right evaluation, so the expression++i
is fully evaluated first, with a result of-2
.||
operator forces a sequence point, so the side effect is applied andi
is now equal to-2
.++i
is not 0, so the expression++j && ++k
is not evaluated at all.++i || ++j && ++k
is1
(true), which is assigned tom
.Just to echo what several others have said, precedence and order of evaluation are not the same thing.