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

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

概述我能做什么(以编程方式)摆脱警告? ... unsigned long long v=(unsigned long long)0xffffeeeeddddcccc; ...g++ main.cpp -o mainmain.cpp:6: warning: integer constant is too large for ‘long’ type 但是当我运行程序时,一切都如预期的那样好: 我能做什么(以编程方式)摆脱警告?
... unsigned long long v=(unsigned long long)0xffffeeeeddddcccc; ...g++ main.cpp -o mainmain.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 – g:警告:对于’long’类型,整数常量太大全部内容,希望文章能够帮你解决c – g:警告:对于’long’类型,整数常量太大所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/1242432.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-06
下一篇 2022-06-06

发表评论

登录后才能评论

评论列表(0条)

保存