分配非ASCII字符宽字符和打印的printf [英] Assigning non-ASCII characters to wide char and printing with printf

查看:225
本文介绍了分配非ASCII字符宽字符和打印的printf的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我该如何分配非ASCII字符到宽字符并将其打印到控制台?这code下不工作:

How can I assign non-ASCII characters to a wide char and print it to the console? This code down doesn't work:

#include <stdio.h>
int main(void)
{
    wchar_t wc = L'ć';
    printf("%lc\n", wc);
    printf("%ld\n", wc);
    return 0;
}

输出:

263
Press [Enter] to close the terminal ...

我使用Windows 7上的MinGW GCC。

I'm using MinGW GCC on Windows 7.

推荐答案

我觉得的printf您的来电()失败,并在返回«非法字节序列»误差错误号,至少是在这里发生的事情上的MacOS X与上面的例子中code(并且如果使用 wprintf()而不是的printf())。对我来说,它的工作原理,当我打电话的setlocale(LC_ALL,); 调用的printf(),使前停止默认情况下使用C语言环境:

I think your calls to printf() fail with an «Illegal byte sequence» error returned in errno, at least that is what happens here on MacOS X with the above example code (and also if using wprintf() instead of printf()). For me it works when I call setlocale(LC_ALL, ""); before the call to printf() so that it stops using the C locale by default:

#include <stdio.h>
#include <stdlib.h>
#include <locale.h>
int main(void)
{
    wchar_t wc = L'ć';

    setlocale(LC_ALL, "");
    printf("%lc\n", wc);

    return 0;
}

目前还不清楚你是什么样的平台/编译器,所以因人而异。

It is unclear what platform/compiler you are on, so YMMV.

这篇关于分配非ASCII字符宽字符和打印的printf的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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