Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7860943
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T22:30:01+00:00 2026-06-02T22:30:01+00:00

I have a program that prints UTF-8 string to the console: #include <stdio.h> int

  • 0

I have a program that prints UTF-8 string to the console:

#include <stdio.h>

int main()
{
    printf("Мир Peace Ειρήνη\n");
    return 0;   
}

I configure the console to use True Type fonts (Lucida Console), define UTF-8 code-page (chcp 65001) compile this program with both MinGW GCC and Visual Studio 2010 it works perfectly, I see: the output:

Мир Peace Ειρήνη

I do the same using std::cout

#include <iostream>

int main()
{
    std::cout << "Мир Peace Ειρήνη\n" ;
    return 0;   
}

This works perfectly fine as above using MinGW GCC but with Visual Studio 2010
I get squares, more than that the squares (two per each non-ASCII letter).

If I run the program with redirection test >test.txt I get perfect UTF-8 output
in the file.

Both tests done on Windows 7.

Questions:

  1. What is the difference between printf and std::cout in the Visual Studio standard library in handling of the output stream – clearly one of them works and other does not?
  2. How can this be fixed?

Real Answer:

In short: you are screwed – std::cout does not really work with MSVC + UTF-8 – or at least
requires enormous effort to make it behave reasonably.

In long: read two articles referenced in the answer.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T22:30:09+00:00Added an answer on June 2, 2026 at 10:30 pm

    You have a number of flawed assumptions, lemme correct those first:

    • That things appear to work with g++ does not mean that g++ works correctly.

    • Visual Studio is not a compiler, it’s an IDE that supports many languages and compilers.

    • The conclusion that the Visual C++’s standard library needs to be fixed is correct, but the reasoning leading to that conclusion is wrong. Also g++ standard library needs to be fixed. Not to mention the g++ compiler itself.

    Now, Visual C++ has Windows ANSI, the encoding specified by the GetACP API function, as its undocumented C++ execution character set. Even if your source code is UTF-8 with BOM, narrow strings end up translated to Windows ANSI. If that, on your computer at the time of compilation, is a code page that includes all the non-ASCII characters, then OK, but otherwise the narrow strings will get garbled. The description of your test results is therefore seriously incomplete without mentioning the source code encoding and what your Windows ANSI codepage is.

    But anyway, “If I run the program with redirection test >test.txt I get perfect UTF-8 output in the file” indicates that what you’re up against is a bit of C++ level help from the Visual C++ runtime, where it bypasses the stream output and uses direct console output in order to get correct characters displayed in the console window.

    This help results in garbage when its assumptions, such as Windows ANSI encoded narrow string literals, don’t hold.

    It also means that the effect mysteriously disappears when you redirect the stream. The runtime library then detects that the stream goes to a file, and turns off the direct console output feature. You’re not guaranteed to then get the raw original byte values, but evidently you did, which was bad luck because it masked the problem.

    By the way, codepage 65001 in the console in Windows is not usable in practice. Many programs just crash. Including e.g. more.


    One way to get correct output is to use the Windows API level directly, with direct console output.

    Getting correct output with the C++ streams is much more complicated.

    It’s so complicated that there’s no room to describe it (correctly!) here, so I have to instead refer you to my 2-part blog article series about it: part 1 and part 2.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Consider this program: #include <stdio.h> int main() { printf(%s\n, __FILE__); return 0; } Depending
I have one application that prints messages from Test.exe in console .My java program
I have a (java) program that prints a line of hex numbers to stdout
I have a program that prints out the location of commas in a paragraph
I have a terribly uncomplicated test program that prints out the following numbers. i.e.
I have a program that executes various shell commands via system() and occasionally prints
I have a C program that prints every environmental variable, whose name is given
I have created a program that prints results on command line. (It is server
I have to write program that prints a truth table of expressions. So, I
I have a program that prints the cube and which can be rotated. Here's

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.