Printing a Unicode Symbol in C

泄露秘密 提交于 2019-11-29 11:34:19

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

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 using waddwstr, or
  • storing it in a cchar_t structure (with setcchar), and using wadd_wch with that as a parameter, or
  • converting the wchar_t to a multibyte string, and using waddstr
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!