What is an unsigned char?

后端 未结 17 1126
借酒劲吻你
借酒劲吻你 2020-11-22 11:05

In C/C++, what an unsigned char is used for? How is it different from a regular char?

相关标签:
17条回答
  • 2020-11-22 11:37

    In C++, there are three distinct character types:

    • char
    • signed char
    • unsigned char

    If you are using character types for text, use the unqualified char:

    • it is the type of character literals like 'a' or '0'.
    • it is the type that makes up C strings like "abcde"

    It also works out as a number value, but it is unspecified whether that value is treated as signed or unsigned. Beware character comparisons through inequalities - although if you limit yourself to ASCII (0-127) you're just about safe.

    If you are using character types as numbers, use:

    • signed char, which gives you at least the -127 to 127 range. (-128 to 127 is common)
    • unsigned char, which gives you at least the 0 to 255 range.

    "At least", because the C++ standard only gives the minimum range of values that each numeric type is required to cover. sizeof (char) is required to be 1 (i.e. one byte), but a byte could in theory be for example 32 bits. sizeof would still be report its size as 1 - meaning that you could have sizeof (char) == sizeof (long) == 1.

    0 讨论(0)
  • 2020-11-22 11:37

    If you want to use a character as a small integer, the safest way to do it is with the int8_tand uint8_t types.

    0 讨论(0)
  • 2020-11-22 11:38

    char and unsigned char aren't guaranteed to be 8-bit types on all platforms—they are guaranteed to be 8-bit or larger. Some platforms have 9-bit, 32-bit, or 64-bit bytes. However, the most common platforms today (Windows, Mac, Linux x86, etc.) have 8-bit bytes.

    0 讨论(0)
  • 2020-11-22 11:40

    unsigned char is the heart of all bit trickery. In almost ALL compiler for ALL platform an unsigned char is simply a byte and an unsigned integer of (usually) 8 bits that can be treated as a small integer or a pack of bits.

    In addiction, as someone else has said, the standard doesn't define the sign of a char. so you have 3 distinct char types: char, signed char, unsigned char.

    0 讨论(0)
  • 2020-11-22 11:40

    An unsigned char is an unsigned byte value (0 to 255). You may be thinking of char in terms of being a "character" but it is really a numerical value. The regular char is signed, so you have 128 values, and these values map to characters using ASCII encoding. But in either case, what you are storing in memory is a byte value.

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