2010年1月10日

Unicode C Example

Unicode Consortium
如何用GCC編譯UTF8檔能正確顯示中文
//-- test_unicode.c file encoding=UTF-8 --
#include <stdio.h>
#include <stdlib.h>
#include <stddef.h>
#include <locale.h>
#include <wctype.h>
#include <wchar.h>

int main(void) {
setlocale(LC_ALL,"");

putwchar(0x6587); // 文 UTF-16BE: 6587
putwchar(L'字');
wchar_t *wstr = L"中文, アプンノ, 평화";
wprintf(L"\n-> %ls 字串長度 %d\n", wstr, wcslen(wstr));
return EXIT_SUCCESS;
}
/*
$ gcc -Wall -std=c99 test_unicode.c -otest_unicode
$ ./test_unicode
文字
-> 中文, アプンノ, 평화 字串長度 12
*/

沒有留言:

網誌存檔