r/cpp_questions • u/[deleted] • Aug 31 '25
OPEN What is long long
I saw some c++ code and I noticed it has long long, I never knew you could put 2 primitives next to each other. What does this do?
13
u/y53rw Aug 31 '25 edited Aug 31 '25
All of the fundamental types are here.
https://en.cppreference.com/w/cpp/language/types.html
Note that some types can be written multiple ways. long long can also be written long long int or signed long long int or signed long long.
7
u/Warshrimp Aug 31 '25
Wouldn't it be great if char = 8, short = 16, int = 32, long = 64 and long long were 128? I hope the next time someone treats a new data model they stick with this.
7
u/no-sig-available Aug 31 '25
Wouldn't it be great if
We have had systems with char = 9, short = 18, int = 36, and long long = 72. The standard didn't want to ban those.
https://stackoverflow.com/questions/6971886/exotic-architectures-the-standards-committees-care-about
You can add
static_assertfor you code, so it will fail to compile on such systems (because it would probably not work anyway). Or useint32_t, which has the same effect (fail to compile when it doesn't exist).1
u/DrShocker Sep 01 '25
Or if you don't want to fail to compile there's the fast or least versions that help the compiler pick what number to use in a way that respects the properties you need
19
u/y53rw Aug 31 '25
I would prefer just go with the naming scheme used by Rust and other modern systems languages. i8, u8, i16, u16, etc... And then isize/usize for the pointer sized integers.
7
5
u/KeretapiSongsang Aug 31 '25
There are multiple CPU and OS architectures. To each their own set of data width/length.
Fixed data length definition like that is usually available in trans piled/LLVM/JIT languages where the "compilers" or "interpreters" has the control of the data types.
2
u/BSModder Aug 31 '25 edited Aug 31 '25
long is usual 32.
I propose this naming scheme
short= 16,long= 32,short short= 8,long long= 64.This can be extended to cover all sizes, want a 128 bits type?
long long long. 4 bits type?short short short. 24 bits?short long. 21?short long short long short7
5
u/WildCard65 Aug 31 '25
Long has different sizes between Windows and Linux. Windows its 32 bits while 64bit Linux its 64 bits.
1
u/no-sig-available Aug 31 '25
Long has different sizes between Windows and Linux.
Yes, Windows is consistent, and has had 32-bit
longall the way since 16-bit Windows. :-)Linux found it a good idea to add
long long, and then makelongthe same size?3
u/Vazumongr Aug 31 '25
Man, I just prefer int8, int16, int32, int64 tbh. All the information regarding the type right in the name.
edit: uint8, uint16, uint32, and uint64 for unsigned ints too :>
5
u/BSModder Aug 31 '25
My comment was satire. I don't think any language should use it ever.
IntN system is probably the best for clarity.
2
1
u/SoerenNissen Aug 31 '25
Depending on whether the system models unsigned types, I either prefer
I8
I16etc./ (for a system that doesn't model unsigned)
or
S8/U8
S16/U16etc./ (for a system that models a difference between signed/unsigned)
3
u/SoerenNissen Aug 31 '25
I suggest a log scale centered on 32 bit.
sizeof(Im2) == 1
sizeof(Im1) == 2
sizeof(I) == 4with alias typesI0,Im0andIp0
sizeof(Ip1) == 8
sizeof(Ip2) == 161
u/DrShocker Aug 31 '25
In my opinion, char and uint8 shouldn't be considered synonyms since the kinds of operations you want to do with a byte or an 8 bit integer might reasonably be wrong if they get mix.
2
1
u/heyheyhey27 Sep 02 '25
C++ goes out of its way to support odd architectures, including ones that don't do 8 bits per byte and ones that only have 7-bit chars limiting their character set.
1
2
0
u/no-sig-available Aug 31 '25
Note that some types can be written multiple ways.
long longcan also be writtenlong long intorsigned long long intorsigned long long.Or, if you want to go all the way,
long signed int longalso works. :-)The rules just say that the words can be combined, but the order isn't specified. You can also add
const,volatile, ortypedefanywhere in the soup.1
u/tangerinelion Sep 01 '25
Sure,
class loong { static constexpr inline volatile long const signed int long x = 0; };. Beautiful.
7
u/DrShocker Aug 31 '25
You can see the list of integegral types here:
https://en.cppreference.com/w/cpp/language/types.html#Integral_types
Note: the exact meaning of some of them is platform dependent. So, generally it's suggested you use the ones defined with the width in their name since it's more clear and portable. (std::int64_t for example)
4
2
u/SmokeMuch7356 Aug 31 '25 edited Aug 31 '25
It designates an integer type that's at least as wide as a long but could be wider. For example, if someone builds an 80-bit architecture,1 an int could be 32 bits, long 64 bits, and long long 80 bits.
6.8.2 Fundamental types [basic.fundamental]
1 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 may also be implementation-defined extended signed integer types. The standard and extended signed integer types are collectively called signed integer types. The range of representable values for a signed integer type is −2N-1 to 2N−1 − 1 (inclusive), where N is called the width of the type.
Latest working draft of the C++ standard.
See also section 9.2.9.1 on type specifiers. The language grammar allows arbitrary combinations of type specifiers, but there are additional semantic rules that only allow certain combinations:
(2.1)
constcan be combined with any type specifier except itself.(2.2)
volatilecan be combined with any type specifier except itself.(2.3)
signedorunsignedcan be combined withchar,long,short, orint.(2.4)
shortorlongcan be combined withint.(2.5)
longcan be combined withdouble.(2.6)
longcan be combined withlong.
Powers of 2 are convenient but not necessary.
1
u/wigglytails Aug 31 '25
There should be longer long
1
u/dan-stromberg Aug 31 '25
I'm not sure if you mean that literally (like "longer long i;") or if you want a wider type in C++. But C++23 has one, and g++ and clang both have __int128.
1
Aug 31 '25
Its a type int but it has more bit thn int Example int has 16 bits but if you get a bigger value which can’t be dealt by int you can use long long or long which is 64 & 32 bits.
1
35
u/ddxAidan Aug 31 '25
“Long long” is a primitive itself, but its exact meaning could depend. It is at least 64 bits