#include<stdio.h>
int main()
{
int x=2;
x=x++;
printf("%d",x);
return 0;
}
根据我的逻辑输出: 2
Windows 上的输出: 3
Linux 上的输出: 2
为什么 Windows 给出 3 作为输出。 根据我的理解,x++ 将 2 递增到 3,但返回 2。所以 x 应该有 2。Windows 对此的评估有什么不同吗?
同样:
#include<stdio.h>
int main()
{
int x=2,y=4;
x=x++ + ++y;
printf("%d %d",x,y);
return 0;
}
根据我的输出: 7 5
Windows 中的输出: 8 5
Linux 上的输出: 7 5
同样的情况。
请帮忙......
最佳答案
x = x++
是未定义的行为,因此两个编译器生成两段不同的代码。
只需 x++
就足以满足您的第一段代码。
Here is a question with your exact problem for the second piece of code
关于c - gcc 对 Windows 和 Linux 给出不同的结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32146641/