Special characters

Hi all.

I'm new to c++ and I wonder how do you display special characters to the console.
For example, say you have a string like so:

char s [] = {'ü', 'ä', '\0' };

How do you display these charaters using cout?

Does the string need to be of type unsigned short and then needs to be converted back to char?

I'm a bit confused by this so any help is appreciated :)
Hi there

Check out http://www.cdrummond.qc.ca/cegep/informat/Professeurs/Alain/files/ascii.htm for a list of special characters and their codes. Using the desired character code, you can use the following program:
1
2
3
4
5
6
7
#include <iostream>
int main()
{
  char a = 165 //example character code
  std::cout >> a;
  return 0;
}

Enter the code for the character you want to print it onto the console.
Last edited on
Two ways of doing this.

1
2
3
4
5
6
7
8
9
10
#include <iostream>
#include <string>

int main()
{
	char* c = "üä"; //Using a char*
	std::string s = "üä"; //Using the preferred std::string.
	std::cout << s << c;
	return 0;
}
üäüä


Note the double-quotes. Also, note that \0 is a non-printing character.

EDIT @programmer47: You didn't even test that code. It most obviously doesn't work.

-Albatross
Last edited on
Printing anything but basic ASCII from the console is more difficult than you think because the standard doesn't directly enforce it, therefore the way to do it varies for different platforms/compilers.

If you're on *nix, it might be as easy as Albatross suggested, provided the cpp file is saved as UTF-8. I've found that *nix consoles accept UTF-8 as output and print it correctly.

On Windows, it's not as simple. The Windows console has these things called "code pages" which select different glyphs for character codes 0x80 and up. It ends up being a tremendous pain.

I helped a guy troubleshoot this about a year ago. Here's the thread:

http://cplusplus.com/forum/windows/9797/

I found a solution that worked on my Windows box, but I never heard from him if he ever got it working.


Also... char* c = "whatever"; is incorrect =P. It should be const char* c = "whatever";
Last edited on
@Disch: the const correctness here is a detail. =P

It would be nice, however, if the standard did enforce it (does the new standard? I should check). I am on a UNIX-certified system, and it is that easy. On Windows... it looks like you went through a lot of trouble.

-Albatross

Albatross, I had tested the code and it does work, I just mistyped "cout <<" as "cout >>". Try it out for yourself, it does work!
You also forgot a semicolon at line 4. :P

Now, on Mac OS X, at least, aside from those details, there's another problem with the actual output. I expect the problem is related to the unenforced standard, but I'm not certain. I tried this two ways, and got the same result each time.

1
2
3
4
5
6
7
#include <iostream>
int main()
{
	char a = 165; //Character code for Ñ, decimal
	std::cout << a;
	return 0;
}
\245

1
2
3
4
5
6
7
#include <iostream>
int main()
{
	char a = '\245'; //Ñ, using the \octal escape
	std::cout << a;
	return 0;
}
\245


Note: the encoding is indeed UTF-8. I specifically checked.

-Albatross
Last edited on
It could be that the code only works with Windows, but I'm not sure. I think the Mac has a different text encoding when it runs programs (but again, I'm not sure about it)
Last edited on
programmer47's code will only work on a system that is not set up to output Unicode.

Basically it will only work on Windows, and only if the right codepage is selected.

I don't know what codepage programmer47 has going, but in Unicode, Ñ is U+00D1, which is 321 octal / 209 decimal (not 245 / 165)

The reason Albatross is having trouble is because \245 is not a valid UTF-8 stream. Codepoints beyond ASCII (over 0x7F) are represented by multiple characters.

Since Albatross is on *nix it sounds like he would be outputting UTF-8, which is why he's getting that weird output.

Albatross: try this:

1
2
3
4
5
6
7
8
9
#include <iostream>
#include <cstring>
int main()
{
    const char* foo = "Ñ";
    std::cout << foo << std::endl;  // this should output Ñ as you expect
    std::cout << strlen(foo);      // this will output 2, even though 'foo' is only 1 character long
    return 0;
}
Topic archived. No new replies allowed.