Declaring fixed-size integer typedef in Standard C
c, iso, portability, standards
Solution
Yes you can.
The header file `limits.h` should be part of C90. Then I would test through preprocessor directives values of `SHRT_MAX`, `INT_MAX`, `LONG_MAX`, and `LLONG_MAX` and set typedefs accordingly.
Example:
#include <limits.h>
#if SHRT_MAX == 2147483647
typedef unsigned short int uint32_t;
#elif INT_MAX == 2147483647
typedef unsigned int uint32_t;
#elif LONG_MAX == 2147483647
typedef unsigned long uint32_t ;
#elif LLONG_MAX == 2147483647
typedef unsigned long long uint32_t;
#else
#error "Cannot find 32bit integer."
#endif
Problem
Is there a reliable way to declare typedefs for integer types of fixed 8,16,32, and 64 bit length in ISO Standard C? When I say ISO Standard C, I mean that strictly: - ISO C89/C90, not C99. - No headers not defined in the ISO standard. - No preprocessor symbols not defined in the ISO standard. - No type-size assumptions not specified in the ISO standard. - No proprietary vendor symbols. I see other questions similar to this in StackOverflow, but no answers yet that do not violate one of the above constraints. I'm not sure it's possible without resorting to platform symbols.