What\'s the best way to declare an integer type which is always 4 byte on any platforms? I don\'t worry about certain device or old machines which has 16-bit int
You could hunt down a copy of Brian Gladman's brg_types.h
if you don't have stdint.h
.
brg_types.h
will discover the sizes of the various integers on your platform and will create typedefs for the common sizes: 8, 16, 32 and 64 bits.
C doesn't concern itself very much with exact sizes of integer types, C99 introduces the header stdint.h , which is probably your best bet. Include that and you can use e.g. int32_t
. Of course not all platforms might support that.
You need to include inttypes.h
instead of stdint.h
because stdint.h
is not available on some platforms such as Solaris, and inttypes.h
will include stdint.h
for you on systems such as Linux.
If you include inttypes.h
then your code is more portable between Linux and Solaris.
This link explains what I'm saying: HP link about inttypes.h
And this link has a table showing why you don't want to use long
or int
if you have an intention of a certain number of bits being present in your data type.
IBM link about portable data types