printf 中的一元运算符
谁能解释一下以下的输出。我试图推理一切,并可以解释后面的部分,其中“x”被分配了表达式的值,但无法理解 printf 语句中的答案有何不同!
不同的编译器可能表现不同。如果有人能为任何编译器解释这种行为,那就太好了。
我在 openSUSE 12.1 (Asparagus) (i586) 上使用 gcc (SUSE Linux) 4.6.2
代码:
#include<stdio.h>
int main()
{
unsigned int x=0;
printf("expr= %d x=%d\n",(x^x),x);
printf("x=%d\n",x);
x=0;
printf("expr= %d x=%d\n",(x^x)||x++,x);
printf("x=%d\n",x);
x=0;
printf("expr= %d x=%d\n",(x^x)||x++||++x,x);
printf("x=%d\n",x);
x=0;
printf("expr= %d x=%d\n",(x^x)||x++||++x||x++,x);
printf("x=%d\n",x);
x=0;
printf("expr= %d x=%d\n",x++,x);
printf("x=%d\n",x);
x=0;
printf("expr= %d x=%d\n",++x||x++,x);
printf("x=%d\n",x);
x=0;
printf("expr= %d x=%d\n",x++||++x||x++,x);
printf("x=%d\n",x);
x=0;
printf("expr= %d x=%d\n",(x^x)||x++||++x||x++,x);
printf("x=%d\n",x);
x=0;
(x^=x);
printf("x=%d\n",x);
x=0;
(x^=x)||x++;
printf("x=%d\n",x);
x=0;
(x^=x)||x++||++x;
printf("x=%d\n",x);
x=0;
(x^=x)||x++||++x||x++;
printf("x=%d\n",x);
return 0;
}
输出:
expr= 0 x=0
x=0
expr= 0 x=1
x=1
expr= 1 x=2
x=2
expr= 1 x=2
x=2
expr= 0 x=1
x=1
expr= 1 x=1
x=1
expr= 1 x=2
x=2
expr= 1 x=2
x=2
x=0
x=1
x=2
x=2
谢谢
Can anyone explain me the output of the following. I tried to reason everything and can explain the later part where 'x' is assigned the value of the expression but cannot understand how the answer is different in a printf statement!!!
Different compilers might behave differently. It would be great if someone could explain this behavior for any compiler.
I am using gcc (SUSE Linux) 4.6.2 on openSUSE 12.1 (Asparagus) (i586)
code :
#include<stdio.h>
int main()
{
unsigned int x=0;
printf("expr= %d x=%d\n",(x^x),x);
printf("x=%d\n",x);
x=0;
printf("expr= %d x=%d\n",(x^x)||x++,x);
printf("x=%d\n",x);
x=0;
printf("expr= %d x=%d\n",(x^x)||x++||++x,x);
printf("x=%d\n",x);
x=0;
printf("expr= %d x=%d\n",(x^x)||x++||++x||x++,x);
printf("x=%d\n",x);
x=0;
printf("expr= %d x=%d\n",x++,x);
printf("x=%d\n",x);
x=0;
printf("expr= %d x=%d\n",++x||x++,x);
printf("x=%d\n",x);
x=0;
printf("expr= %d x=%d\n",x++||++x||x++,x);
printf("x=%d\n",x);
x=0;
printf("expr= %d x=%d\n",(x^x)||x++||++x||x++,x);
printf("x=%d\n",x);
x=0;
(x^=x);
printf("x=%d\n",x);
x=0;
(x^=x)||x++;
printf("x=%d\n",x);
x=0;
(x^=x)||x++||++x;
printf("x=%d\n",x);
x=0;
(x^=x)||x++||++x||x++;
printf("x=%d\n",x);
return 0;
}
output :
expr= 0 x=0
x=0
expr= 0 x=1
x=1
expr= 1 x=2
x=2
expr= 1 x=2
x=2
expr= 0 x=1
x=1
expr= 1 x=1
x=1
expr= 1 x=2
x=2
expr= 1 x=2
x=2
x=0
x=1
x=2
x=2
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您正在调用未指定的行为。
在像 func(a,b) 这样的表达式中,C 标准没有指定应该首先计算哪个参数;编译器可以自由地执行任一操作。
所以现在考虑 func(x++,x);未指定它是否等同于 this:
或 this:
You are invoking unspecified behaviour.
In an expression like
func(a,b)
, the C standard does not specify which argument should be evaluated first; the compiler is free to do either.So now consider
func(x++,x)
; it is unspecified whether it is equivalent to this:or this:
此函数显示未指定的行为。
(x^x)||x++||++x
和x
之间的求值顺序未指定。程序中的大多数其他
printf
调用都存在相同的问题。输出依赖于未指定行为的程序不是严格符合的程序(参见 C99,4.p5)。
This function shows unspecified behavior. The order of evaluation between
(x^x)||x++||++x
andx
is unspecified.Most of the other
printf
calls in your program have the same issue.A program whose output depends on unspecified behavior is not a strictly conforming program (see C99, 4.p5).