tags:

views:

319

answers:

1

What can I do (programmatically) to get rid of the warning?

 ...
 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

but when I run the program everything is fine as expected:

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

used environment:

  • Ubuntu-Karmic 32bit
  • g++ version: v4.4.1

EDIT: here is the complete and compilable 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;
}
+5  A: 

According to C++ Standard 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.

New C++ Standard allows to use ull suffix. g++ 4.4.1 supports this suffix as well as Visual C++ 2008. You could use it as follows:

unsigned long long v=0xffffeeeeddddccccULL;
Kirill V. Lyadvinsky
I have never seen this 'ull' thing before. Could you give me a reference?
`ull` is a part of new C++ Standard. g++ 4.4.1 supports this suffix.
Kirill V. Lyadvinsky
I prefer to write the `ULL` and other suffixes in capital letters so that they are visually distinct. With the `u` there this is not perhaps that big of a concern but with just `l` or `ll` it's way too easy to mistake it for `1`.
jk
@jk, good point.
Kirill V. Lyadvinsky