gcc 为 windows 和 linux 给出不同的结果

gcc giving different result for windows and linux

#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,但 return 返回 2。所以 x 应该 2.Is 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