1

I am to print some unicode (4 digit Hex) characters in C.

Those characters I've stored in some short int variables. The following is the code I'm supposed to use for my purpose :

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

int main(void) {
    
    setlocale(LC_ALL,"");

    short a = 0x099A, b = 0x09BE;
    wchar_t *string1 = (wchar_t *) calloc(20, sizeof(wchar_t));
    sprintf(string1, "\\u%04x\\u%04x", a, b);
    printf(" %s ", string1);

    wchar_t *string2 = (wchar_t *) calloc(20, sizeof(wchar_t));
    strcpy(string2, (wchar_t *) "\u099A\u09BE");
    printf(" %s \n", string2);

    return 0;
}

Now the problem is:

although string2 is showing the corect output in terminal, string1 isn't.

But I must use the 1st approach, i.e I have the unicode characters stored in some arbitrary variables & I need a way to get them printed on screen.

Any suggestion shall be keenly appreciated.

Community
  • 1
  • 1
  • 4
    Portability note: There is no guarantee that a `wchar_t` is 4-byte long or can store Unicode code points. – kennytm Aug 18 '12 at 09:13

1 Answers1

3

For printing Unicode characters stored in variables, why not convert them to wchar_t and the use e.g. wprintf?

wchar_t ac = a;
wchar_t bc = b;

wprintf(L"%c%c", ac, bc);
Some programmer dude
  • 400,186
  • 35
  • 402
  • 621