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

查看:60
本文介绍了在 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
", star);

    return 0;
}

我很感激任何建议,尤其是我如何使用 ncurses 库来完成这项工作.

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

推荐答案

两个问题:首先,一个wchar_t必须用%lc格式打印,而不是<代码>%c.第二个是除非你调用 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
", star);
}

对于ncurses只需在调用之前初始化语言环境<代码>initscr.

And for ncurses, just initialize the locale before the call to initscr.

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

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