I'm writing code which will compile on multiple Windows and multiple *nix
platforms. Some will have 64-bit integers, I believe.
What is my best option to define platform-independent types such as UINT32
or UINT64? I'm not sure how to sort out the various platforms and integer
sizes, and some of it may even vary based on compiler options.
Start by including <limits.h>. If you want for example a 32 bit
unsigned type, check whether UINT_MAX is 0xffffffff, if not then check
whether ULONG_MAX is 0xffffff, otherwise #error (sort it out when you
run into the situation).
When running on a 64 bit system, you sometimes want 64 bit values
instead of 32 bit, and sometimes you don't. A variable that counts the
number of open windows in an application, or the number of children in
a family, doesn't have to be 64 bit. Variables counting the sizes of
things, or number of items, you will likely want to change in size on
a 64 bit system. I'd say checking whether SIZE_MAX == 2^64 - 1 is a
good indication. So you want some type that _does_ change on a 64 bit
system and needs to be named appropriately.
One hint: If you define such a type, give it a different type on a 32
bit vs. 64 bit system. For example, don't use long in both cases, but
use int for 32 bit (if it is large enough) and long for 64 bit (if it
is large enough). That way you can find mistakes where the wrong type
is used, because either on a 32 bit system or a 64 bit system the
compiler will produce an error.