Geoff wrote:
> Does the standard guarantee that CHAR_BIT will always be 8?
No. The standard guarantees only that CHAR_BIT will NOT BE LESS THAN 8. It
doesn't define an explicit upper bound for CHAR_BIT (the implicit upper
bound would have a practical limit based on the internal representation of a
pre-processor's "constant-expression").
> In other words, does one ever have to defensively write:
>
> #include <limits.h>
>
> #if CHAR_BIT != 8
> #error This code assumes CHAR_BIT is 8;
> #endif
>
> ... for code that assumes the ASCII character set is in effect?
You mistakenly conflate 8-bit char widths with the ASCII character code.
Your code (above) won't guarantee that "the ASCII character set is in
effect", no matter what the size of CHAR_BIT.
For instance, EBCDIC is an 8-bit character code, and an EBCDIC-based
compiler on an IBM system would probably (knowing IBM's 360/370/zSeries
architecture) have CHAR_BIT == 8. However, the character code in use /would
NOT/ be ASCII.
For that matter, ASCII is a 7-bit character code, and is expressed in C in
/at least/ 8 bits; your code would exclude (say) a PDP-11 (with 9-bit
characters) that uses ASCII as it's character code.
--
Lew Pitcher
"In Skills, We Trust"
PGP public key available upon request