x+=x*=x
undefined behavior?14) With respect to an indeterminately-sequenced function call, the operation of compound assignment operators, and both prefix and postfix forms of increment and decrement operators are single evaluations.
x+=x*=x
has undefined behaviour because x
is assigned to twice between sequence points.
The text corresponding to 14) in C11, C17 says
A compound assignment of the form E1 op= E2 is equivalent to the simple assignment expression E1 = E1 op (E2), except that the lvalue E1 is evaluated only once, and with respect to an indeterminately-sequenced function call, the operation of a compound assignment is a single evaluation.
What I believe it means is that
int x = 0;
int foo(void) {
x = 5;
return x;
}
int main(void) {
int y = foo() + (x += 2);
}
will have either the behaviour of
int main(void) {
int _tmp = x += 2;
int y = foo() + _tmp;
}
or
int main(void) {
int _tmp = foo();
int y = _tmp + (x += 2);
}
and cannot be split to for example
int main(void) {
int _tmp = x;
int _tmp2 = foo();
x = _tmp + 2;
int y = _tmp2 + x;
}
Note that this guarantee is new in C11, and it does not exist in C89, C99.