Print UTF-8 multibyte character in C

感情迁移 提交于 2021-02-05 10:31:55

问题


I wrote this code to print a UTF-8 multibyte string. But it does not print properly. Note: I am doing it in a Linux system.

#include <stdio.h>
#include <locale.h>

int main()
{
    char *locale = setlocale(LC_ALL, "");
    printf("\n locale =%s\n", locale);
    printf("test\n \x263a\x263b Hello from C\n", locale);

    return 0;
}

回答1:


Use \u instead of \x:

#include <stdio.h>
#include <locale.h>

int main()
{
    char *locale = setlocale(LC_ALL, "");
    printf("\n locale =%s\n", locale);
    printf("test\n \u263a\u263b Hello from C\n");

    return 0;
}

This runs and produces the following output:

$ gcc foo.c
$ ./a.out 

 locale =C
test
 ☺☻ Hello from C


来源:https://stackoverflow.com/questions/58039815/print-utf-8-multibyte-character-in-c

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