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 write long x;
. However, in C++, it seems that long
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?
long long
might be slower than long
, which might be slower than int
.
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).
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.
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
.
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.
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). long int
is a synonym for long
.
Later on, long long
was introduced to mean "long (64 bits) on Windows for real this time". long long int
is a synonym for this.
It is considered good C++ style to avoid short
, int
, long
etc. 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
You can use these int*_t
types by including the <cstdint>
header. size_t
is in <stdlib.h>
.
Historically, in early C times, when processors had 8 or 16 bit wordlength,int
was identical to todays short
(16 bit). In a certain sense, int is a more abstract data type thanchar
,short
,long
orlong long
, as you cannot be sure about the bitwidth.
When definingint n;
you could translate this with "give me the best compromise of bitwidth and speed on this machine for n". Maybe in the future you should expect compilers to translateint
to be 64 bit. So when you want your variable to have 32 bits and not more, better use an explicitlong
as data type.
[Edit: #include <stdint.h>
seems to be the proper way to ensure bitwidths using the int##_t types, though it's not yet part of the standard.]
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.
char
was almost unanimously 8 bits, short
was 16, and long
was 32; int
could either be 16 or 32. Note for some platforms (esp. the 68000) both 16-bit and 32-bit int
were quite common, and indeed some compilers had options to support either. Code which needed to be portable was thus expected to use short
or long
in preference to int
.
There is no deffirence, (long long x ) is equivalent to (long long int x ), but the second confirms that variable x is integer
Success story sharing
unsigned
andunsigned int
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).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.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 :-)