I am trying to print the euro sign on the console in the Gnome Terminal app through a c console application. My code is:
#include <stdio.h>
#include <wctype.h>
#include <wchar.h>
#include <locale.h>
int main (int argc, char* argv[]) {
setlocale(LC_CTYPE,"UTF-8");
wchar_t w_char1 = '€';
wprintf(L"%x\n", w_char1);
wprintf(L"%c\n", w_char1);
wchar_t w_char2=0xE282AC;
wprintf(L"%x\n", w_char2);
wprintf(L"%c\n", w_char2);
wchar_t w_char3='\u20AC';
wprintf(L"%x\n", w_char3);
wprintf(L"%c\n", w_char3);
}
This prints the following on the console:
e282ac
e282ac
e282ac
I've tried it both with and without setlocale. In the Gnome Terminal Preferences, under Compatibility, the Encoding is set as Unicode -- UTF-8.
Any ideas why the Euro symbol does not print?
This does not have a complete solution, but I think it provides some useful pointers towards a solution. It being midnight here, I need to go to bed.
The POSIX manual page for
wprintf()indicates that you need to use anlmodifier before thecconversion specifier:There's then a question of whether a
wchar_tconverts to awint_t— it probably does.I ran into a problem that I had to compile with
-Wno-multicharto suppress errors like:You also should check whether the
setlocale()operation works — it doesn't for me. I usedsetlocale(LC_ALL, "")and checked it; that worked. I am using a Mac, and haveLANG=en_us.UTF-8set in the environment. And I'm still not seeing the Euro symbol.Hmmm…
One of the rules of debugging when things are failing is "check every function that can report an error". This code does that, and the result is as shown:
Output (on my Mac):
Note that the error reporting function clears the error status on standard output (
clearerr(stdout)) and resetserrnoto zero — no standard C library function does that. Before I calledclearerr(), all the operations after the first reported failure.I think that it may be necessary to use
u8'€'or something similar. Check §6.4.4.4 Character constants and §6.4.5 String literals for more ideas.Twenty-four hours later.
This code is working moderately well:
The output it gives is:
Notice how the successful values all contain the U+20AC.