Question:
In C, what is an unsigned char used for? How is this different from a regular char?
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 -128 to 127 range.
- unsigned char, which gives you at least the 0 to 255 range.