What is an unsigned char?

后端 未结 17 1124
借酒劲吻你
借酒劲吻你 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:16

    This is implementation dependent, as the C standard does NOT define the signed-ness of char. Depending on the platform, char may be signed or unsigned, so you need to explicitly ask for signed char or unsigned char if your implementation depends on it. Just use char if you intend to represent characters from strings, as this will match what your platform puts in the string.

    The difference between signed char and unsigned char is as you'd expect. On most platforms, signed char will be an 8-bit two's complement number ranging from -128 to 127, and unsigned char will be an 8-bit unsigned integer (0 to 255). Note the standard does NOT require that char types have 8 bits, only that sizeof(char) return 1. You can get at the number of bits in a char with CHAR_BIT in limits.h. There are few if any platforms today where this will be something other than 8, though.

    There is a nice summary of this issue here.

    As others have mentioned since I posted this, you're better off using int8_t and uint8_t if you really want to represent small integers.

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

    As for example usages of unsigned char:

    unsigned char is often used in computer graphics, which very often (though not always) assigns a single byte to each colour component. It is common to see an RGB (or RGBA) colour represented as 24 (or 32) bits, each an unsigned char. Since unsigned char values fall in the range [0,255], the values are typically interpreted as:

    • 0 meaning a total lack of a given colour component.
    • 255 meaning 100% of a given colour pigment.

    So you would end up with RGB red as (255,0,0) -> (100% red, 0% green, 0% blue).

    Why not use a signed char? Arithmetic and bit shifting becomes problematic. As explained already, a signed char's range is essentially shifted by -128. A very simple and naive (mostly unused) method for converting RGB to grayscale is to average all three colour components, but this runs into problems when the values of the colour components are negative. Red (255, 0, 0) averages to (85, 85, 85) when using unsigned char arithmetic. However, if the values were signed chars (127,-128,-128), we would end up with (-99, -99, -99), which would be (29, 29, 29) in our unsigned char space, which is incorrect.

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

    signed char and unsigned char both represent 1byte, but they have different ranges.

       Type        |      range
    -------------------------------
    signed char    |  -128 to +127
    unsigned char  |     0 to 255
    

    In signed char if we consider char letter = 'A', 'A' is represent binary of 65 in ASCII/Unicode, If 65 can be stored, -65 also can be stored. There are no negative binary values in ASCII/Unicode there for no need to worry about negative values.

    Example

    #include <stdio.h>
    
    int main()
    {
        signed char char1 = 255;
        signed char char2 = -128;
        unsigned char char3 = 255;
        unsigned char char4 = -128;
    
        printf("Signed char(255) : %d\n",char1);
        printf("Unsigned char(255) : %d\n",char3);
    
        printf("\nSigned char(-128) : %d\n",char2);
        printf("Unsigned char(-128) : %d\n",char4);
    
        return 0;
    }
    

    Output -:

    Signed char(255) : -1
    Unsigned char(255) : 255
    
    Signed char(-128) : -128
    Unsigned char(-128) : 128
    
    0 讨论(0)
  • 2020-11-22 11:22

    Some googling found this, where people had a discussion about this.

    An unsigned char is basically a single byte. So, you would use this if you need one byte of data (for example, maybe you want to use it to set flags on and off to be passed to a function, as is often done in the Windows API).

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

    An unsigned char uses the bit that is reserved for the sign of a regular char as another number. This changes the range to [0 - 255] as opposed to [-128 - 127].

    Generally unsigned chars are used when you don't want a sign. This will make a difference when doing things like shifting bits (shift extends the sign) and other things when dealing with a char as a byte rather than using it as a number.

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

    signed char has range -128 to 127; unsigned char has range 0 to 255.

    char will be equivalent to either signed char or unsigned char, depending on the compiler, but is a distinct type.

    If you're using C-style strings, just use char. If you need to use chars for arithmetic (pretty rare), specify signed or unsigned explicitly for portability.

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