如何将非ascii字符分配给宽字符并将其打印到控制台?下面的代码不能工作:
#include <stdio.h>
int main(void)
{
wchar_t wc = L'ć';
printf("%lcn", wc);
printf("%ldn", wc);
return 0;
}
输出:263
Press [Enter] to close the terminal ...
我在Windows 7上使用MinGW GCC
您应该使用wprintf
打印宽字符字符串:
wprintf(L"%cn", wc);
我认为你对printf()
的调用失败了errno
中返回的«非法字节序列»错误,至少这是在MacOS X上使用上述示例代码发生的情况(并且如果使用wprintf()
而不是printf()
)。对我来说,当我在调用printf()
之前调用setlocale(LC_ALL, "");
时,它可以工作,以便它默认情况下停止使用C语言环境:
#include <stdio.h>
#include <stdlib.h>
#include <locale.h>
int main(void)
{
wchar_t wc = L'ć';
setlocale(LC_ALL, "");
printf("%lcn", wc);
return 0;
}
不清楚你在哪个平台/编译器上,所以YMMV.
使用wprintf("% lc n",wc);你会得到你想要的输出