Printing UTF-8 in C Console Applications on Windows
As described in the original question, it can be challenging to display UTF-8 characters correctly when using C console applications in English Windows environments. However, a solution exists that allows you to achieve this objective.
To enable UTF-8 output in your C console application, follow these steps:
Include the necessary header files:
#include <cstdio> #include <windows.h>
Set the execution character set to UTF-8:
#pragma execution_character_set( "utf-8" )
Configure the console output code page:
SetConsoleOutputCP( 65001 );
By using this approach, you can now use either cout or wcout to print UTF-8 encoded Japanese text to the console, ensuring that it is displayed correctly.
Note that some sources recommend changing the console font to Lucida Console for optimal display results. However, on certain systems, Consolas may also display the characters satisfactorily.
The above is the detailed content of How Can I Print UTF-8 Characters Correctly in a C Console Application on Windows?. For more information, please follow other related articles on the PHP Chinese website!