gpt4 book ai didi

c - 当a=1时a,a++,++a的输出是多少;

转载 作者:行者123 更新时间:2023-11-30 21:42:15 25 4
gpt4 key购买 nike

任何人都可以向我解释以下代码输出背后的正确逻辑吗?

 #include <stdio.h>
int main()
{
int a=1;
printf("%d\t%d\t%d\n",a,a++,++a);
return 0;
}

预期输出为 3 2 3

最佳答案

您的代码是undefined behavior ,所以它不存在“正确的逻辑”——它可以输出 0 0 0, 1 1 2, hello, world,或发出 nasal demons .

来自C99 standard :

  • 第 6.5 节第 2 段:

    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.

  • 第 6.5.2.2 节第 10 段:

    The order of evaluation of the function designator, the actual arguments, and subexpressions within the actual arguments is unspecified, but there is a sequence point before the actual call.

关于c - 当a=1时a,a++,++a的输出是多少;,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28344634/

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