任何人都可以向我解释以下代码输出背后的正确逻辑吗?
#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/