源代码:
#include "glib.h" #include <stdio.h> int main() { g_print("марко\n"); fprintf(stdout,"марко\n"); }
像这样构建:
gcc main.c -o main $(pkg-config glib-2.0 --cflags --libs)
你可以看到glib无法打印utf8和fprintf可以:
[marko@marko-work utf8test]$./main ????? марко
解决方法
您可以在大多数系统上通过环境变量设置正确的区域设置,也可以使用setlocale函数以编程方式执行.区域设置名称是系统相关的(不是POSIX标准的一部分),但在大多数系统上,以下内容将起作用:
#include <locale.h> : setlocale(LC_ALL,"en_US.utf8");
除了LC_ALL之外,您还可以仅为某些操作设置区域设置(例如,“en_US”将导致英文编号和日期格式,但您可能不希望以这种方式格式化数字/日期).要引用setlocale手册页:
LC_ALL Set the entire locale
generically.LC_COLLATE Set a locale for string
collation routines. This controls
alphabetic ordering in
strcoll() and strxfrm().LC_CTYPE Set a locale for the
ctype(3) and multibyte(3) functions.
This controls recognition of
upper and lower case,alphabetic or non-alphabetic
characters,and so on.LC_MESSAGES Set a locale for message
catalogs,see catopen(3) function.LC_MONETARY Set a locale for
formatting monetary values; this
affects the localeconv() function.LC_NUMERIC Set a locale for
formatting numbers. This controls the
formatting of decimal points in
input and output of floating point numbers in functions
such as printf() and scanf(),as
well as values returned by localeconv().LC_TIME Set a locale for
formatting dates and times using the
strftime() function.
所有系统上始终可用的唯一两个区域设置值是“C”,“POSIX”和“”.
Only three locales are defined by default: the empty string “” (which denotes the native environment) and the “C” and “POSIX” locales (which denote the C-language environment). A locale argument of NULL causes setlocale() to return the current locale. By default,C programs start in the “C” locale. The only function in the library that sets the locale is setlocale(); the locale is never changed as a side effect of some other routine.
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。