我无法理解的 7 行简单 C 代码
A 7 Line Simple C Code That I Can't Figure Out
谁能解释一下为什么这里的输出是2 1 1,这里c的优先级是什么?为什么 i++ 的输出是 1。提前致谢
~
#include <stdio.h>
void main(){
int i=1;
int *p=&i;
printf("%d%d%d\n",*p,i++,i);
}
根据 C 2018 6.5 2,此代码的行为未由 C 标准定义:
If a side effect on a scalar object is unsequenced relative to either a different side effect on the same scalar object or a value computation using the value of the same scalar object, the behavior is undefined…
i++
具有用其增量值更新 i
的副作用。函数调用参数还包括 *p
和 i
,它们都引用 i
并使用它的值。 i
是标量对象。函数调用参数的计算顺序及其副作用是无序的(这意味着 C 标准不对它们强加任何顺序要求)。因此,满足上述规则的所有条件,因此该行为未由 C 标准定义。
谁能解释一下为什么这里的输出是2 1 1,这里c的优先级是什么?为什么 i++ 的输出是 1。提前致谢 ~
#include <stdio.h>
void main(){
int i=1;
int *p=&i;
printf("%d%d%d\n",*p,i++,i);
}
根据 C 2018 6.5 2,此代码的行为未由 C 标准定义:
If a side effect on a scalar object is unsequenced relative to either a different side effect on the same scalar object or a value computation using the value of the same scalar object, the behavior is undefined…
i++
具有用其增量值更新 i
的副作用。函数调用参数还包括 *p
和 i
,它们都引用 i
并使用它的值。 i
是标量对象。函数调用参数的计算顺序及其副作用是无序的(这意味着 C 标准不对它们强加任何顺序要求)。因此,满足上述规则的所有条件,因此该行为未由 C 标准定义。