In C/C++, what an
unsigned char is used for? How is it different from a regular
In C++, there are three distinct character types:
If you are using character types for text, use the unqualified
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.
This is implementation dependent, as the C standard does NOT define the signed-ness of
char. Depending on the platform, char may be
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
unsigned char will be an 8-bit unsigned integer (
255). Note the standard does NOT require that
char types have 8 bits, only that
1. You can get at the number of bits in a char with
limits.h. There are few if any platforms today where this will be something other than
There is a nice summary of this issue here.
As others have mentioned since I posted this, you're better off using
uint8_t if you really want to represent small integers.