Tell me how the Standards regulate the size of the byte?
I heard somewhere that there is a convention in C that a byte can only be 8 bits. But I can't find the source.
Many are accustomed to operating with bytes using char and CHAR_BIT , but char is not only 8-bit.
CHAR_BIT is also misleading, because this macro constant seems to have nothing to do with the byte size, it refers to the bitness of the char type.
Often there is a need to navigate through memory / objects through void * , exactly byte-wise. Someone uses a cast to char * for this, but this is obviously wrong. I, for example, use the cast to uint8_t * , but that is probably also wrong.
I have not seen the 8 bits per byte convention anywhere in C. But let's open the c11 standard – this is certainly not the standard itself, but a document that is very close to it.
Clause 6.2.6 Representations of types gives the following
Values stored in non-bit-field objects of any other object type consist of n × CHAR_BIT bits, where n is the size of an object of that type, in bytes. The value may be copied into an object of type unsigned char [n] (eg, by memcpy);
it says a little implicitly that
n × CHAR_BIT bits == n bytes. That is,
CHAR_BIT bits is one byte.
At the same time, paragraph 3.6 says
byte addressable unit of data storage large enough to hold any member of the basic character set of the execution environment
that is, a byte is enough to accommodate any character type. hmm …
NOTE 1 It is possible to express the address of each individual byte of an object uniquely.
byte must have a unique address
NOTE 2 A byte is composed of a contiguous sequence of bits, the number of which is implementation defined.
and here it is – the number of bits is determined by the implementation.
Paragraph 188.8.131.52 Numerical limits says that CHAR_BIT is at least 8 or more.
What to do? Add the CHAR_BIT == 8 check to the build system and sleep well: the platform is most likely x86 / 64, you can use
char , 'memcpy' / 'memmove' functions, everything will work.
But when the condition does not work, then start raising a panic and asking for documentation to the compiler.
Here they say that not eight-bit can be on
- Texas Instruments C54x DSPs (OMAP2) (16 bit)
- Digital Equipment Corporation PDP-6/10 (36 bit)
- IBM 701/704/709/7090/7094 (36 bit)
- UNIVAC 1103 / 1103A / 1105/1100/2200 (36 bit)