在C中打印Unicode符号 [英] Printing a Unicode Symbol in C

查看:165
本文介绍了在C中打印Unicode符号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试打印Unicode星号( 0x2605 )在使用C的linux终端中.我遵循了网站上其他答案所建议的语法,但是没有得到输出:

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

我希望您能提出任何建议,尤其是如何使用ncurses库来完成这项工作.

I'd appreciate any suggestions, especially how I can make this work with the ncurses library.

推荐答案

两个问题:首先,必须以%lc格式而不是%c打印wchar_t.第二个问题是,除非调用setlocale,否则字符集设置不正确,并且可能会得到?而不是星号.但是,以下代码似乎可以正常工作:

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

这篇关于在C中打印Unicode符号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆