I need to print some unicode characters on the Linux terminal using iostream
. Strange things happen though. When I write:
cout << "\u2780";
I get: ➀
, which is almost exactly what I want. However if I write:
cout << '\u2780';
I get: 14851712
.
The problem is, I don't know the exact character to be printed at compile-time. Therefore I'd like to do something like:
int x;
// some calculations...
cout << (char)('\u2780' + x);
Which prints: �
. Using wcout
or wchar_t
instead don't work either. How do I get correct printing?
From what I found around on the Internet it seems important that I use g++ 4.7.2 compiler straight from Debian Wheezy repository.
The Unicode character \u2780
is outside of the range for the char
datatype. You should have received this compiler warning to tell you about it: (at least my g++ 4.7.3 gives it)
test.cpp:6:13: warning: multi-character character constant [-Wmultichar]
If you want to work with characters like U+2780 as single units you'll have to use the widechar datatype wchar_t
, or if you are lucky enough to be able to work with C++11, char32_t
or char16_t
. Note that one 16-bit unit is not enough to represent the full range of Unicode characters.
If that's not working for you, it's probably because the default "C" locale doesn't have support for non-ASCII output. To fix that problem you can call setlocale
in the start of the program; that way you can output the full range of characters supported by the user's locale: (which may or may not have support for all of the characters you use)
#include <clocale>
#include <iostream>
using namespace std;
int main() {
setlocale(LC_ALL, "");
wcout << L'\u2780';
return 0;
}