简体   繁体   English

为什么32位程序的结果与64位程序的结果不同?

[英]Why is the result of the 32-bit program different from the 64-bit one?

I was working on a assignment on integer byte level representation. 我正在进行整数字节级表示的赋值。 And I wrote a little program: 我写了一个小程序:

e1.c e1.c

int main(void) {
    printf("%d\n", -2147483648 < 2147483647);
    return 0;
}

When I compiled a 32-bit version of the executable file using the C89 standard, with the command gcc e1.c -m64 -std=c89 -g -O0 -o e1 , it worked as I expected: it printed 0 indicating that C compiler regarded the value 2147483648 as unsigned int , thus it converts the rest of the expression to unsigned int . 当我使用C89标准编译32位版本的可执行文件时,使用命令gcc e1.c -m64 -std=c89 -g -O0 -o e1 ,它按预期工作:它打印0表示C编译器将值2147483648视为unsigned int ,因此它将表达式的其余部分转换为unsigned int But weirdly this relationship doesn't hold in the 64-bit version, which prints 1 . 但奇怪的是,这种关系并不适用于打印1的64位版本。

Can anyone explain that? 有谁能解释一下?

The C89 spec reads: C89规范如下:

The type of an integer constant is the first of the corresponding list in which its value can be represented. 整数常量的类型是相应列表中可以表示其值的第一个。 Unsuffixed decimal: int , long int , unsigned long int ; unsuffixed decimal: intlong intunsigned long int ; [...] [...]

Thus, the type of the literal 2147483648 depends on the size of int , long , and unsigned long , respectively. 因此,文字2147483648的类型分别取决于intlongunsigned long的大小。 Let's assume int is 32 bits, as it is on many platforms (and is likely the case on your platforms). 假设int是32位,因为它在许多平台上(在您的平台上可能就是这种情况)。

On a 32-bit platform, it's common for long to be 32 bits. 在32位平台上, long 32位是long常见的。 Thus, the type of 2147483648 would be unsigned long . 因此, 2147483648的类型将是unsigned long

On a 64-bit platform, it's common for long to be 64 bits (though some platforms, like MSVC, will still use 32 bits for long ). 在64位平台上,通常long 64位(尽管某些平台,如MSVC,仍然会long使用32位)。 Thus, the type of 2147483648 would be long . 因此, 2147483648的类型会很long

This leads to the discrepancy you see. 这会导致你看到的差异。 In one case, you're negating an unsigned long , and in the other case, you're negating a long . 在一种情况下,你是在否定一个unsigned long ,而在另一种情况下,你是在否定一个long

On a 32-bit platform, -2147483648 evaluates to 2147483648 (using the unsigned long type). 在32位平台上, -2147483648计算结果为2147483648 (使用unsigned long类型)。 Thus the resulting comparison is 2147483648 < 2147483647 , which evaluates to 0 . 因此,得到的比较是2147483648 < 2147483647 ,其评估为0

On a 64-bit platform, -2147483648 evaluates to -2147483648 (using the long type). 在64位平台上, -2147483648计算结果为-2147483648 (使用long类型)。 Thus the resulting comparison is -2147483648 < 2147483647 , which evaluates to 1 . 因此,得到的比较是-2147483648 < 2147483647 ,其评估为1

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 从引导加载程序调用32位或64位程序 - Call 32-bit or 64-bit program from bootloader 使用 32 位编译器编译的结果与使用 64 位编译器编译的结果不同 - Compilation using 32-bit compiler gives different result from compilation using 64-bit compiler 32位和64位之间完全不同的输出 - Completely different output between 32-bit and 64-bit 在 32 位和 64 位上编译,但校验和不同 - Compiled on 32-bit and 64-bit but checksum is different C:如果参数是从右往左推,为什么会出现下面的情况? (64位操作系统,32位程序) - C: If the parameters are pushed from right to left, why do the following happen? (64-bit operation system, 32-bit program) 为什么 32 位和 64 位程序的结构大小不同? - Why does the structure size differ in 32-bit and 64-bit program? 从32位和64位二进制文​​件中读取 - Reading from a binary file on 32-bit and 64-bit 在GCC中使用内联汇编从32位IMUL返回64位结果 - Returning a 64-bit result from a 32-bit IMUL with inline assembly in GCC 为什么cmake在64位系统上找到32位库而不是64位库? - Why is cmake finding 32-bit libraries instead of 64-bit libs on a 64-bit system? 在32位Debian Squeeze下编译的AC程序导致我朋友的64位程序段出现段错误 - A C program compiled under 32-bit Debian Squeeze causes a segfault on my friend's 64-bit one
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM