A discussion arose around the C statement x = b[i] + i++;
and its definedness.
The argument for said statement to be undefined goes something like this:
§ 6.5 of C99 states:
[…] the order of evaluation of subexpressions and the order in which side effects take place are both unspecified.
Thus it is not guaranteed that i
is incremented after it is used in the subscript operator as index of the array.
However, I interpret said specification differently.
§ 6.5 of C99 additionally states:
Between the previous and next sequence point an object shall have its stored value modified at most once by the evaluation of an expression. Furthermore, the prior value shall be read only to determine the value to be stored.
§ 5.1.2.3 of C99 states:
At certain specified points in the execution sequence called sequence points, all side effects of previous evaluations shall be complete and no side effects of subsequent evaluations shall have taken place.
A list of sequence points is given in annex C and only the following matches the statement in question IMHO.
The end of a full expression
The evaluation of b[i]
(the value of element i
of b
) and that of i++
(just i
) can happen in any order before the addition (and evaluation of =
, which is the value of the RHS) is done. However, the side effects of the whole statement are deferred until after all these evaluations because that's the only sequence point. In this case the side effects are the change of x
and the increment of i
.
Who is right? Are there additional paragraphs relevant for the argument? Is it any different in C++?