C语言里整型比较的一个诡异之处

以下代码定义了一个整型int_min,值为0x80000000。在计算机补码表示法下,0x80000000是int可表示的最小的数。
然后取int_min的负数,根据补码运算规则,-int_min的位模式和int_min相同,也等于0x80000000。
神奇的事情来了,-int_min = int_min < 0 竟然不成立。

#include <limits.h>
#include <assert.h>

void test_int_min() {
    int int_min = 0x80000000;
    assert(int_min == INT_MIN);     // True
    assert(int_min < 0);            // True
    assert(int_min == -int_min);    // True
    assert(-int_min < 0);           // False
}

在so上面找到了一个类似问题 Why is 0 < -0x80000000?,猜测-int_min和0比较的时候自动转换成了unsigned类型,于是对-int_min进行强制类型转换,没想到依然是False。

assert((int)-int_min < 0);      // False

再考虑到可能是编译器的问题,但是不论windows+mingw64+gcc8.1还是Kali+gcc8.2编译运行结果都是一样,用gbd单步调试得到的结果也是-int_min < 0

clipboard.png

编译是用的最简单的命令gcc test_int_min.c -o test_int_min,没有额外编译参数。

补充1:完成操作流程。

PS E:\Repository\csapp\tests> Get-WmiObject -Class Win32_OperatingSystem | Select-Object -ExpandProperty Caption
Microsoft Windows 10 教育版
PS E:\Repository\csapp\tests> gcc --version
gcc.exe (x86_64-win32-seh-rev0, Built by MinGW-W64 project) 8.1.0
Copyright (C) 2018 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

PS E:\Repository\csapp\tests> cat t.c
#include <limits.h>
#include <assert.h>

void test_int_min() {
    int int_min = 0x80000000;
    assert(int_min == INT_MIN);     // True
    assert(int_min < 0);            // True
    assert(int_min == -int_min);    // True
    assert(-int_min < 0);           // False
}


int main(int argc, char const *argv[])
{
    test_int_min();
    return 0;
}
PS E:\Repository\csapp\tests> gcc t.c -o t.exe
PS E:\Repository\csapp\tests> .\t.exe
Assertion failed!

Program: E:\Repository\csapp\tests\t.exe
File: t.c, Line 9

Expression: -int_min < 0
PS E:\Repository\csapp\tests> bash
kali@A003657-PC01:/mnt/e/Repository/csapp/tests$ cat /etc/issue
Kali GNU/Linux Rolling \n \l

kali@A003657-PC01:/mnt/e/Repository/csapp/tests$ gcc --version
gcc (Debian 8.2.0-13) 8.2.0
Copyright (C) 2018 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

kali@A003657-PC01:/mnt/e/Repository/csapp/tests$ gcc t.c -o t
kali@A003657-PC01:/mnt/e/Repository/csapp/tests$ ./t
t: t.c:9: test_int_min: Assertion `-int_min < 0' failed.
Aborted (core dumped)
kali@A003657-PC01:/mnt/e/Repository/csapp/tests$

补充2:在centos7+gcc4.8环境下能得到预期结果。

clipboard.png

有人能解释一下吗?

阅读 3.8k
1 个回答

c语言都是直接编译为机器指令,-int_min这个运算,在x86 cpu上就是一条指令,其他cpu应该也一样。所以这个问题不是c的问题,而是在整数表示范围边界上的取补码运算的cpu指令的行为,是cpu处理方法不同导致的。

你可以反汇编看看具体是什么cpu指令,然后查cpu指令的文档。

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进