Home > Backend Development > C++ > How to Print UTF-8 from C Console Applications on Windows?

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

Patricia Arquette
Release: 2024-11-28 21:42:15
Original
766 people have browsed it

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 <cstdio>
#include <windows.h>

#pragma execution_character_set("utf-8")

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

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.

The above is the detailed content of How to Print UTF-8 from C Console Applications on Windows?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template