954,483 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Question about char values

Ok I feel like this may be a dumb question, but I'm putting it out there anyway. It's been a while since I've worked with C++.
What characters does C++ print for negative values of char?
Positive values seem to follow ASCII, but ASCII is only 128 characters. So where are the others from?

#include<iostream>

using namespace std;

int main() {

   char myChar = 127;
   
   while(myChar > -128) {
      cout << static_cast<int>(myChar) << " " << myChar << endl;
      myChar--;
   }
   cout << static_cast<int>(myChar) << " " << myChar << endl;

   return 0;
}
EvolutionFallen
Junior Poster
198 posts since Aug 2009
Reputation Points: 40
Solved Threads: 31
 

>Positive values seem to follow ASCII, but ASCII is only 128 characters. So where are the others from?
It really depends on the character set your system is using, but probably Unicode or some form of extended ASCII.

Narue
Bad Cop
Administrator
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
 

Thanks Narue. I guess it must be extended ASCII (didn't know that exists) because if I set char > 127 or < -128 the program does funny things.

EvolutionFallen
Junior Poster
198 posts since Aug 2009
Reputation Points: 40
Solved Threads: 31
 

Yup, this looks like what I get. I think the characters I get from negative values are from the second table: http://www.cdrummond.qc.ca/cegep/informat/Professeurs/Alain/files/ascii.htm

Thanks again, Narue!

EvolutionFallen
Junior Poster
198 posts since Aug 2009
Reputation Points: 40
Solved Threads: 31
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You