How I can print the wchar_t values to console?

Example:

#include <iostream>

using namespace std;

int main()
{
    wchar_t en[] = L"Hello";
    wchar_t ru[] = L"Привет"; //Russian language
    cout << ru
         << endl
         << en;
    return 0;
}

This code only prints HEX-values like adress. How to print the wchar_t string?


Edit: This doesn’t work if you are trying to write text that cannot be represented in your default locale. :-(

Use std::wcout instead of std::cout.

wcout << ru << endl << en;

Can I suggest std::wcout ?

So, something like this:

std::cout << "ASCII and ANSI" << std::endl;
std::wcout << L"INSERT MULTIBYTE WCHAR* HERE" << std::endl;

You might find more information in a related question here.


You cannot portably print wide strings using standard C++ facilities.

Instead you can use the open-source {fmt} library to portably print Unicode text. For example (https://godbolt.org/z/nccb6j):

#include <fmt/core.h>

int main() {
  const char en[] = "Hello";
  const char ru[] = "Привет";
  fmt::print("{}\n{}\n", ru, en);
}

prints

Привет
Hello

This requires compiling with the /utf-8 compiler option in MSVC.

For comparison, writing to wcout on Linux:

wchar_t en[] = L"Hello";
wchar_t ru[] = L"Привет";
std::wcout << ru << std::endl << en;

may transliterate the Russian text into Latin (https://godbolt.org/z/za5zP8):

Privet
Hello

This particular issue can be fixed by switching to a locale that uses UTF-8 but a similar problem exists on Windows that cannot be fixed just with standard facilities.

Disclaimer: I'm the author of {fmt}.