Why C and C++ hate signed char so much?

前端 未结 3 1440
旧巷少年郎
旧巷少年郎 2021-01-11 10:47

Why does C allow accessing object using "character type":

6.5 Expressions (C)

An object shall have its stored value acc

3条回答
  •  臣服心动
    2021-01-11 11:50

    Here's my take on the motivation:

    On a non-twos-complement system, signed char will not be suitable for accessing the representation of an object. This is because either there are two possible signed char representations which have the same value (+0 and -0), or one representation that has no value (a trap representation). In either case, this prevents you from doing most meaningful things you might do with the representation of an object. For example, if you have a 16-bit unsigned integer 0x80ff, one or the other byte, as a signed char, is going to either trap or compare equal to 0.

    Note that on such an implementation (non-twos-complement), plain char needs to be defined as an unsigned type for accessing the representations of objects via char to work correctly. While there's no explicit requirement, I see this as a requirement derived from other requirements in the standard.

提交回复
热议问题