Shi*_*hab -3 c operator-precedence post-increment
我执行了以下代码.
int main(void)
{
int c;
c=0;
printf("%d..%d..%d \n", c,c,c);
c=0;
printf("%d..%d..%d \n", c,c,c++);
c=0;
printf("%d..%d..%d \n", c,c++,c);
c=0;
printf("%d..%d..%d \n", c++,c,c);
return 0;}
Run Code Online (Sandbox Code Playgroud)
我期望输出为
0..0..0
1..1..0
0..1..0
0..0..0
但是输出(用gcc编译)是
0..0..0
1..1..0
1..0..1
0..1..1
我的期望有什么问题?在gcc中,评估顺序是从右到左.是吗?
我的期望有什么问题?
未指定功能参数的评估顺序 - 由实现决定.此外,参数之间没有序列点*,因此在序列点之前再次使用修改后的参数无法给出可预测的结果:它是未定义的行为(谢谢,Eric,用于提供对标准的引用).
如果需要特定的评估顺序,则需要将参数计算为完整表达式(强制每个表达式之间的序列点):
int arg1 = c++;
int arg2 = c;
int arg3 = c;
// This: printf("%d..%d..%d \n", c++,c,c);
// Becomes this:
printf("%d..%d..%d \n", arg1, arg2, arg3);
Run Code Online (Sandbox Code Playgroud)