As everyone knows, this loops through zero:
while (x-- > 0) { /* also known as x --> 0 */
printf("x = %d\n", x);
}
But x = x--
yields undefined behaviour.
Both examples need some 'return' value of x--
, which is not there I guess. How can it be that x-- > 0
is defined but x = x--
is not?
Because in
x = x--
you're modifying the value ofx
twice without an intervening sequence point. So the order of operations is not defined. Inx-- > 0
the value ofx
is modified once, and it is clearly defined that result of evaluatingx--
will be the value ofx
before the decrement.