I am trying to write cross-platform code in C++ for Windows(MinGW) and Linux(g++). I was used to define 64 bit integers in Linux as "long", but when I moved to MinGW, the sizeof(long) returned 4 bytes. Then I discovered that I can use "long long" or "__INT64" to define 64 bit integers in MinGW. I have two questions:
1.-What is the most portable way to define 64 bit integers for both Windows and Linux? I am currently using #ifdef, but I don't know if this is the best way to do it:
#ifdef LINUX
#define INT64 long
#elif WIN32
#define INT64 long long
#endif
2.-Should I use "long long" or "__INT64" in MinGW? and why?
You could use the type int64_t
, which is defined in the header cstdint
. This is standard as of C++11.
Note that this type might not exist if the platform you're using does not support 64 bit integers.
As for long long
, that is another possibility. long long
s are at least 64-bits wide. Note that it is standard as of C++11 as well, even though it will work on several compilers when using C++03.
As mentioned by Pete Becker, you could use int_least64_t
. This is a good choice if you don't mind using exactly 64 bit integers, but some integral type that is at least 64 bits wide.