2

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?

Ankush
  • 1,061
  • 1
  • 13
  • 22
  • 4
    *"Does the c(89) standard specify certain hardware properties that must be defined by the implementation?"* No – Ry- Jul 10 '17 at 10:45
  • 4
    While the standard itself costs money, you can easily find the drafts available for free to read online. The last draft before ratification is usually identical to the then standard. – Some programmer dude Jul 10 '17 at 10:48
  • the complete list is indicated in the standard you cite, so the only identifiers that you can safely assume to appear are the ones that appear in the document. – Luis Colorado Jul 12 '17 at 09:10

2 Answers2

4

CHAR_BIT comes to my mind (see 5.2.4.2.1/1 of the C11 Standard draft).

It defines the width (number of bits) of the smallest integer that is not a bit field. On recent systems this typically is 8.

Still, whether this is necessarily to be taken as hardware property is arguable.

C tries to abstract hardware properties. That is probably a (main?) reason why it spread.

alk
  • 69,737
  • 10
  • 105
  • 255
  • 2
    Or in the [C89 standard draft](http://port70.net/~nsz/c/c89/c89-draft.html) under 2.2.4.2 Numerical limits. `__WORDSIZE` is compiler internal as said here: https://stackoverflow.com/a/30734004/8051589 – Andre Kampling Jul 10 '17 at 11:20
  • uv'ing, still a question: what would be the argument that `CHAR_BIT` does *not* relate to a hardware property? –  Jul 10 '17 at 11:26
  • @FelixPalmen: I could imagine a C implementation on a 9 bit hardware using 8 bits per "byte" only. Might make sense on a 8.5 bit hardware (each even byte is 8 bit, each odd byte is 9 bit)... ;-) – alk Jul 10 '17 at 11:43
4

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.