Reputation: 1317
what is the purpose of signed char if both char and signed char ranges from -127 - 127? what is the place where we use signed char instead of just char?
Upvotes: 5
Views: 1886
Reputation: 31
See lamia,
First I want to prepare background for your question.
................................................
char data type is of two types:
unsigned char;
signed char;
(i.e. INTEGRAL DATATYPES)
.................................................
Exaplained as per different books as:
char 1byte –128 to 127 (i.e. by default signed char)
signed char 1byte –128 to 127
unsigned char 1byte 0 to 255
.................................................
one more thing 1byte=8 bits.(zero to 7th bit)
As processor flag register reserves 7th bit for representing sign(i.e. 1=+ve & 0=-ve)
-37 will be represented as 1101 1011 (the most significant bit is 1),
+37 will be represented as 0010 0101 (the most significant bit is 0).
.................................................
similarly for char last bit is by default taken as signed
This is why?
Because char also depends on ASCII codes of perticular charectors(Eg.A=65).
In any case we are using char and using 7 bits only.
In this case to increase memory range for char/int by 1 bit we use unsigned char or unsigned int;
Thanks for the question.
Upvotes: 2
Reputation: 154047
It is implementation defined whether plain char
uses the same
representation as signed char
or unsigned char
. signed char
was
introduced because plain char
was underspecified. There's also the
message you send to your readers:
char
: character datasigned char
: small itegersunsigned char
: raw memory(unsigned char
may also be used if you're doing a lot of bitwise
operations. In practice, that tends to overlap with the raw memory
use.)
Upvotes: 7
Reputation: 36451
Note that on many systems, char
is signed char
.
As for your question: Well, you would use it when you would need a small signed number.
Upvotes: 1
Reputation: 385395
unsigned char
is unsigned.
signed char
is signed.
char
may be unsigned or signed depending on your platform.
Use signed char
when you definitely want signedness.
Possibly related: What does it mean for a char to be signed?
Upvotes: 19