Samik
Samik

Reputation: 575

Why Unicode characters are not displayed properly in terminal with GCC?

I've written 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 it prints gibberish. unicodetest

I've checked:

Upvotes: 4

Views: 1254

Answers (1)

Connor Smith
Connor Smith

Reputation: 301

wprintf is a version of printf which takes a wide string as its format string, but otherwise behaves just the same: %c is still treated as char, not wchar_t. So instead you need to use %lc to format a wide character. And since your strings are ASCII you may as well use printf. For example:

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

Upvotes: 6

Related Questions