Why is 'char' signed by default in C++?

后端 未结 2 1461
春和景丽
春和景丽 2020-11-28 10:09

Why is char by default in the range from -128 to 127 when it is supposed to represent a \'character\' whose textual reprezentations are in the range from 0 to 255? In this s

2条回答
  •  有刺的猬
    2020-11-28 10:29

    Here is your answer from the standard:

    3.9.1 Fundamental types [basic.fundamental]

    1 Objects declared as characters char) shall be large enough to store any member of the implementation's basic character set. If a character from this set is stored in a character object, the integral value of that character object is equal to the value of the single character literal form of that character. It is implementation-defined whether a char object can hold negative values. Characters can be explicitly declared unsigned or signed. Plain char, signed char, and unsigned char are three distinct types. A char, a signed char, and an unsigned char occupy the same amount of storage and have the same alignment requirements (basic.types); that is, they have the same object representation. For character types, all bits of the object representation participate in the value representation. For unsigned character types, all possible bit patterns of the value representation represent numbers. These requirements do not hold for other types. In any particular implementation, a plain char object can take on either the same values as a signed char or an unsigned char; which one is implementation-defined.

提交回复
热议问题