Difference between signed / unsigned char [duplicate]
So I know that the difference between a signed int
and unsigned int
is that a bit is used to signify if the number if positive or negative, but how does this apply to a char
? How can a character be positive or negative?
Solution 1:
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.
Solution 2:
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.
Solution 3:
There are three char types: (plain) char
, signed char
and unsigned char
. Any char is usually an 8-bit integer* and in that sense, a signed
and unsigned char
have a useful meaning (generally equivalent to uint8_t
and int8_t
). When used as a character in the sense of text, use a char
(also referred to as a plain char). This is typically a signed char
but can be implemented either way by the compiler.
* Technically, a char can be any size as long as sizeof(char)
is 1, but it is usually an 8-bit integer.
Solution 4:
Representation is the same, the meaning is different. e.g, 0xFF, it both represented as "FF". When it is treated as "char", it is negative number -1; but it is 255 as unsigned. When it comes to bit shifting, it is a big difference since the sign bit is not shifted. e.g, if you shift 255 right 1 bit, it will get 127; shifting "-1" right will be no effect.