3

Does C standard mandates it? Is there a platform where the number of bits in a byte is not equal to the number of bits in a type char?

jfs
  • 399,953
  • 195
  • 994
  • 1,670
  • These might be of interest as well: http://stackoverflow.com/questions/2098149/what-platforms-have-something-other-than-8-bit-char and http://stackoverflow.com/questions/5516044/system-where-1-byte-8-bit (neither answers the question completely). – cdarke Mar 29 '16 at 16:41
  • 2
    Reopened. This question is different to the 8 bits in a byte duplicate: it's not about something being equal to 8 bits but rather it is about something having the same number of bits as something else. – Bathsheba Mar 29 '16 at 16:44
  • @cdarke: to be clear, this question is not about what `CHAR_BIT` value is. It is about whether you need *all* bits in a byte to represent `char` values (no padding bits). – jfs Mar 29 '16 at 16:55
  • Yes, I get that. You also asked if there is a platform where the number of bits in a byte is not equal to a char, and the links I gave show several discussions and examples of such platforms. I'm not claiming your question is a duplicate. – cdarke Mar 29 '16 at 19:26
  • @cdarke: I see `CHAR_BIT != 8` platforms in your links but I do not see platforms where the number of bits is not equal (I don't see platforms where the width of a `char` is less than `CHAR_BIT` and my answer implies that no such platform may exist unless it violates the C standard). Could you post such platforms as an answer (if you see them)? – jfs Mar 29 '16 at 20:03

1 Answers1

4

Yes. Both are equal to CHAR_BIT*.

C standard defines CHAR_BIT as: "number of bits for smallest object that is not a bit-field (byte)". c99 says explicitly: "A byte contains CHAR_BIT bits."

"UCHAR_MAX shall equal 2CHAR_BIT - 1" — it means unsigned char requires at least CHAR_BIT bits (char_bits >= CHAR_BIT).

sizeof(char) == 1 (single-byte character fits in a byte) i.e., type char requires at most CHAR_BIT bits (char_bits <= CHAR_BIT).

From char_bits >= CHAR_BIT and char_bits <= CHAR_BIT follows that char_bits == CHAR_BIT (no padding bits).

POSIX says it explicitly: "CHAR_BIT Number of bits in a type char."


*: If char is signed and CHAR_BIT > 8 then (without the $6.2.6.2 quote below) it was not clear whether SCHAR_MIN..SCHAR_MAX range covers all CHAR_BIT bits. Though the name CHAR_BIT communicates the intent clearly ("number of bits in char").

c11 says ($6.2.6.2 in n1570 draft): "signed char shall not have any padding bits. There shall be exactly one sign bit."

From $6.2.5.15:

The implementation shall define char to have the same range, representation, and behavior as either signed char or unsigned char

it follow: All CHAR_BIT bits are used to represent CHAR_MIN..CHAR_MAX range (because both signed and unsigned char type use all bits).

For comparison, unlike char; _Bool may use less bits $6.7.2.1.4(122):

While the number of bits in a _Bool object is at least CHAR_BIT, the width (number of sign and value bits) of a _Bool may be just 1 bit.

jfs
  • 399,953
  • 195
  • 994
  • 1,670
  • 1
    Sadly I can't add a new answer, as the question is closed, but buried in the tech overview for the C55x: http://www.ti.com/lit/ug/spru393/spru393.pdf you can see that the system is configurable for byte sizes from 8-48 bits (some 54x series don't offer 8-bit, but I couldn't find public docs). The "new" c99 compliant tools make `CHAR_BIT == 8` and `sizeof char == 1` because that's what the standard says (well, `sizeof char` was always 1 by definition), but the byte size in the CPU is still not 8 bits in most cases, and this does affect the memory you allocate and the code you write. – Nick Bastin Mar 29 '16 at 16:41
  • @NickBastin: you can add answer now. – jfs Mar 29 '16 at 16:46
  • 1
    @NickBastin: ISO C permits any value >=8 for `CHAR_BIT`. POSIX requires `CHAR_BIT == 8`. Both require `sizeof (char) == 1`. – Keith Thompson Mar 29 '16 at 17:13