如何将非 ASCII 字符分配给宽字符并将其打印到控制台?这段代码不起作用:
#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。
您应该使用wprintf
打印宽字符串:
wprintf(L"%c\n", wc);
我认为你的调用printf()
失败并返回一个«Illegal byte sequence»错误errno
,至少这是在 MacOS X 上使用上面的示例代码(以及如果使用wprintf()
而不是printf()
)发生的情况。对我来说,当我在调用setlocale(LC_ALL, "");
之前调用它时它会起作用,printf()
以便它默认停止使用 C 语言环境:
#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;
}
目前还不清楚你在哪个平台/编译器上,所以 YMMV。
使用 wprintf("%lc\n" ,wc); 你会得到你想要的输出