Difference between signed char and unsigned char
This article explains the difference between the C types signed char and unsigned char.
Understanding the basics
The unsigned char type consists of an area of 8bits used by the CPU to store unsigned bits. This means that if the content of the 8bits is to be interpreted as an integer, the integer value can be from 0 to 255 (2^8).
The unsigned char type consists of an area of 8bits, 7 of which store a value, and one bit is used to identify to sign of the other 7bits. If the sign of the 7bits is to be interpreted as an integer, the value can by from -127 to 127 (-2^7 ~ +2^7).
It is important to keep in mind that the OS decides of the signature of a char. So to guarantee code portability it is good to explicitly specify the sign of your chars.
Illustration
The following sample program illustrates the difference between signed and unsigned char.
#include<stdio.h> int main(int argc, char *argv[]) { signed char char1=255; unsigned char char2=255; printf("Signed char : %d\n",char1); printf("Unsigned char : %d\n",char2); }
The output of the above program is as follows :
Signed char : -1 Unsigned char : 255
| Labels: coding |
|

Comment