我可以通过编程做什么来消除这个警告?
...
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 32位
- 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;
}
ull
是新的 C++ 标准的一部分。g++ 4.4.1 支持这个后缀。 - Kirill V. LyadvinskyULL
和其他后缀写成大写字母,以便它们在视觉上有所区别。由于有u
,这可能并不是那么重要,但仅使用l
或ll
很容易将其误认为是1
。 - JaakkoK