Difference between signed / unsigned char
I slightly disagree with the above. The unsigned char
simply means: Use the most significant bit instead of treating it as a bit flag for +/- sign when performing arithmetic operations.
It makes significance if you use char
as a number for instance:
typedef char BYTE1;
typedef unsigned char BYTE2;
BYTE1 a;
BYTE2 b;
For variable a
, only 7 bits are available and its range is (-127 to 127) = (+/-)2^7 -1.
For variable b
all 8 bits are available and the range is 0 to 255 (2^8 -1).
If you use char
as character, "unsigned" is completely ignored by the compiler just as comments are removed from your program.
There's no dedicated "character type" in C language. char
is an integer type, same (in that regard) as int
, short
and other integer types. char
just happens to be the smallest integer type. So, just like any other integer type, it can be signed or unsigned.
It is true that (as the name suggests) char
is mostly intended to be used to represent characters. But characters in C are represented by their integer "codes", so there's nothing unusual in the fact that an integer type char
is used to serve that purpose.
The only general difference between char
and other integer types is that plain char
is not synonymous with signed char
, while with other integer types the signed
modifier is optional/implied.