long long是8字节,但是我得到整数溢出? [英] long long is 8 bytes, but I get integer overflow?

查看:858
本文介绍了long long是8字节,但是我得到整数溢出?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设

  long long b = 5 * 1024 * 1024 * 1024; // 5个演出,足够小到64位
printf(%lu \\\
,sizeof(long long)); //打印8(字节)= 64位

但编译器抱怨:

 警告:表达式中的整数溢出[-Woverflow] 

为什么它会溢出,我错过了什么?

解决方案

因为右边的数字是 int ,而不是 long long ,所以 int 在表达式中执行算术运算,导致溢出。



如果您向其中的一个添加 LL ,它会他们会全力推广他们。


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?

解决方案

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.

If you add LL to one of them, it'll promote them all.

这篇关于long long是8字节,但是我得到整数溢出?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆