3 ms·
"char" is portable and by definition sizeof(char) == 1 and on top of that char pointers can alias other types. If I could time travel and influence the design
by simias 8y ago
"char" is portable and by definition sizeof(char) == 1 and on top of that char pointers can alias other types.
If I could time travel and influence the design of C the first things I'd do is change switch to break by default. The 2nd thing I'd do is rename "char" into "byte" since that's effectively what it is (and it's seldom a character these days since we often use UTF-8 or other multi-byte encodings).
- saagarjha 8y ago> The 2nd thing I'd do is rename "char" into "byte" since that's effectively what it is This isn't always true. See machines where CHAR_BIT > 8.
- Sean1708 8y agoI always thought that 1 char was exactly 1 byte, but that 1 byte was not necessarily 8 bits?
- saagarjha 8y agoI think from a C standards point of view, that's true; I was using the general convention of 1 byte=8 bits, but char being one or more of these bytes. Hence CHAR_BIT is the number of bits in a char, and sizeof gives the number of chars that would fit in the specified type.
- simias 8y agoA byte is the smallest addressable memory unit, nothing more, nothing less. It's true that on most modern machines it's always equal to 8 bits (and it's mandated by POSIX AFAIK) but that's orthogonal. In French when talking about storage capacity we use "octet" instead of "byte", I always thought that made more literal sense (if you have a 1megabyte memory on a system where CHAR_BITS is 16, do you have 8 or 16 megabits?).
- vardump 8y agoThere are bit-addressable CPUs and DSPs. Examples are many microcontrollers (part of the memory accessible in bit-addressable windows), and famous TMS34010 (https://en.wikipedia.org/wiki/TMS34010 https://en.wikipedia.org/wiki/TMS34010). 4-bit CPUs (like the one in your toothbrush or thermometer) can also of course address 4-bit "words", "bytes" or nibbles. So 8 bits might not be the smallest addressable memory unit.