An int64_t should be 64 bits wide on any platform (hence the name), whereas a long can have different lengths on different platforms. In particular, sizeof(long) is often 4, ie. 32 bits.
a) Can you explain to me the difference between int64_t and long (long int)? In my understanding, both are 64 bit integers. Is there any reason to choose one over the other?
The former is a signed integer type with exactly 64 bits. The latter is a signed integer type with at least 32 bits.
b) I tried to look up the definition of int64_t on the web, without much success. Is there an authoritative source I need to consult for such questions?
c) For code using int64_t to compile, I am including <iostream>, which doesn't make much sense to me. Are there other includes that provide a declaration of int64_t?
It is declared in <cstdint> or <cinttypes> (under namespace std), or in <stdint.h> or <inttypes.h> (in the global namespace).
int64_t is guaranteed by the C99 standard to be exactly 64 bits wide on platforms that implement it, there's no such guarantee for a long which is at least 32 bits so it could be more.
§7.18.1.3 Exact-width integer types 1 The typedef name intN_t
designates a signed integer type with width N , no padding bits, and
a two’s complement representation. Thus, int8_t denotes a signed
integer type with a width of exactly 8 bits.
gcc -dM -E -x c /dev/null | grep __INT64
#define __INT64_C(c) c ## L
#define __INT64_MAX__ 0x7fffffffffffffffL
#define __INT64_TYPE__ long int
gcc -dM -E -x c++ /dev/null | grep __UINT64
#define __UINT64_C(c) c ## UL
#define __UINT64_MAX__ 0xffffffffffffffffUL
#define __UINT64_TYPE__ long unsigned int
Clang
clang -dM -E -x c++ /dev/null | grep INT64_TYPE
#define __INT64_TYPE__ long int
#define __UINT64_TYPE__ long unsigned int
Clang, GNU compilers: -dM dumps a list of macros. -E prints results to stdout instead of a file. -x c and -x c++ select the programming language when using a file without a filename extension, such as /dev/null
note: for swig user, on Linux x86_64 use -DSWIGWORDSIZE64
MacOS
On Catalina 10.15 IIRC
Clang
clang -dM -E -x c++ /dev/null | grep INT64_TYPE
#define __INT64_TYPE__ long long int
#define __UINT64_TYPE__ long long unsigned int
Clang: -dM dumps a list of macros. -E prints results to stdout instead of a file. -x c and -x c++ select the programming language when using a file without a filename extension, such as /dev/null
note: for swig user, on macOS x86_64 don't use -DSWIGWORDSIZE64
Visual Studio 2019
First
sizeof(long int) == 4 and sizeof(long long) == 8
in stdint.h we have:
#if _VCRT_COMPILER_PREPROCESSOR
typedef signed char int8_t;
typedef short int16_t;
typedef int int32_t;
typedef long long int64_t;
typedef unsigned char uint8_t;
typedef unsigned short uint16_t;
typedef unsigned int uint32_t;
typedef unsigned long long uint64_t;
note: for swig user, on windows x86_64 don't use -DSWIGWORDSIZE64