What platforms have something other than 8-bit char?

前端 未结 12 1248
再見小時候
再見小時候 2020-11-22 00:31

Every now and then, someone on SO points out that char (aka \'byte\') isn\'t necessarily 8 bits.

It seems that 8-bit char is almost universal. I would h

12条回答
  •  孤街浪徒
    2020-11-22 01:33

    When writing code, and thinking about cross-platform support (e.g. for general-use libraries), what sort of consideration is it worth giving to platforms with non-8-bit char?

    It's not so much that it's "worth giving consideration" to something as it is playing by the rules. In C++, for example, the standard says all bytes will have "at least" 8 bits. If your code assumes that bytes have exactly 8 bits, you're violating the standard.

    This may seem silly now -- "of course all bytes have 8 bits!", I hear you saying. But lots of very smart people have relied on assumptions that were not guarantees, and then everything broke. History is replete with such examples.

    For instance, most early-90s developers assumed that a particular no-op CPU timing delay taking a fixed number of cycles would take a fixed amount of clock time, because most consumer CPUs were roughly equivalent in power. Unfortunately, computers got faster very quickly. This spawned the rise of boxes with "Turbo" buttons -- whose purpose, ironically, was to slow the computer down so that games using the time-delay technique could be played at a reasonable speed.


    One commenter asked where in the standard it says that char must have at least 8 bits. It's in section 5.2.4.2.1. This section defines CHAR_BIT, the number of bits in the smallest addressable entity, and has a default value of 8. It also says:

    Their implementation-defined values shall be equal or greater in magnitude (absolute value) to those shown, with the same sign.

    So any number equal to 8 or higher is suitable for substitution by an implementation into CHAR_BIT.

提交回复
热议问题