Why don't the C or C++ standards explicitly define char as signed or unsigned?

前端 未结 2 1783
北恋
北恋 2020-11-27 19:18
int main()
{
    char c = 0xff;
    bool b = 0xff == c;
    // Under most C/C++ compilers\' default options, b is FALSE!!!
}

Neither the C or C++ s

2条回答
  •  星月不相逢
    2020-11-27 19:56

    char at first is meant to store characters, so whether it's signed or unsigned is not important. What really matters is how to perform maths on char efficiently. So depend on the system, the compiler will choose what's most appropriate

    Prior to ARMv4, ARM had no native support for loading halfwords and signed bytes. To load a signed byte you had to LDRB then sign extend the value (LSL it up then ASR it back down). This is painful so char is unsigned by default.

    why unsigned types are more efficent in arm cpu?

    In fact a lot of ARM compilers still use unsigned char by default, because even if you can load a byte with sign extension on modern ARM ISAs, that instruction is still less flexible than the zero extension version

    • is char signed or unsigned by default on iOS?
    • char is unsigned by default on Android NDK

    And most modern compilers also allow you to change char's signness instead of using the default setting

提交回复
热议问题