Search code examples
cunicodeprintfchar16-t

How to find the built-in function to deal with char16_t in C?


Please tell what is the char16_t version for the String Manipulation Functions

such as:

http://www.tutorialspoint.com/ansi_c/c_function_references.htm

I found many references site, but no one mentioned that.

Especially for printing function, this is that most important, because it help me to verify whether the Manipulation function is work.

#include <stdio.h>
#include <uchar.h>



char16_t *u=u"α";
int main(int argc, char *argv[])
{
    printf("%x\n",u[0]); // output 3b1, it is UTF16



    wprintf("%s\n",u); //no ouput
    _cwprintf("%s\n",u); //incorrect output

    return 0;
}

Solution

  • wprintf and its wchar colleagues need to have th format string in wchar too: wprintf( L"%s\n", u);

    For wchar L is used as a prefix to the string literals.

    Edit:

    Here's a code snippet (tested on Windows):

    #include <stdio.h>
    #include <io.h>
    #include <fcntl.h>
    #include <wchar.h>
    
    void main()
    {
        wchar_t* a = L"α";
        fflush(stdout); //must be done before _setmode
        _setmode(_fileno(stdout), _O_U16TEXT); // set console mode to unicode
        wprintf(L"alpha is:\n\t%s\n", a);      // works for me :)
    }
    

    The console doesn't work in unicode and prints a "?" for non ascii chars. In Linux you need to remove the underscore prefix before setmode and fileno.

    Note: for windows GUI prints, there already proper support, so you can use wsprintf to format unicode strings.