anon
anon

Reputation:

g++: warning: integer constant is too large for ‘long’ type

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:

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;
}

Upvotes: 7

Views: 7509

Answers (1)

Kirill V. Lyadvinsky
Kirill V. Lyadvinsky

Reputation: 99685

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;

Upvotes: 12

Related Questions