微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

c – g:警告:对于’long’类型,整数常量太大

我能做什么(以编程方式)摆脱警告?
...
 unsigned long long v=(unsigned long long)0xffffeeeeddddcccc;
 ...

g++ main.cpp -o main
main.cpp:6: warning: integer constant is too large for ‘long’ type

但是当我运行程序时,一切都如预期的那样好:

./main
  sizeof(unsigned long long)==8
  value of v==0xffffeeeeddddcccc

使用环境:

> Ubuntu-Karmic 32bit
> g版本:v4.4.1

编辑:这是完整和可编译的main.cpp

#include <iostream>
#include <iomanip>
using namespace std;

int main(void) {
  unsigned long long v=(unsigned long long)0xffffeeeeddddcccc;
  const unsigned v_size = sizeof(unsigned long long);
  cout << "sizeof(unsigned long long)==" << v_size << endl;
  cout << "value of v==0x" << setw(v_size) << setfill('0') << hex << v << endl;
  return 0;
}

解决方法

根据C标准2.13.1 / 2:

The type of an integer literal depends on its form,value,and suffix. If it is decimal and has no suffix,it has the first of these types in which its value can be represented: int,long int; if the value cannot be represented as a long int,the behavior is undefined.

新C标准允许使用ull后缀. g 4.4.1支持此后缀以及Visual C 2008.您可以按如下方式使用它:

unsigned long long v=0xffffeeeeddddccccULL;

原文地址:https://www.jb51.cc/c/119744.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐