为什么会出现整数溢出,如何解决?
Why do I get an integer overflow, and how do I fix it?
尝试在我的代码中断中编译以下行:
printf("%llu\n", 0x12345678 * 0x12345678);
我明白了:
program.c:45:34: warning: integer overflow in expression [-Woverflow]
printf("%llu\n", (0x12345678 * 0x12345678));
我该如何解决这个问题?
[根据 ] 评论接受更正后
在您的机器上,0x12345678
比 unsigned long long
窄 - 当然是 signed long
或者 int
.
A signed long * signed long
仍然是 signed long
并且可能遭受 有符号整数 溢出,这是 UB。你的 signed long
的范围小于 0x12345678 * 0x12345678
的数学乘积。通过使用 ULL
后缀,数学至少用 unsigned long long
数学完成。
printf("%llu\n", 0x12345678ULL * 0x12345678);
// or if the constant can not be changed
printf("%llu\n", 1ULL * SOME_BIG_CONSTANT * SOME_BIG_CONSTANT);
迂腐的注解:当打印 可能 比 int/unsigned
宽的整数类型时,确保最终计算结果与说明符匹配。考虑 SOME_BIG_CONSTANT 可能比 unsigned long long
宽。或者不进行强制转换,并应对潜在的编译器警告。
printf("%llu\n", (unsigned long long) (1ULL * SOME_BIG_CONSTANT * SOME_BIG_CONSTANT));
另见
和
尝试在我的代码中断中编译以下行:
printf("%llu\n", 0x12345678 * 0x12345678);
我明白了:
program.c:45:34: warning: integer overflow in expression [-Woverflow]
printf("%llu\n", (0x12345678 * 0x12345678));
我该如何解决这个问题?
[根据
在您的机器上,0x12345678
比 unsigned long long
窄 - 当然是 signed long
或者 int
.
A signed long * signed long
仍然是 signed long
并且可能遭受 有符号整数 溢出,这是 UB。你的 signed long
的范围小于 0x12345678 * 0x12345678
的数学乘积。通过使用 ULL
后缀,数学至少用 unsigned long long
数学完成。
printf("%llu\n", 0x12345678ULL * 0x12345678);
// or if the constant can not be changed
printf("%llu\n", 1ULL * SOME_BIG_CONSTANT * SOME_BIG_CONSTANT);
迂腐的注解:当打印 可能 比 int/unsigned
宽的整数类型时,确保最终计算结果与说明符匹配。考虑 SOME_BIG_CONSTANT 可能比 unsigned long long
宽。或者不进行强制转换,并应对潜在的编译器警告。
printf("%llu\n", (unsigned long long) (1ULL * SOME_BIG_CONSTANT * SOME_BIG_CONSTANT));
另见
和