I'm trying to print a unicode star character (0x2605) in a linux terminal using C. I've followed the syntax suggested by other answers on the site, but I'm not getting an output:
#include <stdio.h>
#include <wchar.h>
int main(){
wchar_t star = 0x2605;
wprintf(L"%c\n", star);
return 0;
}
I'd appreciate any suggestions, especially how I can make this work with the ncurses
library.
Two problems: first of all, a
wchar_t
must be printed with%lc
format, not%c
. The second one is that unless you callsetlocale
the character set is not set properly, and you probably get?
instead of your star. The following code seems to work though:Whether you are using stdio or ncurses, you have to initialize the locale, as noted in the ncurses manual. Otherwise, multibyte encodings such as UTF-8 do not work.
wprintw
doesn't necessarily know aboutwchar_t
(though it may use the same underlyingprintf
, this depends on the platform and configuration).With ncurses, you would display a
wchar_t
in any of these ways:wchar_t
, and usingwaddwstr
, orcchar_t
structure (withsetcchar
), and usingwadd_wch
with that as a parameter, orwchar_t
to a multibyte string, and usingwaddstr