On what kind of a system can uint8_t
be legally defined to be a type other than unsigned char
?
In summary, uint8_t
can only be legally defined on systems where CHAR_BIT
is 8. It's an addressable unit with exactly 8 value bits and no padding bits.
In detail, CHAR_BIT
defines the width of the smallest addressable units, and uint8_t
can't have padding bits; it can only exist when the smallest addressable unit is exactly 8 bits wide. Providing CHAR_BIT
is 8, uint8_t
can be defined by a type definition for any 8-bit unsigned integer type that has no padding bits.
Here's what the C11 standard draft (n1570.pdf) says:
5.2.4.2.1 Sizes of integer types
1 The values given below shall be replaced by constant expressions suitable for use in #if
preprocessing directives. ... Their implementation-defined values shall be equal or
greater in magnitude (absolute value) to those shown, with the same sign.
-- number of bits for smallest object that is not a bit-field (byte)
CHAR_BIT 8
Thus the smallest objects must contain exactly CHAR_BIT bits.
6.5.3.4 The sizeof and _Alignof operators
...
4 When sizeof is applied to an operand that has type char, unsigned
char, or signed char, (or a qualified version thereof) the result is
1. ...
Thus, those are (some of) the smallest addressable units. Obviously int8_t
and uint8_t
may also be considered smallest addressable units, providing they exist.
7.20.1.1 Exact-width integer types
1 The typedef name intN_t designates a signed integer type with width
N, no padding bits, and a two’s complement representation. Thus,
int8_t denotes such a signed integer type with a width of exactly 8
bits.
2 The typedef name uintN_t designates an unsigned integer type with
width N and no padding bits. Thus, uint24_t denotes such an unsigned
integer type with a width of exactly 24 bits.
3 These types are optional. However, if an implementation provides
integer types with widths of 8, 16, 32, or 64 bits, no padding bits,
and (for the signed types) that have a two’s complement
representation, it shall define the corresponding typedef names.
The emphasis on "These types are optional" is mine. I hope this was helpful :)
char
with only 7 real bits and 1 padding bit. – Chapschar
s must have all of their representation bits participate in determining their value. – Alpenglowuint8_t
where 8 is real and 8 is padding. I'd shoot whoever made such an environment though. :) – Chapstypedef signed integer type int8_t; // optional
– Gangchar
. – Recruitmentuint8_t
shouldn't exist at all. – Pooi