假设我有这个代码:
int main(void) { int i = rand(); if (++i) ++i; return i; }
这里定义的行为是什么?我知道i = i未定义,main中的第二行包含类似的内容. rand()调用是为了阻止编译器优化我认为是有问题的行.
解决方法
从C
standard
以下是描述的序列点: –
….
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 07001; the controlling expression of a selection statement (if or switch) 07002; 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.
所以是的,这是有效的.我只是向您展示标准报价,以便您对某个地方的规则表示满意.