158

I found both long int long and int long long can compile for a variable type. Is there any difference between long int long, int long long , long long and long long int?

In general, is the type identical if it has the same number of long?

1 long:

long l;
int long il;
long int li;

2 long:

long long ll;
int long long ill;
long int long lil;
long long int lli;

Also if above is right, are the following declarations also identical?

long long* llp;
int long long* illp;
long int long* lilp;
long long int* llip;
6
  • 8
    Test it (albeit only on your compiler) using std::swap. This will not compile if the types are not identical.
    – Bathsheba
    Jun 9, 2015 at 8:15
  • 1
    Related: stackoverflow.com/questions/589575/… Jun 9, 2015 at 8:15
  • 26
    Voted to reopen. The alleged duplicate is related but it isn't this question. The answer over there does not answer this question. Close-voters: please do not vote to close on the basis of ignorance. Jun 9, 2015 at 8:51
  • 1
  • Compare the sizeof() each to be sure. A long long should be 64bits so sizeof(l) should be 8, if the sizeof(ill) == sizeof(lil) then your compiler sees them the same. This will be true for your current compiler and any future compilers that you may encounter (until the demise of sizeof() - which will hopefully be never or I've got a serious amount of rewriting to do). Jun 10, 2015 at 2:26

4 Answers 4

144

According to the C++ Standard (7.1.6.2 Simple type specifiers)

3 When multiple simple-type-specifiers are allowed, they can be freely intermixed with other decl-specifiers in any order.

So for example the following declaration is valid

long static long const int x = 10;

You may even use constexpr specifier along with const qualifier. For example

constexpr long static long const int x = 10;

By the way, we forgot about specifier signed! Let's add it for example before declarator x

constexpr long static long const int signed x = 10;

In C you may also use several type qualifiers in the same declare specifier sequence. According to the C Standard (6.7.3 Type qualifiers)

5 If the same qualifier appears more than once in the same specifier-qualifier-list, either directly or via one or more typedefs, the behavior is the same as if it appeared only once....

So for example in C the following declaration is also valid

const long const long static const int const signed x = 10;

So if you are paid according to the number of symbols typed in the program then I advise you to use such declarations. :)

15
116

Is the type identical...

Yes.

C++11 §7.1.6.2/3

When multiple simple-type-specifiers are allowed, they can be freely intermixed with other decl-specifiers in any order.

0
43

Yes, but please don't. Just as English and German have conventional word orders for adjectives and adverbs (e.g. time - manner - place), so do C and C++. Varying from the conventional order won't confuse the compiler, but it will confuse your fellow developers. I would suggest that the conventional order is roughly along the lines of

  1. static/extern (linkage)
  2. const/volatile (modification)
  3. signed/unsigned (signedness)
  4. short/long (length)
  5. Basic type (head noun)

although there's certainly some wiggle room.

31

Is “long long” = “long long int” = “long int long” = “int long long”?

All other answers here talked about the second part of your question. For the first part: Is “long long” = “long long int” ?, answer is yes.

C++11 7.1.6.2 Simple type specifiers (table 10)

Specifier(s)            Type
...                     ...
long long int           “long long int”
long long               “long long int”
long int                “long int”
long                    “long int”  
...                     ...  

For the second part of your question: Is “long int long” = “int long long”?, answer is yes again.

The type-specifiers may occur in any order and can be intermixed with the other declaration specifiers. Therefore, all of the following

long long  
long long int  
long int long  
int long long  

are valid and equivalent.

4
  • 3
    I am not getting why you don't improve @Cheers and hth. - Alf answer instead. Jun 9, 2015 at 10:37
  • 8
    @phresnel; Because all other answers covers only half. All addressed about the intermixing of format specifier missing the first half of the question in the title: Is “long long” = “long long int” = “long int long” = “int long long”?
    – haccks
    Jun 9, 2015 at 11:59
  • 5
    @phresnel Some people answer for the incentive of gaining rep. Editing someone else's answer to cover the other half an answer is a lot of work for effectively giving credit to someone else.
    – user820304
    Jun 10, 2015 at 15:14
  • 1
    True. At first this appeared as just a minor addition. (CC: @Thebluefish) Jun 10, 2015 at 15:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.