I have an open-source codebase that is written in both C and C++. I'm looking for an integer type that is guaranteed to be at least 64 bits wide, which can be reliably compiled on most OS X (Intel, 64-bit) and Linux boxes with open-source C and C++ compilers, without too much extra work on the end user's part. Windows and 32-bit client support are not important at this time.
I did some testing on OS X, and the latest GCC that ships with the developer tools does not support C+11 mode (and therefore does not seem to guarantee availability of long long
). Clang does not support this, either, though it supports long long
if C99 mode is enabled, after a certain version.
Is the general suggestion to use int64_t
in place of long long
, when portability is an important goal? Using the format specifiers seems painful.
Can I reliably cast an int64_t
to long long
(and likewise to the unsigned
equivalent with uint64_t
) to use it with existing functions and libraries that take long long
as parameters? (And back again, of course.)
In that frame of mind, if I ship code that requires Clang functionality not in GCC, is Clang going to replace GCC as the compiler of choice on Linux? Is that compiler something I can expect, for the most part, when offering source code to end users?
Basically, I'd like to ask for some advice from other developers who have used both types for portable C and C++ code, who might have some suggestions on what might be the better long-term way to go, given the above goal in mind.
In a 64-bit compile, int64_t is long int , not a long long int (obviously).
The __int64 type is synonymous with type long long .
A long on some systems is 32 bits (same as an integer), the int64_t is defined as a 64 bit integer on all systems (otherwise known as a long long). Portability may be affected using long, but using int64_t looks like it was created to increase portability.
long and long int are identical. So are long long and long long int . In both cases, the int is optional.
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
).
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
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With