I am aware that the specification of the C language does not dictate the exact size of each integer type (e.g., int
).
What I am wondering is: Is there a way in C (not C++) to define an integer type with a specific size that ensures it will be the same across different architectures? Like:
typedef int8 <an integer with 8 bits>
typedef int16 <an integer with 16 bits>
Or any other way that will allow other parts of the program to be compiled on different architecture.
What you want is <stdint.h>
, which compilers that conform to the C standard ("C99") will implement. Unfortunately, this does not include Microsoft. Fortunately, an open-source project provides a <stdint.h>
for Windows, see msinttypes.
This will allow you to use int32_t
and uint32_t
, plus 8, 16, and 64, and many others.
Note: the header file itself is not optional in the standard, however, most of the types in the header are individually optional. Some are not. The most commonly used types are the optional ones, but nothing stops you from using the required ones. The thing is, if an implementation provides the header at all, in practice they define all of the types.
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