假设我想打印一些值。我假设如果我的有符号变量超过 from TMin
and TMax
(在这种情况下,使用 4 字节 int,0x7FFFFFFF
asTmax
和0x80000000
as Tmin
),我应该得到整数溢出,但在这些示例中,我没有得到我期望的结果(在评论中解释):
// Tmax = 0x7FFFFFFF == 2147483647
// Tmin = 0x80000000 == -2147483648
printf("1- overflow test: %d\n", 0x7FFFFFFF+1 ); // overflow - how compiler finds out it's a signed value, not an Unsigned one
printf("2- overflow test: %d\n", 0x80000000-1 ); // in contrast, why I am not getting an integer overflow here
printf("3- overflow test: %d\n", (int) 0x80000000-1 ); // overflow (Expected)
printf("4- overflow test: %d\n",(int) (0x7FFFFFFF+1)); // overflow (Expected)