In C/C++, what an unsigned char
is used for? How is it different from a regular char
?
In terms of direct values a regular char is used when the values are known to be between CHAR_MIN
and CHAR_MAX
while an unsigned char provides double the range on the positive end. For example, if CHAR_BIT
is 8, the range of regular char
is only guaranteed to be [0, 127] (because it can be signed or unsigned) while unsigned char
will be [0, 255] and signed char
will be [-127, 127].
In terms of what it's used for, the standards allow objects of POD (plain old data) to be directly converted to an array of unsigned char. This allows you to examine the representation and bit patterns of the object. The same guarantee of safe type punning doesn't exist for char or signed char.
quoted frome "the c programming laugage" book:
The qualifier signed
or unsigned
may be applied to char or any integer. unsigned numbers
are always positive or zero, and obey the laws of arithmetic modulo 2^n, where n is the number
of bits in the type. So, for instance, if chars are 8 bits, unsigned char variables have values
between 0 and 255, while signed chars have values between -128 and 127 (in a two' s
complement machine.) Whether plain chars are signed or unsigned is machine-dependent,
but printable characters are always positive.
Because i feel it's really called for, i just want to state some rules of C and C++ (they are the same in this regard). First, all bits of unsigned char
participate in determining the value if any unsigned char object. Second, unsigned char
is explicitly stated unsigned.
Now, i had a discussion with someone about what happens when you convert the value -1
of type int to unsigned char
. He refused the idea that the resulting unsigned char
has all its bits set to 1, because he was worried about sign representation. But he don't have to. It's immediately following out of this rule that the conversion does what is intended:
If the new type is unsigned, the value is converted by repeatedly adding or subtracting one more than the maximum value that can be represented in the new type until the value is in the range of the new type. (
6.3.1.3p2
in a C99 draft)
That's a mathematical description. C++ describes it in terms of modulo calculus, which yields to the same rule. Anyway, what is not guaranteed is that all bits in the integer -1
are one before the conversion. So, what do we have so we can claim that the resulting unsigned char
has all its CHAR_BIT
bits turned to 1?
UCHAR_MAX+1
to -1
will yield a value in range, namely UCHAR_MAX
That's enough, actually! So whenever you want to have an unsigned char
having all its bits one, you do
unsigned char c = (unsigned char)-1;
It also follows that a conversion is not just truncating higher order bits. The fortunate event for two's complement is that it is just a truncation there, but the same isn't necessarily true for other sign representations.
unsigned char
takes only positive values....like 0 to 255
where as
signed char
takes both positive and negative values....like -128 to +127
If you like using various types of specific length and signedness, you're probably better off with uint8_t
, int8_t
, uint16_t
, etc simply because they do exactly what they say.
unsigned char
takes only positive values: 0 to 255 while
signed char
takes positive and negative values: -128 to +127.