Sequenced expressions
suggest changeThe following expressions are sequenced:
a && b
a || b
a , b
a ? b : c
for ( a ; b ; c ) { ... }
In each case, a is fully evaluated and all of its side effects are applied before b or c is evaluated. For ?:, only one of b and c is evaluated. In the for statement, the body is sequenced after the controlling expression b and before the expression c. c is not evaluated on the iteration where b ends the loop.
In all cases, the evaluation of expression a is sequenced before the evaluations of b or c (alternately, the evaluations of b and c are sequenced after the evaluation of a).
Thus, expressions like
x++ && x++
x++ ? x++ : y++
(x = f()) && x != 0
for ( x = 0; x < 10; x++ ) { ... }
y = (x++, x++);
have defined behavior, provided an increment does not overflow a signed integer. Signed overflow is undefined behavior even when the two modifications are sequenced.
Found a mistake? Have a question or improvement idea?
Let me know.
Table Of Contents