Suppose
假设
long long b = 5*1024*1024*1024; // 5 gigs, small enough for 64 bits
printf ("%lu\n",sizeof(long long)); // prints 8 (bytes) = 64 bits
but the compiler complains:
但编译器抱怨:
warning: integer overflow in expression [-Woverflow]
Why does it overflow, what am I missing?
为什么会溢出,我错过了什么?
1 个解决方案
#1
19
Because the numbers on the right hand side are of type int
, not long long
, so int
arithmetic is performed in the expression, leading to an overflow.
因为右侧的数字是int类型,而不是很长,所以在表达式中执行int算术,导致溢出。
If you add LL
to one of them, it'll promote them all.
如果你向其中一个添加LL,它将全部提升它们。
#1
19
Because the numbers on the right hand side are of type int
, not long long
, so int
arithmetic is performed in the expression, leading to an overflow.
因为右侧的数字是int类型,而不是很长,所以在表达式中执行int算术,导致溢出。
If you add LL
to one of them, it'll promote them all.
如果你向其中一个添加LL,它将全部提升它们。