the_pwner224
the_pwner224

Reputation: 99

Size of long in C++

After searching on the Internet, I found out (correct me if I am wrong) that on Windows compilers, both ints and longs are 32 bits long, but on most Linux compilers ints are 32 bit and longs are 64 bit.

If I use GCC on Linux to make a Windows executable, when I run it on Windows will the longs still be 64 bits long or will they only be 32 bits large? If not, how can I force a long to be 64 bits long on all architectures/OSs/compilers?

Also, if I use 32 bit GCC or compile for a 32 bit architecture, will longs still be 64 bits or will they become 32 bits?

Upvotes: 0

Views: 266

Answers (1)

John Saxton
John Saxton

Reputation: 540

C++11 defined fixed width integers, which you can use by including <cstdint>. Instead of using an int or a long, you can use an int32_t, for example. Similar functionality exists in C99's <stdint.h>.

If those aren't an option, you can create your own header by doing something like.

#ifdef PLATFORM1
typedef int myint64
...
#endif
#ifdef PLATFORM2
typedef long myint64
...
#endif

Then in Platform 1's makefile, pass in -DPLATFORM1, and in Platform 2's makefile, pass in -DPLATFORM2

Upvotes: 3

Related Questions