Search code examples
cunicodewchar-t

How can I store a unicode in C?


I am trying to store a Unicode codepoint inside a variable in C. I tried using wchar_t, however since the Unicode codepoint I am trying to store is U+1F319, it doesn't fit in wchar_t. How can I get around this? I'm using a Windows computer.

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

int main(void){

    setlocale(LC_ALL,"en_US.UTF-8");

    unsigned long long x = 0x1F319;
    wchar_t wc =L'\U0001f319';
    wprintf(L"%lc",wc);

    return EXIT_SUCCESS;
}

The following code gives this error:

Unicode.c:12:14: warning: character constant too long for its type
wchar_t wc =L'\U0001f319';


Solution

  • How can I store a unicode in C?

    Since C11, "to store a Unicode codepoint", use char32_t @Shawn

    #include <uchar.h>
    
    char32_t ch1 = 0x1F319;
    char32_t ch2 = U'\U0001f319';
    

    Works on my Windows computer. ref


    char32_t

    which is an unsigned integer type used for 32-bit characters and is the same type as uint_least32_t... C11 §7.27 2