Signed vs unsigned char

Heelo, All!
Plese, can someone explain me the difference between unsigned and signed char?

Some EASIEST examples would be extremely helpfull!
Or , at least good source of informatins!
I appologise, tried to find but on accessible literature just cant find- nor google it!

MANY THANKS!!!

char (and signed/unsigned variations) is a smallest integral type in C++. On PC it is usually 8 bits in size. That means it can hold 256 distinct values. In signed char values threated as signed and belongs to range [-128;127]. Unsigned char holds unsigned (ppositive) values and which belongs to range [0;255].

TL;DR: same difference as between signed and unsigned int.
Hello, MiiNiPaa!!!
MANY THANKS!!!

The only problem is I stil can't find out an example for signed letters!!!

I can undersand what is difference between (-3) and (+3)==(3)

But , how should I imagine a and (-a)?
Even in ascii table, no letter has negative number value...

Please ,if somoen has nerves for that...!!!
MANY THANKS!!
All characters are represented by character code i.e. number. Mapping of those numbers to actual characters called a codepage. For example in ASCII code for 'a' is 97, so ('a' == 97) will be true.

Basic ASCII contains only character with codes from 0 to 127 so it can be stored in both signed or unsigned characters and there would not be any problems:
1
2
3
signed char a = 'f';
unsigned char b = 'f';
std::cout << (a == b); // true 

But there is extended ascii which uses range 128-255. When you try to fit it into singed chars overflow happens and usually (through technically it is UB) it value becomes negative. It is either graphic symbols, or national symbols. And there could be problems:
1
2
3
4
5
signed char a = 'я';
unsigned char b = 'я';
std::cout << int(a) << '\n' << 
             int(b) << '\n' << 
             (a == b); 
-113
143
0
Last edited on
Topic archived. No new replies allowed.