Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Size of int in C on different architectures

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.

like image 839
NawaMan Avatar asked Oct 23 '09 06:10

NawaMan


1 Answers

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.

like image 101
DigitalRoss Avatar answered Sep 28 '22 14:09

DigitalRoss