On older machines, codes smaller than 8 bits were fairly common, but most of those have been dead and gone for years now.
C and C++ have mandated a minimum of 8 bits for char
, at least as far back as the C89 standard. [Edit: For example, C90, §5.2.4.2.1 requires CHAR_BIT
>= 8 and UCHAR_MAX
>= 255. C89 uses a different section number (I believe that would be §2.2.4.2.1) but identical content]. They treat "char" and "byte" as essentially synonymous [Edit: for example, CHAR_BIT
is described as: "number of bits for the smallest object that is not a bitfield (byte)".]
There are, however, current machines (mostly DSPs) where the smallest type is larger than 8 bits -- a minimum of 12, 14, or even 16 bits is fairly common. Windows CE does roughly the same: its smallest type (at least with Microsoft's compiler) is 16 bits. They do not, however, treat a char
as 16 bits -- instead they take the (non-conforming) approach of simply not supporting a type named char
at all.
CHAR_BIT
. – Equanimitychar
is by definition a "byte" doesn't really make much difference. These are just two internal terms in C/C++ terminology, which can be used interchangeably. The "byte" from C/C++ terminology has no formal relation to the machine "byte". – HaneyCHAR_BIT
be something other than 8 is not planning for the future; it's dragging along ancient history. POSIX finally shut the door on that chapter by mandatingCHAR_BIT==8
, and I don't think we'll ever see a new general-purpose (non-DSP) machine with non-8-bit bytes. Even on DSPs, makingchar
16- or 32-bit is just laziness by the compiler implementors. They could still makechar
8-bit if they wanted, and hopefully C2020 or so will force them to... – Deepfreezefputc
(and other output functions) must write more than a single octet to the communications channel (file/socket/etc.). This meansfputc('A', f)
will write at least two octets, leaving extra junk (likely one or more null octets) for the recipient to read. Conversely,fgetc
would read multiple octets from the communication channel as a singlechar
for the host, requiring further ugly processing to break it apart to use it. This is a brief summary in limited comment space. – Deepfreezefgetc
andfputc
must be value-preserving. Not to mention, if it didn't, saving/loading binary data would not work. – Deepfreezechar
s, which will represent "bytes" in C/C++sense of the term. – HaneyCHAR_BIT
values differ, suddenly we also have to worry about bit-ordering. The order of bytes in an I/O stream is well-defined, but the order of bits in an I/O stream is not specified AFAIK. If one machine outputs 60 bits to an I/O stream and the other reads 8, which 8 are they? What happens to the 4 left over (60-7*8)? I just give up and requireCHAR_BIT = 8
for cross-machine I/O, which works most of the time. – Gabriellia