why is char's sign-ness not defined in C?

前端 未结 5 1287
走了就别回头了
走了就别回头了 2020-12-06 16:19

The C standard states:

ISO/IEC 9899:1999, 6.2.5.15 (p. 49)

The three types char, signed char, and unsigned char are collectively called th

相关标签:
5条回答
  • 2020-12-06 16:33

    On some machines, a signed char would be too small to hold all the characters in the C character set (letters, digits, standard punctuation, etc.) On such machines, 'char' must be unsigned. On other machines, an unsigned char can hold values larger than a signed int (since char and int are the same size). On those machines, 'char' must be signed.

    0 讨论(0)
  • 2020-12-06 16:34

    in those good old days C was defined, the character world was 7bit, so the sign-bit could be used for other things (like EOF)

    0 讨论(0)
  • 2020-12-06 16:45

    "Plain" char having unspecified signed-ness allows compilers to select whichever representation is more efficient for the target architecture: on some architectures, zero extending a one-byte value to the size of "int" requires less operations (thus making plain char 'unsigned'), while on others the instruction set makes sign-extending more natural, and plain char gets implemented as signed.

    0 讨论(0)
  • 2020-12-06 16:48

    I suppose (out of the top of my head) that their thinking was along the following lines:

    If you care about the sign of char (using it as a byte) you should explicitly choose signed or unsigned char.

    0 讨论(0)
  • 2020-12-06 16:54

    Perhaps historically some implementations' "char" were signed and some were unsigned, and so to be compatible with both they couldn't define it as one or the other.

    0 讨论(0)
提交回复
热议问题