1

How to use char16_t and char32_t to display in C++20 (using IDE:: Eclipse IDE verion 2021)?

 // C++ program to illustrate

 #include <iostream>
 using namespace std;

 int main()
 {
    char16_t value = u'猫';
    cout << " My Character " << value ;
    return 0;
 }
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
  • 1
    This depends on what output device your program is connected to via `std::cout`. Usually it's a terminal so it would depend on how the terminal is configured. – Galik Nov 20 '21 at 03:33
  • 2
    Have a look at [C++ unicode characters printing](https://stackoverflow.com/questions/16944750/c-unicode-characters-printing) – Wyck Nov 20 '21 at 03:34

1 Answers1

4

There are no standard output streams for unicode char types. Standard output streams exist only for narrow native character type char and the wide native character type wchar_t. You will have to convert the char16_t and char32_t (and char8_t) strings to one of those types in order to meaningfully write to the standard output stream.

Due to lack of standard output stream support, char16_t and char32_t (and char8_t) are only really useful for writing into files.

Both narrow and wide output native character encoding - which are used by the standard streams - may be but aren't necessarily unicode. That depends on the system configuration. In case the native encoding isn't unicode, that encoding won't necessarily be able to represent the unicode string that you wish to display.

Here is a way to display 猫 without using char16_t nor char32_t with the precondition that the compiler and the system support unicode as the narrow native encoding:

std::cout << "猫";

See it here.

eerorika
  • 232,697
  • 12
  • 197
  • 326
  • 1
    `std::cout << "猫";` will work only if the source file, the compiler, and the terminal are all using UTF-8, which is not always the case. – Remy Lebeau Nov 20 '21 at 05:44