Background:
C99 6.5 §2 said:
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.
Where “the prior value shall be read only to determine the value to be stored” would equal what C11 calls value computation (new formal term in C11).
The corresponding text in C11 6.5 §2:
If a side effect on a scalar object is unsequenced relative to either a different side effect on the same scalar object or a value computation using the value of the same scalar object, the behavior is undefined. If there are multiple allowable orderings of the subexpressions of an expression, the behavior is undefined if such an unsequenced side effect occurs in any of the orderings.
When dissecting this closer as done in this question, the last sentence in particular marks a difference in behavior between C99 and C11. In code such as this
#define get_next() (0,array[i++])
...
get_next() + get_next()
it does not modify the stored value more than once between the previous and next sequence point (C99). However in C11, in the expanded sub-expressions (0,array[i++]) + (0,array[i++])
, the two i++
are unsequenced in relation to each other, despite the sequence points. Essentially C11 allows the compiler to evaluate the program in this order:
0
0
array[i++]
array[i++]
+
So that would mean undefined behavior. Whereas C99 doesn’t care, the above is merely unspecified behavior there, C99 6.5 §3:
…the order of evaluation of subexpressions and the order in which side effects take place are both unspecified.
My question:
Did gcc and clang forget all about C99 (and earlier) compliance when implementing C11 support? Minimal example:
int i;
int array[10];
#define get_next() (0,array[i++])
int main (void)
{
get_next() + get_next();
}
clang 18.1.0 -std=c99 -pedantic
:
multiple unsequenced modifications to ‘i’ [-Wunsequenced]
What is “unsequenced”? There is no such term in C99 and why should I care about C11 and beyond issues in conforming C99 code?
gcc 14.1 -std=c99 -pedantic -Wall
:
operation on ‘i’ may be undefined
Undefined according to whom? Not according to C99 6.5 §2.
The diagnostic messages in each compiler appear to be incorrect. I don’t know if the compilers also generate code accordingly and treat this as UB, but if they do that might be yet another problem.