Why are Unicode characters displayed incorrectly in the terminal using GCC?

I wrote a small C program:

#include <stdio.h>
#include <stdlib.h>
#include <locale.h>

int main() {
    wprintf(L"%s\n", setlocale(LC_ALL, "C.UTF-8"));
    wchar_t chr = L'┐';
    wprintf(L"%c\n", chr);
}

      

Why doesn't this print the character

?

Instead, he is typing gibberish. unicodetest

I checked:

  • tried compiling without setlocale, same result
  • the terminal itself can print the character, I can copy it to the terminal from a text editor, this is gnome-terminal on Ubuntu
  • GCC version - 4.8.2
+3


source to share


1 answer


wprintf

is a version printf

that accepts a wide string as a format string, but otherwise behaves the same: %c

is still treated as char

, not wchar_t

. So you need to use %lc

wide character to format instead . And since your strings are ASCII, you can use printf

. For example:



int main() {
    printf("%s\n", setlocale(LC_ALL, "C.UTF-8"));
    wchar_t chr = L'┐';
    printf("%lc\n", chr);
}

      

+5


source







All Articles