Search code examples
cstandardsc89

What hardware specific defines does the c89 standard require the implementation to provide?


Does the c(89) standard specify certain hardware properties that must be defined by the implementation? For example on my Linux system there is a define for __WORDSIZE (defined as 64) - can I expect __WORDSIZE to be defined on every system complying with c(89)? Are there other hardware specific values that the c standard requires an implementation to provide?


Solution

  • C89 specifies limits provided by limits.h, see here for the freely accessible draft text.

    As already commented by alk answered by alk, the only one that's truly hardware specific is CHAR_BIT, the others are implementation specific.

    As for __WORDSIZE, this isn't a standard define, and it's questionable what a word size should be.

    You can always determine the number of bits in a type using an ingenious macro found for example in this answer, quoting it here:

    /* Number of bits in inttype_MAX, or in any (1<<b)-1 where 0 <= b < 3E+10 */
    #define IMAX_BITS(m) ((m) /((m)%0x3fffffffL+1) /0x3fffffffL %0x3fffffffL *30 \
                      + (m)%0x3fffffffL /((m)%31+1)/31%31*5 + 4-12/((m)%31+3))
    

    With this, you can determine the size in bits of an unsigned int like this:

    IMAX_BITS((unsigned)-1)
    

    But is this really the word size? On x86_64, the result will be 32, while pointers are 64 bits.

    With C99 and later, you could instead use

    IMAX_BITS((uintptr_t)-1)
    

    But be aware that uintptr_t is only required to be able to hold a pointer -- it could be larger.