"Does the definition int a = 0, b = a++, c = a++; have defined behavior in C?"...个
在当前的C标准ISO/IEC9899:2017中,程序执行包含在第5.1.2.3(3)节中,其中包括对排序和副作用的讨论.原文转载如下,以供参考.
从下面的文本部分总结,声明语句中的初始值设定项是按顺序排列的,以保证声明中的初始值设定项表达式已发布...
int a = 0, b = a++, c = a++;
which describes an "...init-declarator-list [which] is a comma-separated sequence of declarators," (section 6.7 Declarations)
...will not invoke undefined behavior, or even indeterminate results. Each comma separated expression is guaranteed to be sequenced starting from left, and not moving to the right until all evaluations and side-effects for the current expression are resolved and complete. In this way the results of each expression is fully defined.
摘自§5.1.2.3
"Sequenced before is an asymmetric, transitive, pair-wise relation
between evaluations executed by a single thread, which induces a
partial order among those evaluations. Given any two evaluations A and
B, if A is sequenced before B, then the execution of A shall precede
the execution of B. (Conversely, if A is sequenced before B, then B is
sequenced after A.) If A is not sequenced before or after B, then A
and B are unsequenced. Evaluations A and B are indeterminately
sequenced when A is sequenced either before or after B, but it is
unspecified which.13) The presence of a sequence point between the
evaluation of expressions A and B implies that every value computation
and side effect associated with A is sequenced before every value
computation and side effect associated with B. (A summary of the
sequence points is given in annex C.)"个
附件C规定的有关段落:
"The following are the sequence points described in 5.1.2.3:"+(3)
......
"Between the evaluation of a full expression and the next full
expression to be evaluated. The following are full expressions: a full
declarator for a variably modified type; an initializer that is not
part of a compound literal (6.7.9); the expression in an expression
statement (6.8.3); the controlling expression of a selection statement
(if or switch) (6.8.4); the controlling expression of a while or do
statement (6.8.5); each of the (optional) expressions of a for
statement (6.8.5.3); the (optional) expression in a return statement
(6.8.6.4)".
(emphasis mine)