When attempting to output Unicode characters to the Windows console using cout in C , strange characters are displayed instead. Why does this occur, and is it possible to display Unicode characters correctly?
The issue arises from the default behavior of the Windows console, which doesn't handle Unicode outputs. To resolve this, the wide-characters output stream std::wcout can be used instead of cout.
#include <iostream> int main() { std::wcout << L"Hello World!" << std::endl; return 0; }
However, even with std::wcout, the console may still fail to handle Unicode outputs. To address this, the console can be configured manually.
To manually configure the console:
Another solution is to use _setmode(_fileno(stdout), _O_U16TEXT);. This function requires the inclusion of fcntl.h and io.h, and allows the output stream to handle Unicode characters.
The above is the detailed content of How Can I Display Unicode Characters Correctly in the Windows Console Using C ?. For more information, please follow other related articles on the PHP Chinese website!