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;
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
ggrr
  • 7,737
  • 5
  • 31
  • 53
  • 8
    Test it (albeit only on your compiler) using `std::swap`. This will not compile if the types are not identical. – Bathsheba Jun 09 '15 at 08:15
  • 1
    Related: http://stackoverflow.com/questions/589575/what-does-the-c-standard-state-the-size-of-int-long-type-to-be – TobiMcNamobi Jun 09 '15 at 08: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. – Cheers and hth. - Alf Jun 09 '15 at 08:51
  • 1
    Also see [Where in a declaration may a storage class specifier be placed?](http://stackoverflow.com/q/13073952/1708801) – Shafik Yaghmour Jun 09 '15 at 15:25
  • 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). – Michael Stimson Jun 10 '15 at 02:26
  • @MichaelStimson `sizeof` is not a function, but an operator. That means it is written without the parantheses. – Kami Kaze Nov 06 '19 at 12:27

4 Answers4

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

Jean-François Corbett
  • 37,420
  • 30
  • 139
  • 188
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
  • 104
    When you absolutely, positively have to be sure the value will never change. – Bob Jun 09 '15 at 09:18
  • 1
    @Bob If you define a constant then it is supposed that the value will not be changed.:) – Vlad from Moscow Jun 09 '15 at 09:20
  • 2
    If you're paid according to the number of symbols typed, then overly long comments work better than adding redundant keywords. :) –  Jun 09 '15 at 10:18
  • 3
    @hvd Unfortunately usually comments are excluded from such a calculation. – Vlad from Moscow Jun 09 '15 at 10:20
  • 9
    Even if written like `#define REM(...)`, and then for an example of a huge word count, `i++; REM(Here, we increment i to make sure the new value of i is one more than the old value of i. This is safe because we know the value of i is less than the maximum value of i's type.)`? –  Jun 09 '15 at 10:22
  • 5
    @Bob sounds like "[The primary purpose of the DATA statement is to give names to constants; instead of referring to pi as 3.141592653589793 ... This also simplifies modifying the program, should the value of pi change.](http://www.heuse.com/cphumor.htm) – chux - Reinstate Monica Jun 09 '15 at 18:09
  • 17
    You don't even have to intermix the qualifiers... `const const const const const const const const const const const const const const const const const const const const const const const const const const const const const const const const const const const const const const const const const const answer = 42;` compiles just fine in C :-) – Lucas Trzesniewski Jun 09 '15 at 22:29
  • @Lucas Trzesniewski I used examples that demonstrate the first quote from the C++ Standard.:) – Vlad from Moscow Jun 10 '15 at 07:17
  • @Marius Bancila I did not add it because in the question there was said about long long int. Instead of unsigned I could add signed.:) – Vlad from Moscow Jun 10 '15 at 10:55
  • 2
    @hvd: *error: unterminated argument list invoking macro "REM"*. Be careful with ' in such fake comments. – Emil Jeřábek Jun 10 '15 at 12:15
  • @EmilJeřábek Ha, I didn't think anyone would actually try it. I certainly didn't. :) Strictly speaking, there is no unterminated argument list, but the behaviour is undefined at preprocessing time, so the preprocessor complaining about an unterminated argument list is a valid result of that. –  Jun 10 '15 at 12:36
  • 1
    Not related to the answer\question but to a user with this high reputation and knowledge shown in his answer, then go to his profile just to see he is unemployed is like taking an arrow in the heart. All the best in finding a job and thanks for your answer!. +1 – Tony Tannous Oct 17 '17 at 21:53
  • @TonyTannous Thanks. You are a kind person. When I mention that I have such a reputation at So nobody in Russia wants to suggest me a job.:) – Vlad from Moscow Oct 17 '17 at 21:55
  • In this case I'd consider moving to Western Europe. All the best and good luck in your search. – Tony Tannous Oct 17 '17 at 22:51
  • @user743382 `REM(Here, we increment i to make sure the new value of i is one more than the old value of i. This is safe because we know the value of i is less than the maximum value of i's type.)` wouldn't work. The apostrophe would cause the compiler to complain that it either didn't find the end single quote or the ending macro parenthesis wasn't found. – Adrian Nov 02 '18 at 14:54
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.

Drew Dormann
  • 59,987
  • 13
  • 123
  • 180
Cheers and hth. - Alf
  • 142,714
  • 15
  • 209
  • 331
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.

hobbs
  • 223,387
  • 19
  • 210
  • 288
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.

Community
  • 1
  • 1
haccks
  • 104,019
  • 25
  • 176
  • 264
  • 3
    I am not getting why you don't improve @Cheers and hth. - Alf answer instead. – Sebastian Mach Jun 09 '15 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 09 '15 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. –  Jun 10 '15 at 15:14
  • 1
    True. At first this appeared as just a minor addition. (CC: @Thebluefish) – Sebastian Mach Jun 10 '15 at 15:19