为什么这个小程序输出 True? GCC 有溢出保护吗?

Why this little program output True? Is GCC's overflow protection?

#include<stdio.h>

void main(){

    int x = 0x80000000;
    if((x-1)<1)
        printf("True");
    else
        printf("False");

}

这是csapp练习2.44的,如果是编译器操作,怎么关闭?

假设 int 是 32 位,常量 0x80000000 超出 int 的范围并且类型为 unsigned int。当用于初始化 int 时,它以实现定义的方式进行转换。对于 gcc,该转换导致 x 具有值 -231(其表示恰好是 0x80000000),这是它可以容纳的最小值。

然后当您尝试计算 x-1 时,它会导致有符号整数溢出,即 undefined behavior。例如,如果我在 gcc 4.8.5 下用 -O0-O1 编译这段代码,我得到“False”作为输出,如果我用 -O2 或 [=21 编译=] 它输出“真”。