Many C programmers seems to assume that specifying
sizeof(char)
in certain places increase portability.
Common occurences of that expression are found with
malloc
, realloc
,
fread
and fwrite
in particular.
Afterall, char
could be more than 8-bits.
This is simply wrong. It is.
Certainly char
can theoretically
occupy more than 8-bits.
However, the C standard clearly states that sizeof(x)
is the quantity of char
s required
to represent its parameter x
in memory,
not the eighth of the quantity of bits.
Hence by definition, sizeof(char)
is always equal to exactly one (1
),
not only in practice, but also in the most abstract theory of the C language.
In fact, the C standard also states that char
is the atomic memory quantity.
Any type must be an integral number of char
s.
Bit fields are the only exception:
bit fields are only found inside structures, and cannot be referenced directly.
char
must be an integral number of bits, at least 8 of them.
Furthermore, this implies that int8_t
and uint8_t
, if they are defined,
are at least as big as char
.
In other words, on any compiler that provides them,
int8_t
is bound to be signed char
,
and uint8_t
is equivalent to unsigned char
.