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

前端之家收集整理的这篇文章主要介绍了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.f2er.com/c/119744.html

猜你在找的C&C++相关文章