关于顺序点(sequence point),在C标准中有解释,不过很晦涩。
The sequence points laid down in the Standard are the following:
- The point of calling a function, after evaluating its arguments.
- The end of the first operand of the
&&
operator. - The end of the first operand of the
||
operator. - The end of the first operand of the
?:
conditional operator. - The end of the each operand of the comma operator.
- Completing the evaluation of a full expression. They are the following:
- Evaluating the initializer of an
auto
object. - The expression in an ‘ordinary’ statement—an expression followed by semicolon.
- The controlling expressions in
do
,while
,if
,switch
orfor
statements. - The other two expressions in a for statement.
- The expression in a
return
statement.
- Evaluating the initializer of an
我们在平时编码中尽量避免写出与实现相关、受实现影响的代码便是了。而-Wsequence-point选项恰恰可以帮我们这个忙,它可以帮我们查出这样的代码来,并给出其警告。
e.g.
gcc -Wsequence-point test_sequence_point.c
test_sequence_point.c: In function `main':
test_sequence_point.c:10: warning: operation on `i' may be undefined
在两个平台上给出的编译警告都是一致的,但是输出结果却大相径庭。
Solaris输出:
the i is 11
Windows输出:
the i is 12
类似的像这种与顺序点相关的代码例子有:
这样子的代码往往不能按照你所想像的结果输出,所以这样子的情况尽量避免吧。
等等...
引用 http://publications.gbdirect.co.uk/c_book/chapter8/sequence_points.html