#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.
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:
@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.
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.
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)
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()
{
constchar* 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;
}