San
San

Reputation: 315

Print UTF-8 multibyte character in C

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;
}

Upvotes: 0

Views: 510

Answers (1)

Tim
Tim

Reputation: 4948

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

Upvotes: 1

Related Questions