Assigning non-ASCII characters to wide char and printing with printf

浪子不回头ぞ 提交于 2019-12-06 02:32:09

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.

You should use wprintf to print wide-character strings:

wprintf(L"%c\n", wc);

use wprintf("%lc\n" ,wc); and you will get your desired output

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!