0

I wrote two versions of a little program in C++ with MSVC on Windows 11:

First one:

#include <iostream>
#include <Windows.h>
int main()
{
 SetConsoleOutputCP(CP_UTF8);
 std::cout << u8"ä ü ö \n";
}

That did not work. But the following one did work:

#include <iostream>
#include <Windows.h>
int main()
{
 SetConsoleOutputCP(CP_UTF8);
 std::cout << "ä ü ö \n";
}

The difference was the u8 in front of the string literal.

Why did it not work? With "did not work" I mean, that the output on the console was not "ä ü ö" but other symbols.

asked Nov 12 at 21:19
11
  • 4
    Please describe what actually happened in the first case. "did not work" could mean anything from doing nothing to blasting away half the universe. Commented Nov 12 at 21:32
  • Do you mean that it actually compiled std::cout << u8"ä ü ö \n";? That's surprising. Must be an MSVC extension. Commented Nov 12 at 21:38
  • Print numerical values of individual octets of the two string literals. This should prove illuminating. Commented Nov 12 at 21:50
  • 2
    @Tim what charset is the .cpp file itself saved as? If you do not specify /utf-8 during compiling then DO NOT save the file as UTF-8. But if your file IS saved as UTF-8 then you need /utf-8. Read up about your compiler's Source Charset and Execution Charset options. They affect how char strings are interpreted at compile-time and run-time, respectively. Also, your /D defines for Unicode only affect TCHAR-based strings+macros, none of which you are using in your example. Commented Nov 12 at 22:33
  • 1
    @JohnBollinger I was thinking of the std::ostream& operator<<(std::ostream&, const char8_t*) overload required. It's been deleted since C++20 but I see that OP mentions using C++14 in the comments, so that clears that up. :-) Commented Nov 13 at 10:48

0

Know someone who can answer? Share a link to this question via email, Twitter, or Facebook.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.