Should I use long long or int64_t for portable code?
Is the general suggestion to use
int64_t
in place oflong long
, when portability is an important goal?
I'd be very surprised if a compiler offered int64_t
but not long long
.
If long long
is present, it must have at least 64 bits, so casting from (u)int64_t
to (unsigned) long long
is value-preserving.
If you need a type with exactly 64 bits, use (u)int64_t
, if you need at least 64 bits, (unsigned) long long
is perfectly fine, as would be (u)int_least64_t
.
The types long long
and unsigned long long
are standard C and standard C++ types each with at least 64 bits. All compilers I'm aware of provide these types, except possibly when in a -pedantic
mode but in this case int64_t
or uint64_t
won't be available with pre-C++ 2011 compilers, either. On all of the systems <stdint.h>
is available, too. That is, as far as I can tell it doesn't matter much how you spell the type. The main goal of <stdint.h>
is to provide the best match for a specific number of bits. If you need at least 64 bit but you also want to take advantage of the fasted implementation of such a type, you'd use int_least64_t
or uint_least64_t
from <stdint.h>
or <cstdint>
(in case of the latter, the names are defined in namespace std
).