Skip to content

Instantly share code, notes, and snippets.

@himanshub16
Last active June 7, 2018 03:22
Show Gist options
  • Save himanshub16/c2ded7d4cd9ec02072c7b5d74e7fd659 to your computer and use it in GitHub Desktop.
Save himanshub16/c2ded7d4cd9ec02072c7b5d74e7fd659 to your computer and use it in GitHub Desktop.
C++ Literal suffix behavior
#include <iostream>
using namespace std;
int main()
{
// Likely to generate a warning.
unsigned long long limit63bit = 18446744073709551615; // 2^64 - 1
// OK
unsigned long long limit56bit = 18446744073709551615u;
cout << limit63bit << " " << limit56bit << endl;
return 0;
}

Result on compilation:

  • With C++98
himanshu-dell ➜  /tmp g++ sample.cpp -Wall -std=c++98 && ./a.out 
sample.cpp:8:34: warning: integer constant is so large that it is unsigned
  unsigned long long limit63bit = 18446744073709551615; // 2^64 - 1
                                  ^~~~~~~~~~~~~~~~~~~~
sample.cpp:8:2: warning: this decimal constant is unsigned only in ISO C90
  unsigned long long limit63bit = 18446744073709551615; // 2^64 - 1
  ^~~~~~~~
18446744073709551615 18446744073709551615
  • With C++11
himanshu-dell ➜  /tmp g++ sample.cpp -Wall -std=c++11 && ./a.out 
sample.cpp:8:34: warning: integer constant is so large that it is unsigned
  unsigned long long limit63bit = 18446744073709551615; // 2^64 - 1
                                  ^~~~~~~~~~~~~~~~~~~~
18446744073709551615 18446744073709551615
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment