The &&
operator (as well as the ||
operator) is a short-circuit operator. That means that if the left operand evaluates false, the result is false regardless of the right operand, so the right operand is not evaluated.
From section 6.5.13 of the C standard:
4 Unlike the bitwise binary &
operator, the &&
operator guarantees left-to-right evaluation; if the second operand is
evaluated, there is a sequence point between the evaluations
of the first and second operands. If the first operand
compares equal to 0, the second operand is not evaluated.
So in the case of this expression:
x && y && z++;
x && y
is evaluated first. Since x
is 1, y
is evaluated and found to be 0, so the result of the first &&
is 0. Now we have:
0 && z++
Since the left operand is 0, the right operand is not evaluated. This means that z
is not incremented.
This is in contrast to expression in the first piece of code:
x && y || z++
As before, x && y
evaluates to 0, leaving us with:
0 || z++
Since the left operand is 0, the right operand needs to be evaluated. This results in z
getting incremented.