290

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?

Jamal
  • 763
  • 7
  • 22
  • 32
1110101001
  • 4,662
  • 7
  • 26
  • 48
  • 1
    http://stackoverflow.com/q/6462439/139010 – Matt Ball Sep 24 '13 at 01:53
  • 2
    @user2612743 - to be safe, think about what your requirements are and use the appropriate type. `long long` might be slower than `long`, which might be slower than `int`. – Pete Becker Sep 24 '13 at 13:51
  • 1
    http://msdn.microsoft.com/en-us/library/s3f49ktz.aspx – 1110101001 Nov 13 '14 at 23:58
  • 5
    No, "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. – C. M. Jan 27 '15 at 23:52
  • Using long long with win32 listboxes and the like is wont to play havoc with your memory and other variables- even if the limits aren't breached. Even with the innocuous looking C4244 warnings it's not easy to detect. – Laurie Stearn Feb 26 '16 at 09:56

7 Answers7

253

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

paxdiablo
  • 854,327
  • 234
  • 1,573
  • 1,953
  • 9
    same thing with `unsigned` and `unsigned int` – Kal Sep 24 '13 at 01:54
  • 6
    I'm pretty sure `long` is at least 32 bits (2^31-1 on either side of zero) and `long long` is at least 64 (2^63-1 on either side). – chris Sep 24 '13 at 01:55
  • 2
    And `long double` is guaranteed to have at least the range of `double`, 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. – Eric Jablow Sep 24 '13 at 01:56
  • As for the range requirements, the C++11 standard references the C11 standard, which has actual ranges. – chris Sep 24 '13 at 02:05
  • The "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. Jan 27 '15 at 23:37
  • It would improve the answer to include the values of the minimum ranges – M.M Sep 08 '19 at 23:50
  • @paxdiablo `int a=3,b=5; long int c;` now `c=a * long int(b);` give error __primary expression before long__ but `c=a * long(b);` works fine. As both `long` and `long 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 Apr 14 '21 at 06:44
  • @M.M `int a=3,b=5; long int c;` now `c=a * long int(b);` give __error primary expression before long__ but `c=a * long(b);` works fine. As both `long` and `long 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 Apr 14 '21 at 06:49
  • 1
    @AbhishekMane: I believe the problem with `long int(b)` may simply be that `long` is being considered a separate thing from the rest of the cast, `int(b)`. If you use `long(b)` or `(long int)(b)` or even one of `typedef long int LI;` or `using LI = long int;` with `LI(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 `(var)` in C :-) – paxdiablo Apr 14 '21 at 11:33
78

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.

MBT
  • 21,733
  • 19
  • 84
  • 102
Eric Lippert
  • 647,829
  • 179
  • 1,238
  • 2,067
31

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.

Brett Hale
  • 21,653
  • 2
  • 61
  • 90
  • 2
    Is there any platform where all 4 of the integer types: short, int, long and long long have different size? – Sapien2 Mar 13 '23 at 13:54
14

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

xjcl
  • 12,848
  • 6
  • 67
  • 89
12

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.

Siraj Alam
  • 9,217
  • 9
  • 53
  • 65
3

Historically, in early C times, when processors had 8 or 16 bit wordlength,intwas identical to todays short(16 bit). In a certain sense, int is a more abstract data type thanchar,short,longorlong 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 translateintto be 64 bit. So when you want your variable to have 32 bits and not more, better use an explicitlongas 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.]

thomiel
  • 2,467
  • 22
  • 37
  • 2
    That 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. – C. M. Jan 27 '15 at 23:43
  • 2
    As 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. – thomiel Aug 19 '15 at 17:06
  • 1
    I'd say that on twentieth-century implementations for general-purpose reprogrammable microcomputers, `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`. – supercat Jun 23 '16 at 14:30
-1

There is no deffirence, (long long x ) is equivalent to (long long int x ), but the second confirms that variable x is integer