打印wchar到Linux控制台?

编程入门 行业动态 更新时间:2024-10-24 10:16:59
本文介绍了打印wchar到Linux控制台?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我的C程式贴在下面。在bash中,程序打印char is,不打印Ω。我的区域设置都是en_US.utf8。

My C program is pasted below. In bash, the program print "char is ", Ω is not printed. My locale are all en_US.utf8.

#include <stdio.h> #include <wchar.h> #include <stdlib.h> int main() { int r; wchar_t myChar1 = L'Ω'; r = wprintf(L"char is %c\n", myChar1); }

推荐答案

显然,编译器将omega从UTF-8转换为UNICODE,但不知何故,libc会把它弄乱。

This was quite interesting. Apparently the compiler translates the omega from UTF-8 to UNICODE but somehow the libc messes it up.

首先:%c -format说明符需要 char (即使在 wprintf -version),所以你必须指定%lc (因此%ls 字符串)。

First of all: the %c-format specifier expects a char (even in the wprintf-version) so you have to specify %lc (and therefore %ls for strings).

其次,如果你运行你的代码,语言环境设置为 C 环境)。您必须致电 setlocale 空字符串从环境中获取语言环境,所以libc再次很高兴。

Secondly if you run your code like that the locale is set to C (it isn't automatically taken from the environment). You have to call setlocale with an empty string to take the locale from the environment, so the libc is happy again.

#include <stdio.h> #include <wchar.h> #include <stdlib.h> #include <locale.h> int main() { int r; wchar_t myChar1 = L'Ω'; setlocale(LC_CTYPE, ""); r = wprintf(L"char is %lc (%x)\n", myChar1, myChar1); }

更多推荐

打印wchar到Linux控制台?

本文发布于:2023-11-28 09:05:47,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1641784.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:控制台   wchar   Linux

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!