c - gcc 对 Windows 和 Linux 给出不同的结果

标签 c int

#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/

相关文章:

java - Java中Integer转换为int作为栈的返回类型

android - 使用按钮更改 int 值

java - 具有相同值的 Int 到 Byte

C 的格式说明符

c - 使用 C 从服务器获取和存储图像的 HTTP 客户端

python - 如何使用Python字典值作为整数、 float 或 bool 值?

c - 语法错误 : missing ';' before 'type' IN C

c++ - 为什么编译器允许字符串文字不是 const?

c - 如何使我的程序 "forget"成为我在键盘上按下的字母?

c - 如何在我的 C 程序中的所有函数中访问数组?