Output Unicode to console Using C++, in Windows
Solution 1:
What about std::wcout
?
#include <iostream>
int main() {
std::wcout << L"Hello World!" << std::endl;
return 0;
}
This is the standard wide-characters output stream.
Still, as Adrian pointed out, this doesn't address the fact cmd
, by default, doesn't handle Unicode outputs. This can be addressed by manually configuring the console, like described in Adrian's answer:
- Starting
cmd
with the/u
argument; - Calling
chcp 65001
to change the output format; - And setting a unicode font in the console (like Lucida Console Unicode).
You can also try to use _setmode(_fileno(stdout), _O_U16TEXT);
, which require fcntl.h
and io.h
(as described in this answer, and documented in this blog post).
Solution 2:
I'm not sure Windows XP will fully support what you need. There are three things you have to do to enable Unicode with a command console:
- Start the command window with
cmd /u
. The/u
says your programs will output Unicode. - Use
chcp 65001
to indicate you want to use UTF-8 instead of one of the code pages. - Select a font with more glyph coverage. The command windows in newer versions of Windows offer
Lucida Console Unicode
. My XP box has a subset of that calledLucida Console
. It doesn't have a very extensive repertoire, but it should be sufficient if you're just trying to display some accented characters.
Solution 3:
You can use the open-source {fmt} library to portably print Unicode text, including on Windows, for example:
#include <fmt/core.h>
int main() {
fmt::print("Blah blah blah some gibberish unicode: ĐĄßĞĝ\n");
}
Output:
Blah blah blah some gibberish unicode: ĐĄßĞĝ
This requires compiling with the /utf-8
compiler option in MSVC.
I don't recommend using wcout
because it is non-portable, for example:
std::wcout << L"Blah blah blah some gibberish unicode: ĐĄßĞĝ\n";
will print the ĐĄßĞĝ
part incorrectly on macOS or Linux (https://godbolt.org/z/z81jbb):
Blah blah blah some gibberish unicode: ??ss??
and doesn't even work on Windows without changing the code page:
Blah blah blah some gibberish unicode:
Disclaimer: I'm the author of {fmt}.