ChatGPT解决这个技术问题 Extra ChatGPT

What is the difference between "long", "long long", "long int", and "long long int" in C++?

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?

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

p
paxdiablo

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


same thing with unsigned and unsigned int
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).
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.
As for the range requirements, the C++11 standard references the C11 standard, which has actual ranges.
@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 <someType>(var) in C :-)
M
MBT

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.


B
Brett Hale

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.


S
Siraj Alam

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.


Long taking 4 bytes is only valid on Win64, it's platform dependent
Yes obviously...not only long...int takes 4 bytes and 2 bytes depend on the platform, no doubt.
x
xjcl

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


t
thomiel

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


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.
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.
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.
M
Mohanad Talat

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


What does this answer add to the previous ones?
This answer is short and straight to the reason as the answer need