我想下面的代码打印11,但打印12,除了在后一种情况下,它打印10操作数的计算顺序+分配副作用
x=5; x1=x+(x=6); printf("%d\n",x1);
x=5; x1=(x=x)+(x=6); printf("%d\n",x1);
x=5; x1=(x+0)+(x=6); printf("%d\n",x1);
x=5; x1=(x=5)+(x=6); printf("%d\n",x1);
x=5; x1=(x=6)+x; printf("%d\n",x1);
x=5; x1=(x=6)+(x=x); printf("%d\n",x1);
x=5; x1=(x=6)+(x+0); printf("%d\n",x1);
x=5; x1=(x=6)+(x=5); printf("%d\n",x1);
GCC在任何情况下说:“警告: 'x'上的操作可能是未定义的'。
就是这样。
Bernhard
PS:没有问题,对不起。感谢您的回答。 :)
PPS:实际的代码是:
while (data-(data=read(adr)&(1<<6))) i++;
我在ADR等6位停止切换。
变异变量序列点之间两次是un定义的行为。因此,变异变量,然后读取它的值。 无论您期望得到什么样的价值,都是无效的假设。您应该重写代码以符合标准。 – 2010-06-04 21:24:54
[任何人都可以解释这些未定义的行为(i = i ++ + ++ i,i = i ++等...)](http://stackoverflow.com/questions/949433/could-anyone-explain-these- undefined-behaviors-iiiii-etc) – 2010-06-04 21:25:46
结果是,如您的编译器诊断,未定义。所以不要写这样的代码。 – 2010-06-04 21:26:23