4
votes

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:

  • tried compiling without setlocale, same result
  • the terminal itself can print the character, I can copy-paste it to terminal from text-editor, it's gnome-terminal on Ubuntu
  • GCC version is 4.8.2
1
In addition to the answer below regarding the proper printf format specifier, which appears to be the correct answer based on your output, you should also ensure that your source file is encoded using UTF-8 rather than something like ISO-8859-15. That sort of thing is why it's better to use L'\u2510' rather than L'┐'.user539810
With the correct format specifier the locale did make a difference, without it the these box-characters are falling back to their ASCII equivalent e.g. +, - and |.Samik

1 Answers

6
votes

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);
}