Unsigned char is useful if you are working with binary data. For instance, take the following C program:
Code:
#include <stdio.h>
int main()
{
#include <stdio.h>
int main()
{
char ch = 130;
unsigned char uch = 130;
printf("char = %d; unsigned char = %d\n", ch, uch);
if (ch > 128)
{
printf("ch > 128\n");
}
else
{
printf("ch <= 128\n");
}
if (uch > 128)
{
printf("uch > 128\n");
}
else
{
printf("uch <= 128\n");
}
return 0;
}
Compilation:
gcc blah.c -o blah
blah.c: In function `main':
blah.c:10: warning: comparison is always false due to limited range of data type
Output:
./blah
char = -126; unsigned char = 130
ch <= 128
uch > 128
Note that if you are working with binary data and you need to compare the char value to a specific value >= 128, it is always going to be false, because as already stated, a signed char only stores the range -128 to 127.
Note also that when I tried to set it specifically to 130, the actual value printed when cast to an int is -126, this is due to it being stored as a two's complement signed value.