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;

相关文章

本程序的编译和运行环境如下(如果有运行方面的问题欢迎在评...
水了一学期的院选修,万万没想到期末考试还有比较硬核的编程...
补充一下,先前文章末尾给出的下载链接的完整代码含有部分C&...
思路如标题所说采用模N取余法,难点是这个除法过程如何实现。...
本篇博客有更新!!!更新后效果图如下: 文章末尾的完整代码...
刚开始学习模块化程序设计时,估计大家都被形参和实参搞迷糊...