Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

unicode hello world for C?

Tags:

c

unicode

utf-8

I am trying to output things like 안, 蠀, ☃ from C

#include <wchar.h>
int main()
{
    fwprintf(stdout, L"안, 蠀, ☃\n");
    return 0;
}

output is ?, ?, ?

How do I print those characters?

Edit:

#include <wchar.h>
#include <locale.h>
int main()
{
    setlocale(LC_CTYPE, "");
    fwprintf(stdout, L"안, 蠀, ☃\n");
    return 0;
}

this did the trick. output is 안, 蠀, ☃ . except that the chinese character and snowman appears as box in my urxvt probably because I did not enable those locales.

$ locale -a
C
en_US
en_US.iso88591
en_US.iso885915
en_US.utf8
ja_JP.utf8
ko_KR
ko_KR.euckr
ko_KR.utf8
korean
korean.euc
POSIX
zh_CN.utf8

which locale do I have to enable additionally so that it'll display chinese character and snowman? maybe do I need font?

will the above program work on Windows?

like image 222
numeric Avatar asked Apr 24 '09 21:04

numeric


1 Answers

You have to set your output terminal as Unicode compatible.

On Linux (with Bash shell), try:

$ LANG=en.UTF-8

and also make sure that your terminal emulator can actually display Unicode and is configured to do so.

like image 54
Alnitak Avatar answered Oct 06 '22 06:10

Alnitak