What is the difference between char, unsigned char and signed char? (All 3 are different data types)[SOLVED] | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 6

What is the difference between char, unsigned char and signed char? (All 3 are different data types)[SOLVED]

All three are different data types (according to http://eel.is/c++draft/basic#fundamental in 7th point of section 6.8.2 Fundamental types) What are the difference between them. Please explain by giving examples.

4th Jan 2021, 2:19 PM
Yugal Kishore
13 Answers
+ 11
Yugal Kishore I'm thinking it could be a Windows issue, instead of the code editor, but I may be wrong and there could be other factors. Just quoting the link I attached above: "Unlike most Unix-based systems that implement Unicode support using UTF-8 (which uses the regular char data type), Windows does not have built-in support for UTF-8. It uses UTF-16 instead, which requires that you use the larger wchar_t type." Since characters like 'a' and '@' are not part of the extended ASCII set, they do not cause such issues.
4th Jan 2021, 3:45 PM
Hatsy Rei
Hatsy Rei - avatar
+ 8
(Signed) chars store a single byte of memory. The value stored by this byte can range from -128 to 127. Unsigned chars also store one byte, but since it is unsigned, the possible range of value is 0 to 255 instead.
4th Jan 2021, 2:29 PM
Hatsy Rei
Hatsy Rei - avatar
+ 8
Yugal Kishore Interesting. I did not consider those semantics, but yes, in terms of C++ they do refer to different types. From what I can gather, when you use "char" to declare a character variable, the compiler decides whether this is actually a "unsigned char" or a "signed char". In other words, it is machine dependent. https://stackoverflow.com/questions/46463064/what-causes-a-char-to-be-signed-or-unsigned-when-using-gcc I have been so used to char just being compiled to a signed character that it comes naturally to me that chars by default are signed. This is actually untrue, and I suppose this is the distinction between those terms.
4th Jan 2021, 3:00 PM
Hatsy Rei
Hatsy Rei - avatar
+ 7
Yugal Kishore Your code actually works and outputs the character, but Code Playground doesn't seem to be able to display the symbol for me (instead it shows a question mark in place). The warning you get is because the copyright symbol was treated as unicode, before being truncated to 169 (or, in signed value, -87). https://stackoverflow.com/questions/15564047/printing-copyright-symbol-in-visual-studio-2010/15564294#15564294
4th Jan 2021, 3:25 PM
Hatsy Rei
Hatsy Rei - avatar
+ 6
You can find answers here. Next time use search bar to get easier answers. Thanks and Happy Coding! https://www.sololearn.com/Discuss/2361043/?ref=app
4th Jan 2021, 2:21 PM
noteve
noteve - avatar
+ 5
The char datatype itself is already signed. I just mentioned it there to make the distinction between signed and unsigned values. Sorry if it caused some confusion - You don't actually write "signed char" to declare a signed character variable, just "char".
4th Jan 2021, 2:41 PM
Hatsy Rei
Hatsy Rei - avatar
+ 5
As for characters corresponding to the negative values in a signed character variable, you can refer to the extended ASCII table. IIRC, everything past 127 overflows to -127. This means that characters assuming 128 to 255 correspond to values -127 to -1 in signed characters. https://www.ascii-code.com/
4th Jan 2021, 3:03 PM
Hatsy Rei
Hatsy Rei - avatar
+ 3
Hatsy Rei Okay. But it should atleast run in any other code editor like vscode. But it is showing error there too. Same thing happen with other symbols too (® a £ € ñ etc) If the problem in code editor is that it can't print © then atleast it should print it's ascii code in the following program as it shows ascii value of 'a' and '@' #include <iostream> using namespace std; int main() { int a='©'; int b='a'; int c='@'; cout<<a<<endl<<b<<endl<<c; return 0; }
4th Jan 2021, 3:41 PM
Yugal Kishore
+ 2
Hatsy Rei And what about just char? As char stores the characters in form of ASCII code, then how can an ASCII code be in negative? if you try to write signed char a='-1'; Or signed char a=-1; Both will throw an error or just leave blank or leave 1 as output
4th Jan 2021, 2:40 PM
Yugal Kishore
+ 2
Hatsy Rei thankyou so much for your answers and help.🙂
4th Jan 2021, 4:49 PM
Yugal Kishore
+ 1
《 Nicko12 》 Hey my question isn't just for signed and unsigned But Char, signed char and unsigned char. Basically I am confused with char and signed char
4th Jan 2021, 2:26 PM
Yugal Kishore
+ 1
Hatsy Rei But c++ fundamental says that char, signed char and unsigned char all 3 are different. (Link is in my question) And for signed can you give an example of negative characters (as in ascii) ?
4th Jan 2021, 2:44 PM
Yugal Kishore
0
Hatsy Rei So according to your link, © corresponds to ascii 169, so it should include in signed char only. And this program #include <iostream> using namespace std; int main() { signed char a='©'; cout<<a; return 0; } Should work just fine. BUT it is showing errors.
4th Jan 2021, 3:19 PM
Yugal Kishore