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!
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.
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
signedchar a = 'f';
unsignedchar 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
signedchar a = 'я';
unsignedchar b = 'я';
std::cout << int(a) << '\n' <<
int(b) << '\n' <<
(a == b);