[cfe-dev] On sizeof char, bytes, and bits in the C99 standard

Sebastian Redl sebastian.redl at getdesigned.at
Fri Jan 2 07:50:59 PST 2009


Török Edwin wrote:
> Section 7.18.1.1 defines int8_t: "Thus, int8_t denotes a signed integer
> type with a width of exactly 8 bits."
>
> This quote from C99Rationale V.5.10 " Thus, for instance, on a machine
> with 36-bit words, a byte can be defined to consist of 9, 12, 18, or 36
> bits, these numbers being all the exact divisors of 36 which are not
> less than 8.)" shows that the intent was to allow for a definition of
> byte that doesn't necessarily have 8 bits.
>
> However according this quote " These strictures codify the widespread
> presumption that any object can be treated as an array of characters,
> the size of which is given by the sizeof operator with that object’s
> type as its
> operand."  I should be able to treat any objects (thus including int8_t
> type objects) as array of characters.
> This implies that there exists an N such that: number_of_bits(char)*N =
> number_of_bits(int8_t). Given what we know about char and int8_t this means:
> there exists an N such that number_of_bits(byte)*N = 8, which implies
> number_of_bits(byte) <= 8.
>
> Now according to C99Rationale V5.10: " All objects in C must be
> representable as a contiguous sequence of bytes, each of which  is at
> least 8 bits wide.",
> number_of_bits(byte) >= 8.
>
> Thus number_of_bits(byte) = 8.
>
> Am I right, or am I wrong?
>   

You're wrong. 7.8.1.1p3 says that the exact forms are optional. An
implementation where CHAR_BITS is > 8 cannot provide (u)int8_t, but is
nevertheless conforming.

Sebastian



More information about the cfe-dev mailing list