考虑以下表达式中的序列点
i = (++i,i++,i);
如果我是正确的,执行步骤将如下:
1)
++i, i++
2)
step1,i
3)
i = step2
对于步骤 1 中的求值,i 的值只需修改一次即可将构造定义为已定义(因为在求值逗号运算符之后存在一个序列点)。但我认为这里的情况并非如此。因此它应该是未定义的。 请看这个answer 。这里上述表达式被称为定义。我错过了什么吗?
最佳答案
以下指令的行为已明确定义。
i = (++i, i++, i);
逗号运算符 (,
) 的第一个和第二个操作数的计算之间确实存在一个序列点。标准的附录 C 虽然内容丰富,但提供了序列点的描述。
C11, Annex C Sequence points
The following are the sequence points described in 5.1.2.3:
— Between the evaluations of the function designator and actual arguments in a function call and the actual call. (6.5.2.2).
— Between the evaluations of the first and second operands of the following operators: logical AND && (6.5.13); logical OR || (6.5.14); comma , (6.5.17).
— Between the evaluations of the first operand of the conditional ? : operator and whichever of the second and third operands is evaluated (6.5.15). — The end of a full declarator: declarators (6.7.6);
— Between the evaluation of a full expression and the next full expression to be evaluated. The following are full expressions: 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).
— Immediately before a library function returns (7.1.4). — After the actions associated with each formatted input/output function conversion specifier (7.21.6, 7.29.2).
— Immediately before and immediately after each call to a comparison function, and also between any call to a comparison function and any movement of the objects passed as arguments to that call (7.22.5).
关于c - 以下表达式的行为是否明确定义?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12225639/