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.
A signed char
is a signed value which is typically smaller than, and is guaranteed not to be bigger than, a short
. An unsigned char
is an unsigned value which is typically smaller than, and is guaranteed not to be bigger than, a short
. A type char
without a signed
or unsigned
qualifier may behave as either a signed or unsigned char
; this is usually implementation-defined, but there are a couple of cases where it is not:
Part of the reason there are two dialects of "C" (those where char
is signed, and those where it is unsigned) is that there are some implementations where char
must be unsigned, and others where it must be signed.
The same way -- e.g. if you have an 8-bit char, 7 bits can be used for magnitude and 1 for sign. So an unsigned char might range from 0 to 255, whilst a signed char might range from -128 to 127 (for example).
This because a char
is stored at all effects as a 8-bit number. Speaking about a negative or positive char
doesn't make sense if you consider it an ASCII code (which can be just signed*) but makes sense if you use that char
to store a number, which could be in range 0-255 or in -128..127 according to the 2-complement representation.
*: it can be also unsigned, it actually depends on the implementation I think, in that case you will have access to extended ASCII charset provided by the encoding used
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.
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.
The same way how an int
can be positive or negative. There is no difference. Actually on many platforms unqualified char
is signed.
Source: Stackoverflow.com