Printing a Unicode Symbol in C
Solution 1
Two problems: first of all, a wchar_t
must be printed with %lc
format, not %c
. The second one is that unless you call setlocale
the character set is not set properly, and you probably get ?
instead of your star. The following code seems to work though:
#include <stdio.h>
#include <wchar.h>
#include <locale.h>
int main() {
setlocale(LC_CTYPE, "");
wchar_t star = 0x2605;
wprintf(L"%lc\n", star);
}
And for ncurses
, just initialize the locale before the call to initscr
.
Solution 2
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 about wchar_t
(though it may use the same underlying printf
, this depends on the platform and configuration).
With ncurses, you would display a wchar_t
in any of these ways:
- storing it in an array of
wchar_t
, and usingwaddwstr
, or - storing it in a
cchar_t
structure (withsetcchar
), and usingwadd_wch
with that as a parameter, or - converting the
wchar_t
to a multibyte string, and usingwaddstr
Related videos on Youtube

Luke Collins
I like maths and computers, can play the piano and also do a bit of teaching.
Updated on August 10, 2020Comments
-
Luke Collins over 2 years
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.-
Luke Collins over 5 years@chux Well, I can print
a
with value0x0061
but notǎ
with value0x01ce
.
-
-
Luke Collins over 5 yearsThis fixed it, thanks! ★ Any ideas how to
mvwprint
this withncurses
though? -
Luke Collins over 5 yearsThanks for the reply. I keep getting an implicit declaration error for the wide-character
ncurses
functions, are there some other libraries I need to be including? -
Antti Haapala -- Слава Україні over 5 years
-
Thomas Dickey over 5 yearsYou're missing a
#define
. Generally that should be _XOPEN_SOURCE_EXTENDED, but most platforms have fubar'd ifdef's: for Linux just use
-D_GNU_SOURCE. You'll have to link with
-lncursesw`, but that's not implicit declaration -
Luke Collins over 5 yearscan you explain a bit more?
-
Thomas Dickey over 5 yearsIt would be defined using
ncursesw5-config --cflags
, or via a ".pc" file, depending on how ncurses is packaged for your system.