I was wondering if long long specifies a single datatype then why don't things like int int work? I meant obviously that's not a data type but there is a long data type. Essentially what I'm asking is:
int a = 0; //okay
long b = 0; //still fine
long long c = 0; //really long number but its okay....
int int d = 0; //error
why?
-
1That's because the language standard says so.gnasher729– gnasher7292016年05月05日 07:59:56 +00:00Commented May 5, 2016 at 7:59
2 Answers 2
The difference between long long
and int int
is that long
modifies a type, rather than being a type itself. long
is really a shorthand for long int
and long long
a shorthand for long long int
.
More specifically int
is a type specifier, just like char
or bool
. long
is a type modifier. Other type modifiers are unsigned
and signed
and short
.
If one of the modifiers is missing then the type will fall back to a default. E.g. if there is no signed
or unsigned
then the type will be signed. If there is no short
or long
the default size depends on the compiler and the architecture.
Take a look at this table on Wikipedia for a full list of how different type specifiers and modifiers can be combined.
Edit:
In current versions of the C and C++ standards long
and short
are actually type specifiers in their own right. This doesn't change the way that things can be combined though.
-
Note that originally, C defaulted types to int in many circumstances if they weren't specified. That "long" is shorthand for "long int" is a special case of that. There's also "short" -> "short int", "unsigned" -> "unsigned int" and the fact that in original C (before ANSI standardisation) function parameter and return types could be omitted and would default to int, too.Jules– Jules2016年05月05日 09:00:15 +00:00Commented May 5, 2016 at 9:00
-
4Technically, your notion about
long
being a modifier andint
a specifier is not correct. Both are listed as type specifiers in modern C and C++ standards; there is no such thing as type modifiers. The standards also list all possible combinations of type specifiers, includinglong long
,long long int
,signed long long
,signed long long int
(all four synonymous), but notint int
.ach– ach2016年05月05日 12:27:15 +00:00Commented May 5, 2016 at 12:27 -
Yep, you're right. I didn't know that. Looks like I'm harking back to the old days of C.
long
andint
are both instances of asimple-type-specifier
in the current C++ grammar.Will– Will2016年05月06日 06:37:45 +00:00Commented May 6, 2016 at 6:37 -
its very philosophically unfortunate that the modifier cannot be repeated (ex: long long long int = 128 bit type).Sidharth Ghoshal– Sidharth Ghoshal2024年12月12日 23:28:43 +00:00Commented Dec 12, 2024 at 23:28
You can specify a long integer that is longer than a long
as long long
.
But what would it mean to specify an integer that is more integer than int
?
More precisely. In C++ you have type int
, which is normally a 32-bit wide integer. You then have the modifiers short
and long
that can be used as illustrated by the following example:
#include <iostream>
using namespace std;
int main(int argc, char *argv[])
{
cout << "short int: " << sizeof(short int) << endl;
cout << "short: " << sizeof(short) << endl;
cout << "int: " << sizeof(int) << endl;
cout << "long int: " << sizeof(long int) << endl;
cout << "long: " << sizeof(long) << endl;
cout << "long long int: " << sizeof(long long int) << endl;
cout << "long long: " << sizeof(long long) << endl;
}
On my Linux box, running g++ 4.7.2
, this gives:
short int: 2
short: 2
int: 4
long int: 8
long: 8
long long int: 8
long long: 8
So:
int
is a 32-bit signed integershort int
(abbreviated,short
) is a 16-bit signed integerlong int
(abbreviated,long
) is a 64-bit signed integerlong long int
(abbreviated,long long
) is still a 64-bit signed integer on my architecture / compiler, but it is in the language to allow a longer integer type (e.g. 128 bit) on different architectures / compilers.
So, as Will has explained: in this context, long
is a modifier that can be applied twice whereas int
is not a modifier.