long long in C/C++

C++TypesLong Integer

C++ Problem Overview


I am trying this code on GNU's C++ compiler and am unable to understand its behaviour:

#include <stdio.h>;

int main()
{
    int  num1 = 1000000000;
    long num2 = 1000000000;
    long long num3;
    //num3 = 100000000000;
    long long num4 = ~0;

    printf("%u %u %u", sizeof(num1), sizeof(num2), sizeof(num3));
    printf("%d %ld %lld %llu", num1, num2, num3, num4);
    return 0;
}

When I uncomment the commented line, the code doesn't compile and is giving an error:

> error: integer constant is too large for long type

But, if the code is compiled as it is and is executed, it produces values much larger than 10000000000.

Why?

C++ Solutions


Solution 1 - C++

The letters 100000000000 make up a literal integer constant, but the value is too large for the type int. You need to use a suffix to change the type of the literal, i.e.

long long num3 = 100000000000LL;

The suffix LL makes the literal into type long long. C is not "smart" enough to conclude this from the type on the left, the type is a property of the literal itself, not the context in which it is being used.

Solution 2 - C++

Try:

num3 = 100000000000LL;

And BTW, in C++ this is a compiler extension, the standard does not define long long, thats part of C99.

Solution 3 - C++

It depends in what mode you are compiling. long long is not part of the C++ standard but only (usually) supported as extension. This affects the type of literals. Decimal integer literals without any suffix are always of type int if int is big enough to represent the number, long otherwise. If the number is even too big for long the result is implementation-defined (probably just a number of type long int that has been truncated for backward compatibility). In this case you have to explicitly use the LL suffix to enable the long long extension (on most compilers).

The next C++ version will officially support long long in a way that you won't need any suffix unless you explicitly want the force the literal's type to be at least long long. If the number cannot be represented in long the compiler will automatically try to use long long even without LL suffix. I believe this is the behaviour of C99 as well.

Solution 4 - C++

your code compiles here fine (even with that line uncommented. had to change it to

num3 = 100000000000000000000;

to start getting the warning.

Solution 5 - C++

Note:

  1. remove semicolon after the header file
  2. use %lu instead of just %u
  3. long long num3 = 100000000000LL;

Replace these things and you are good to go :)

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionsud03rView Question on Stackoverflow
Solution 1 - C++unwindView Answer on Stackoverflow
Solution 2 - C++Arkaitz JimenezView Answer on Stackoverflow
Solution 3 - C++sellibitzeView Answer on Stackoverflow
Solution 4 - C++Omry YadanView Answer on Stackoverflow
Solution 5 - C++charlieView Answer on Stackoverflow