【发布时间】:2015-10-14 01:02:31
【问题描述】:
这里是代码
int main()
{
int x=15;
printf("%d %d %d %d",x=1,x<20,x*1,x>10);
return 0;
}
输出是1 1 1 1
我期待 1 1 15 1 作为输出,
x*1 等于 15 但这里 x*1 是 1 ,为什么?
在printf() 中使用赋值运算符或修改值会导致undefined behaviour?
【问题讨论】:
-
您明确设置了
x=1。我不确定你还能期待什么。 -
虽然不是问题的直接重复,但Why are these constructs using pre and post-increment undefined behavior? 的答案也确实深入解决了这个问题。
标签: c gcc printf assignment-operator