Summary. . What should I do to correctly print a string literal defined in the source code that was saved in UTF-8 encoding (Windows CP 65001) to the cmd console using the std::cout stream?
Motivation: I would like to change the excellent Catch unit-testing framework (as an experiment), so that it will display my texts with accented characters. Modification should be simple, reliable, and should also be useful for other languages and work environments so that it can be accepted by the author as an improvement. Or if you know Catch and if there is some alternative solution, can you post it?
Details: Start with the Czech version of the "fast brown fox ..."
#include <iostream> #include "windows.h" using namespace std; int main() { cout << "\n-------------------------- default cmd encoding = 852 -------------------\n"; cout << "Příšerně žluťoučký kůň úpěl ďábelské ódy!" << endl; cout << "\n-------- Windows Central European (1250) set for the cmd console --------\n"; SetConsoleOutputCP(1250); std::cout << "Příšerně žluťoučký kůň úpěl ďábelské ódy!" << std::endl; cout << "\n------------- Windows UTF-8 (65001) set for the cmd console -------------\n"; SetConsoleOutputCP(CP_UTF8); std::cout << "Příšerně žluťoučký kůň úpěl ďábelské ódy!" << std::endl; }
It prints the following (font installed in Lucida Console): 
The default cmd encoding is 852, the standard Windows encoding is 1250, and the source code was saved using the 65001 encoding (UTF-8 with specification). SetConsoleOutputCP(1250); changes the encoding of cmd (programmatically) in the same way as chcp 1250 .
Observation: When setting the encoding 1250, the UTF-8 string literal is printed correctly. I think this can be explained, but it is really strange. Is there a decent, human, general way to solve the problem?
Update: "narrow string literal" is stored using the Windows-1250 encoding in my case (built-in Windows encoding for Central European countries). It seems to be independent of the encoding of the source code. The compiler saves it in native Windows encoding. Because of this, switching cmd to this encoding gives the desired result. This is illegal, but how can I get my own Windows encoding programmatically (pass it to SetConsoleOutputCP(cpX) )? I need a constant that is valid for the machine where the compilation took place. It should not be a native encoding for the machine on which the executable is running.
In C ++ 11, u8"the UTF-8 string literal" also added, but it is not suitable for SetConsoleOutputCP(CP_UTF8);