Declaring fixed-size integer typedef in Standard C

前端 未结 5 1278
谎友^
谎友^ 2021-01-13 08:28

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:

5条回答
  •  爱一瞬间的悲伤
    2021-01-13 08:49

    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 
    
    #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
    

提交回复
热议问题