15

I know the difference between long and int But What is the difference between "long long" and "long int"

Syedsma
  • 1,183
  • 5
  • 17
  • 22
  • possible duplicate of [What is the difference between an int and a long in C++?](http://stackoverflow.com/questions/271076/what-is-the-difference-between-an-int-and-a-long-in-c) – Martin York Aug 11 '11 at 14:01
  • @Martin: not quite a duplicate. This questions is related to `long int` syntax versus `long`. – André Caron Aug 11 '11 at 14:03
  • @Andr: Close enough that the answers to that question also answer this question. There is no need to duplicate answers – Martin York Aug 11 '11 at 14:05

8 Answers8

30

There are several shorthands for built-in types.

  • short is (signed) short int
  • long is (signed) long int
  • long long is (signed) long long int.

On many systems, short is 16-bit, long is 32-bit and long long is 64-bit. However, keep in mind that the standard only requires

sizeof(char) == 1
sizeof(char) <= sizeof(short) <= sizeof(int) <= sizeof(long) <= sizeof(long long)

And a consequence of this is that on an exotic system, sizeof(long long) == 1 is possible.

André Caron
  • 44,541
  • 12
  • 67
  • 125
  • 1
    You have a couple of more guarantees from the standard: sizeof(long long) * CHAR_BITS >= 64 while sizeof(long) * CHAR_BITS >= 32 – Martin York Aug 11 '11 at 14:03
  • I don't have a copy of the standard handy, just looked it up in [The C++ Programming Language](http://www.amazon.com/Programming-Language-3rd-Bjarne-Stroustrup/dp/0201889544). – André Caron Aug 11 '11 at 14:05
  • @LokiAstari: I think those guarantees basically just stem from the Pigeonhole Principle, right? For any possible value of `long`, there must be some sequence of `sizeof(long)` values of type `unsigned char` values which represents it; the (1<<(CHAR_BITS*sizeof(long))) possible character sequences must be able to identify at least `(1LL<<32)` such values. – supercat Oct 08 '14 at 21:00
10

According to the C standard the integral types are defined to provide at least the following ranges:

int                     -32767 to               +32767 representable in 16 bits
long               -2147483647 to          +2147483647 representable in 32 bits
long long -9223372036854775807 to +9223372036854775807 representable in 64 bits

Each can be represented as to support a wider range. On common 32 bit systems int and long have the same 32 bit representation.

Note that negative bounds are symmetric to their positive counterparts to allow for sign and magnitude representations: the C language standard does not impose two's complement.

Nicola Musatti
  • 17,834
  • 2
  • 46
  • 55
  • @BaneOfSerenity These values were quoted from an old copy of the C language standard, as mentioned in my answer and not meant to describe how integral types are commonly represented. Unless these were changed in the latest standard, which I don't have access to, your correction is *wrong* – Nicola Musatti Apr 09 '14 at 21:14
  • What C standard document are you referring to that makes these range guarantees? – nobody Apr 09 '14 at 21:38
  • 1
    They are specified in ISO/IEC 9989:1999, paragraph 5.2.4.2.1. "Their implementation defined values shall be equal or greater in magnitude (absolute value) to those shown, with the same sign." – Nicola Musatti Apr 09 '14 at 21:57
4

long long may be a bigger type than long int. For example on x86 32 bit long long would be a 64-bit type rather than 32 bit for long int.

Mark B
  • 95,107
  • 10
  • 109
  • 188
  • This means in 32Bit machines if we need to store the data type of 64bits len , then we can use long long right ? – Syedsma Aug 11 '11 at 14:06
  • 1
    @Syedsma: Yes. long long is guaranteed to be at least 64 bits. – Martin York Aug 11 '11 at 14:09
  • 2
    Careful: `long long` is not part of the C++ standard! It *is* part of the C99 standard, and also part of C++0x though, so many C++ compilers support it. – Fozi Aug 11 '11 at 14:14
4

On 64 bit systems it doesn't make any difference in their sizes. On 32 bit systems long long is guaranteed store values of 64 bit range.

Just to avoid all these confusions, it is always better to use the standard integral types: (u)int16_t, (u)int32_t and (u)int64_t available via stdint.h which provides transparency.

Arun
  • 1,399
  • 12
  • 21
  • Alas, types like `uint32_t` don't necessarily help things, since compilers are free to use a larger `int` type and promote `uint32_t` to it. Thus, (uint32_t)0xFFFFFFFF * (uint32_t)0xFFFFFFFF may yield 1, or -8589934591, or 18446744065119617025 (or, for that matter, anything whatsoever since it could yield Undefined Behavior). – supercat Oct 08 '14 at 21:07
2

An int on 16 bit systems was 16 bits. A "long" was introduced as a 32 bit integer, but on 32 bit systems long and int mean the same thing (both are 32 bit.) So on 32 and 64 bit systems, long long and long int are both 64 bit. The exception is 64 bit UNIX where long is 64 bits.

See the integer Wikipedia article for a more detailed table.

richardolsson
  • 4,041
  • 1
  • 17
  • 19
  • There is no guarantee that on 64 bit systems that int is bigger than 32. In fact on Windows it can be either depending on how the OS was compiled. ie is the OS compiled as 32/64 bit for the 64 bit platform? – Martin York Aug 11 '11 at 14:06
  • @Martin: By "system" I meant the entire configuration including operating system, which I realize now was not clear. – richardolsson Aug 11 '11 at 14:13
  • 1
    You might as well say the exception is Windows. Systems with a 32-bit int and 64-bit long and pointer are called "LP64". Systems with a 32-bit int and long and 64-bit pointer are called "LLP64" (because Long Long and Pointer are 64 bits). AFAIK all the major unix-like systems use LP64, and Windows is the only major system using LLP64 on 64-bit machines. I don't know of any 32-bit system on which `long int` is the same size as `long long`. `long int` is synonymous with `long`, always. – Steve Jessop Aug 11 '11 at 14:13
2

long int is a synonym for long. long long int is a synonym for long long.

The only guarantee you have in standard C++ is that long long is at least as large as long but can be longer. This is specified in §3.9.1.2 in the most recent publicly available draft of the standard n3242.

Scooter
  • 6,802
  • 8
  • 41
  • 64
pmr
  • 58,701
  • 10
  • 113
  • 156
1

The C standard doesn't make any specific width requirements for integral types other than minimal ranges of values that the type needs to be able to represent, and that the widths are non-decreasing: short <= int <= long int <= long long int (similarly for the unsigned types). long long only became part of the standard in C99 and C++0x, by the way. The minimum required ranges can be found in this Wikipedia article.

Kerrek SB
  • 464,522
  • 92
  • 875
  • 1,084
-1

I think:

"long" doubles the number of bits allocated to the data type. So long (32 bits?) becomes 64 bits. Int (16 bits?) becomes 32 bits.

cs94njw
  • 535
  • 5
  • 12