I am working for several days with unicode in C++ now and it is very unclear for me. I have a few questions about its usage and I would be happy if they could be answered. The goal is simply that the output is the string with the proper unicode.
As far as I understood, � is put out when the char is broken. Like when you try to cast a wchat_t to a char.
About my machine OS: kubuntu 19.10
g++ --version
g++ (Ubuntu 9.2.1-9ubuntu2) 9.2.1 20191008
Copyright (C) 2019 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
1. Why does this work as std::string should only be capable of storing chars which "é" is not?
setlocale(LC_ALL, "en_US.utf8");
std::cout << "é" << std::endl;
output: é
2. Printing a wchar_t is very strange. Why is the following output as it is?
setlocale(LC_ALL, "en_US.utf8");
wchar_t a = L'é';
std::cout << a << std::endl;
output: 233
setlocale(LC_ALL, "en_US.utf8");
wchar_t a = L'é';
std::wcout << a << std::endl;
output: �
setlocale(LC_ALL, "en_US.utf8");
wchar_t a = L'é';
printf("%lc\n", a);
output: é
setlocale(LC_ALL, "en_US.utf8");
wchar_t a = L'é';
wprintf(L"%lc\n", a);
output: é
PS: setlocale(LC_ALL, "en_US.utf8") is there as suggested by this source. Otherwise, std::wcout would print question marks instead of the proper chars.
"é"is not astd::stringbut a string literal, which is of typeconst char[N]- NathanOliver