user10587713
user10587713

Reputation:

How to print MACRON/Unicode in Windows Console

So currently I'm working with Unicode a lot lately. I've seen others facing the same issue but so far no answer solved my issue.

The objective is at the moment is to be able to print: ¯ in my Windows Console Window. The character is called "MACRON" and its Unicode number is U+00AF.

At first I simple wrote: cout << "some irrelevant text lorem ipsum etc... \u00AF" << endl;

But this ended up backfiring at me with the windows console window displaying a weird indescribable ugly (no offence) looking T.

I've also tried to use wcout wcout << L"some more irrelevant text lorem ipsum etc... \u00AF" << endl; but the outcome is the same.

Any thoughts as to why my source code/console window is unable to print the MACRON character?

A fix for this character is good but overall I'll encounter weirder and weirder Unicode character so I may need a broader applicable solution without downloading/changing anything outside of the source code.

Programming with C++ in Code::Blocks 17.12 IDE

Upvotes: 0

Views: 361

Answers (1)

Mike Vine
Mike Vine

Reputation: 9852

Use _setmode(..., _O_U16TEXT);

#include <io.h>
#include <fcntl.h>

...

_setmode(_fileno(stdout), _O_U16TEXT);
std::wcout << L"\u00AF\n";

Just make sure to read the caveats on the docs page to make sure you're happy with the trade-offs of using it (for example plain printf will no longer work in that mode):

https://learn.microsoft.com/en-us/cpp/c-runtime-library/reference/setmode?view=vs-2017

Upvotes: 1

Related Questions