Home > Article > Backend Development > How to Output UTF-8 Strings Correctly to `std::cout` on Windows?
Dealing with UTF-8 Strings in C on Windows
Encoding strings as UTF-8 is widely used for cross-platform applications. However, outputting UTF-8 strings to std::cout on Windows presents unique challenges.
The default behavior on Windows is for std::cout to expect strings in non-Unicode formats. When presented with UTF-8 strings, it displays corrupted characters.
To address this issue, there are two primary steps:
Here's a revised code snippet that incorporates these solutions:
<code class="cpp">#include <string> #include <iostream> #include <Windows.h> #include <cstdio> int main() { // Set console code page to UTF-8 SetConsoleOutputCP(CP_UTF8); // Enable buffering to prevent byte-by-byte transmission setvbuf(stdout, nullptr, _IOFBF, 1000); // Output UTF-8 string std::string test = u8"Greek: αβγδ; German: Übergrößenträger"; std::cout << test << std::endl; return 0; }</code>
In addition to these steps, note that raster fonts in the Windows console may not display non-ASCII Unicode characters correctly. To enable proper rendering, it's recommended to switch to a TrueType font, which is now the default in Windows 10 and later versions.
The above is the detailed content of How to Output UTF-8 Strings Correctly to `std::cout` on Windows?. For more information, please follow other related articles on the PHP Chinese website!