gpt4 book ai didi

c - printf ("%d %d %d\n",++a, a++,a) 输出

转载 作者:行者123 更新时间:2023-12-03 18:28:06 25 4
gpt4 key购买 nike

这个问题在这里已经有了答案:




12 年前关闭。




Possible Duplicate:
Could anyone explain these undefined behaviors (i = i++ + ++i , i = i++, etc…)



我无法理解该程序的输出(使用 gcc )。
main()
{
int a=10;
printf("%d %d %d\n",++a, a++,a);
}

输出:
12 10 12

另外,请解释 printf() 的参数求值顺序.

最佳答案

编译器将评估 printf的论点按当时感觉的任何顺序进行。这可能是一个优化的事情,但不能保证:它们被评估的顺序不是由标准指定的,也不是实现定义的。没有办法知道。
但是标准规定的是,在一次操作中修改同一个变量两次是未定义的行为; ISO C++03, 5[expr]/4:

Between the previous and next sequence point a scalar object shall have its stored value modified at most once by the evaluation of an expression. Furthermore, the prior value shall be accessed only to determine the value to be stored. The requirements of this paragraph shall be met for each allowable ordering of the subexpressions of a full expression; otherwise the behavior is undefined.

printf("%d %d %d\n",++a, a++,a);可以做很多事情;以您期望的方式工作,或以您永远无法理解的方式工作。
你不应该写这样的代码。

关于c - printf ("%d %d %d\n",++a, a++,a) 输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1270370/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com