The pointless sizeof discussion

Many C programmers seems to assume that specifying sizeof(char) in certain places increase portability. Common occurences of this expression are found with malloc, realloc, fread and fwrite in particular. Afterall, char could be more than 8-bits.

This is simply wrong. It just is.

Sure, char can -theoretically- be more than 8-bits (but not less). However, the C standard clearly states that sizeof is the number of chars required to represent its parameter in memory. Not the number of 8-bits quantities. Hence by definition, sizeof(char) is always exactly one, not just in practice, but also in the most abstract theory of C.


The C standard also states that char is the atomic memory quantity. Any type must be an integral number of chars. Bitfields are the only exception (they can only be found inside structures, and cannot be referenced directly). char must be an integral number of bits, at least 8 of them.

That 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 char, and uint8_t is equivalent to unsigned char.