"If a worker wants to do his job well, he must first sharpen his tools." - Confucius, "The Analects of Confucius. Lu Linggong"
Front page > Programming > How to Print UTF-8 from C++ Console Applications on Windows?

How to Print UTF-8 from C++ Console Applications on Windows?

Published on 2024-12-22
Browse:490

How to Print UTF-8 from C   Console Applications on Windows?

Printing UTF-8 from C Console Applications on Windows

When developing C console applications on English Windows systems using Visual Studio 2008, users may encounter challenges in displaying UTF-8 encoded content correctly through cout or wcout. Here's how to overcome this issue:

Solution:

The solution involves setting the console's output code page to support UTF-8. Follow these steps:

  1. Include and header files.
  2. Use the #pragma execution_character_set directive to set the execution character set to UTF-8.
  3. In the main function, call SetConsoleOutputCP(65001) to change the output code page to UTF-8.
  4. Utilize printf to output UTF-8 encoded text.

Code Example:

#include 
#include 

#pragma execution_character_set("utf-8")

int main()
{
    SetConsoleOutputCP(65001);
    printf("Testing unicode -- English -- Ελληνικά -- Español -- Русский. aäbcdefghijklmnoöpqrsßtuüvwxyz\n");
}

Additional Tips:

  • Save the source file as Unicode (UTF-8 with signature) - Codepage 65001.
  • Set Project -> Properties -> Configuration Properties -> General -> Character Set to Use Unicode Character Set.
  • While changing the console font to Lucida Console may be suggested, it is not necessary for proper display in this case.
Latest tutorial More>

Disclaimer: All resources provided are partly from the Internet. If there is any infringement of your copyright or other rights and interests, please explain the detailed reasons and provide proof of copyright or rights and interests and then send it to the email: [email protected] We will handle it for you as soon as possible.

Copyright© 2022 湘ICP备2022001581号-3