int main() {
int i =3;
printf("%d %d\n", ++i,++i);
//printf("%d %d\n", i++,i++);
return 0;
}
请问为什么输出5,5啊,是所有的前置加加完成后,再输出?如果删除第一个printf注释行部分输出4,3 这怎么理解
int main() {
int i =3;
printf("%d %d\n", ++i,++i);
//printf("%d %d\n", i++,i++);
return 0;
}
请问为什么输出5,5啊,是所有的前置加加完成后,再输出?如果删除第一个printf注释行部分输出4,3 这怎么理解
第一个printf,我们都知道,++i,是先让i自加再用它的值,如果两个++i,放在同一行,或者说printf函数里,它会让两个i自加完以后再调用它的值。
第二个printf,我开始也想了一下,后来我想到了,在c语言里有很多赋值操作都是从右向左的,这里也不例外,我们都知道i++,是先调用值,再自加,而这里是从右边那个i++开始的,所以是4 3,如果你不相信的话,可以在第二个printf函数里再加一个i––作为验证。
我是这么想的,但不知道对不对,大家都是交流交流,希望能帮到你,如果说错了,还请大神指出来。
看了楼上的回答,这种情况应该算是未定义行为,取决于编译器实现。下面补充一些参考:
1) sequence point from wikipedia
在C与C++程序设计语言中,表达式的值依赖于它的子表达式的求值顺序。增加更多的顺序点限制了可能的求值顺序,能保证有一个一致结果。
其中的一个sequence point是函数调用时的函数入口点。函数实参的求值顺序未指定,但顺序点意味着这些实参求值的副作用在进入函数时都已经完成。表达式f(i++) + g(j++) + h(k++),调用f(), g(), h()的顺序未指定,i, j, k的自增顺序也未指定。函数调用f(a,b,c)的实参列表不是逗号运算符,a, b, and c的求值顺序未指定。
C语言的所有sequence point可以参考上面链接,但是可以发现,上面的例子所涉及的关键部分并没有包含确定的sequence point,因此行为时undefined。
gcc有个-Wsequence-point选项可以用来对违反c/c++ sequence point规则而引发的未定义行为进行警告。经过测试,gcc -Wsequence-point 1.c 编译上述代码都会发现下面的警告:
1.c:4:27: warning: operation on ‘i’ may be undefined [-Wsequence-point]
printf("%d %d\n", ++i,++i);
^
-Wsequence-point说明也可以解释楼主的疑惑:
It is not specified when between sequence points modifications to the values of objects take effect. Programs whose behavior depends on this have undefined behavior; the C and C++ standards specify that "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.". If a program breaks these rules, the results on any particular implementation are entirely unpredictable.
2) increment and decrement operators from wikipedia
Since the increment/decrement operator modifies its operand, use of such an operand more than once within the same expression can produce undefined results. For example, in expressions such as x − ++x, it may not be clear to a user in what sequence the subtraction and increment operations should be performed. Such expressions generally invoke undefined behavior, and should be avoided.
1 回答3.3k 阅读
1 回答1.1k 阅读✓ 已解决
1.1k 阅读
对应的汇编代码是:
根据参数传递顺序, 传第2个参数的时候, 先
28(%esp), %edx
保存原来的值, 作为后面传入的第2个参数, 这一步其实就已经算使用了i, 所以马上又执行i++, 也就是incl 28(%esp)
同样的, 接下来传第1个参数, 也是先保存i原来的值(这个时候i已经等于6了), 然后再i++