Home >Backend Development >C++ >How to Display UTF-8 Characters in a C Console Application on Windows?
Unicode Console Output in C on Windows: A Guide to UTF-8 Display
For a C console application targeting Windows, displaying UTF-8 encoded characters poses a challenge. This is because the default console settings on Windows do not support UTF-8 encoding. Here's a step-by-step solution to printing and correctly displaying UTF-8 encoded text in a C console application on Windows:
Pragma Directive:
Add the following pragma directive at the top of your code:
#pragma execution_character_set("utf-8")
Set Console Output Code Page:
In your main() function, call SetConsoleOutputCP(65001) to set the console output character set to UTF-8.
SetConsoleOutputCP(65001);
Use UTF-8-encoded Characters:
Now you can use UTF-8-encoded characters within your printf statements. For example:
printf("Testing unicode -- English -- Ελληνικά -- Español -- Русский. aäbcdefghijklmnoöpqrsßtuüvwxyz\n");
Source File Encoding:
Project Character Set:
Console Font (Optional):
By following these steps, you can write a C console application that correctly displays UTF-8 encoded text on Windows. This enables you to print and display text in different languages and character sets, enhancing the user experience and internationalization of your application.
The above is the detailed content of How to Display UTF-8 Characters in a C Console Application on Windows?. For more information, please follow other related articles on the PHP Chinese website!