What is the difference between "long", "long long", "long int", and "long long int" in C++?
Solution 1
long
and long int
are identical. So are long long
and long long int
. In both cases, the int
is optional.
As to the difference between the two sets, the C++ standard mandates minimum ranges for each, and that long long
is at least as wide as long
.
The controlling parts of the standard (C++11, but this has been around for a long time) are, for one, 3.9.1 Fundamental types
, section 2 (a later section gives similar rules for the unsigned integral types):
There are five standard signed integer types : signed char, short int, int, long int, and long long int. In this list, each type provides at least as much storage as those preceding it in the list.
There's also a table 9 in 7.1.6.2 Simple type specifiers
, which shows the "mappings" of the specifiers to actual types (showing that the int
is optional), a section of which is shown below:
Specifier(s) Type
------------- -------------
long long int long long int
long long long long int
long int long int
long long int
Note the distinction there between the specifier and the type. The specifier is how you tell the compiler what the type is but you can use different specifiers to end up at the same type.
Hence long
on its own is neither a type nor a modifier as your question posits, it's simply a specifier for the long int
type. Ditto for long long
being a specifier for the long long int
type.
Although the C++ standard itself doesn't specify the minimum ranges of integral types, it does cite C99, in 1.2 Normative references
, as applying. Hence the minimal ranges as set out in C99 5.2.4.2.1 Sizes of integer types <limits.h>
are applicable.
In terms of long double
, that's actually a floating point value rather than an integer. Similarly to the integral types, it's required to have at least as much precision as a double
and to provide a superset of values over that type (meaning at least those values, not necessarily more values).
Solution 2
Long and long int are at least 32 bits.
long long and long long int are at least 64 bits. You must be using a c99 compiler or better.
long doubles are a bit odd. Look them up on Wikipedia for details.
Solution 3
long
is equivalent to long int
, just as short
is equivalent to short int
. A long int
is a signed integral type that is at least 32 bits, while a long long
or long long int
is a signed integral type is at least 64 bits.
This doesn't necessarily mean that a long long
is wider than a long
. Many platforms / ABIs use the LP64
model - where long
(and pointers) are 64 bits wide. Win64 uses the LLP64
, where long
is still 32 bits, and long long
(and pointers) are 64 bits wide.
There's a good summary of 64-bit data models here.
long double
doesn't guarantee much other than it will be at least as wide as a double
.
Solution 4
This looks confusing because you are taking long
as a datatype itself.
long
is nothing but just the shorthand for long int
when you are using it alone.
long
is a modifier, you can use it with double
also as long double
.
long
== long int
.
Both of them take 4 bytes.
Solution 5
While in Java a long
is always 64 bits, in C++ this depends on computer architecture and operating system. For example, a long
is 64 bits on Linux and 32 bits on Windows (this was done to keep backwards-compatability, allowing 32-bit programs to compile on 64-bit Windows without any changes).
It is considered good C++ style to avoid short int long ...
and instead use:
std::int8_t # exactly 8 bits
std::int16_t # exactly 16 bits
std::int32_t # exactly 32 bits
std::int64_t # exactly 64 bits
std::size_t # can hold all possible object sizes, used for indexing
These (int*_t
) can be used after including the <cstdint>
header. size_t
is in <stdlib.h>
.
Related videos on Youtube
1110101001
Updated on July 08, 2022Comments
-
1110101001 over 1 year
I am transitioning from Java to C++ and have some questions about the
long
data type. In Java, to hold an integer greater than 232, you would simply writelong x;
. However, in C++, it seems thatlong
is both a data type and a modifier.There seems to be several ways to use
long
:long x; long long x; long int x; long long int x;
Also, it seems there are things such as:
long double x;
and so on.
What is the difference between all of these various data types, and do they all have the same purpose?
-
Pete Becker about 10 years@user2612743 - to be safe, think about what your requirements are and use the appropriate type.
long long
might be slower thanlong
, which might be slower thanint
. -
C. M. almost 9 yearsNo, "to be safe, use long long" is the same as saying "to be safe, just give everyone one earth AIDS, so we don't have to worry about safe sex, we've all already got it anyhow!" Silly, no? Think about the data and what possible values it can have, and use the best-fitting type. This also helps the compiler make additional optimizations without breaking the original code intent, such as if it has to load additional libraries to handle numbers larger than the natural bit-width of the target platform.
-
-
Kal about 10 yearssame thing with
unsigned
andunsigned int
-
chris about 10 yearsI'm pretty sure
long
is at least 32 bits (2^31-1 on either side of zero) andlong long
is at least 64 (2^63-1 on either side). -
Eric Jablow about 10 yearsAnd
long double
is guaranteed to have at least the range ofdouble
, but it may be the same. It depends on the computer. Some FPUs have extended precision; the x87 chips had 32-bit single precision, 64-bit double precision, and 80-bit extended precision. -
C. M. almost 9 yearsThe "int" part simply differentiates between an integer type and floating point, character, or other non-integer type. In many cases, this can be inferred by the compiler, so it can be dropped--This is why "unsigned" is the same as "unsigned int", for example. The "int" part is simply assumed unless the programmer specifies something else, such as "char" or "double". As for the actual sizes used.. depending on which standard you read, each size may have a minimum number of bits, but they're all defined such that each is at least as large as the previous type.
-
C. M. almost 9 yearsThat last part, using a "long" to get 32-bits when int is 64-bits, is incorrect. If int is 64-bits, then long will be at least 64-bits, as well. Long is guaranteed to be at least as large as int, although it may be larger, but never smaller. Most compilers have various methods which allow the programmer to be more specific, such as a (non-portable) __int32 type that is exactly 32-bits, and so on.
-
thomiel about 8 yearsAs defined in the C standard, a
long
is guaranteed to be at least 32 bits. (Standards may change tough.) Current C++14 draft just says: @C.M. "Plain ints have the natural size suggested by the architecture of the execution environment the other signed integer types are provided to meet special needs" (section 3.9.1). I found no word about the length relations of various ints in it. __int32 isn't really part of the standard, but since C++11 there are typedefs available like int_fast32_t or int_least32_t available to get you exactly what you want. -
supercat over 7 yearsI'd say that on twentieth-century implementations for general-purpose reprogrammable microcomputers,
char
was almost unanimously 8 bits,short
was 16, andlong
was 32;int
could either be 16 or 32. Note for some platforms (esp. the 68000) both 16-bit and 32-bitint
were quite common, and indeed some compilers had options to support either. Code which needed to be portable was thus expected to useshort
orlong
in preference toint
. -
Prodigle over 4 yearsLong taking 4 bytes is only valid on Win64, it's platform dependent
-
Siraj Alam over 4 yearsYes obviously...not only long...int takes 4 bytes and 2 bytes depend on the platform, no doubt.
-
M.M about 4 yearsIt would improve the answer to include the values of the minimum ranges
-
zkoza over 2 yearsWhat does this answer add to the previous ones?
-
Abhishek Mane over 2 years@paxdiablo
int a=3,b=5; long int c;
nowc=a * long int(b);
give error primary expression before long butc=a * long(b);
works fine. As bothlong
andlong int
are specifiers then why this contrary things ? and of course if I include that like in brackets(long int )b
not give error so please put some light on this ? -
Abhishek Mane over 2 years@M.M
int a=3,b=5; long int c;
nowc=a * long int(b);
give error primary expression before long butc=a * long(b);
works fine. As bothlong
andlong int
are specifiers then why this contrary things ? and of course if I include that like in brackets(long int )b
not give error so please put some light on this ? -
paxdiablo over 2 years@AbhishekMane: I believe the problem with
long int(b)
may simply be thatlong
is being considered a separate thing from the rest of the cast,int(b)
. If you uselong(b)
or(long int)(b)
or even one oftypedef long int LI;
orusing LI = long int;
withLI(b)
, it works fine. I'm finding it hard to generate a lot of interest though, since C++ developers should probably be avoiding the old style casts as much as possible. By all means use(long int)b
if you're incorporating legacy C code but there's no<someType>(var)
in C :-)