Search code examples
cutf-16windows-console

What locale LC_CTYPE is used for Windows unicode console app?


While converting a multi-byte console application to Unicode, I ran up against a weird problem where _tcprintf and WriteConsole worked fine but _tprintf was printing the wrong characters...

I've traced it back to using setlocale(LC_ALL, "C") which uses LC_CTYPE of 1 byte based on MS doc:

The C locale assumes that all char data types are 1 byte and that their value is always less than 256.

However, I want to keep "C" for everything except the LC_CTYPE but I don't know what to use?

I thought the whole point of using UTF16 is that all the characters are available and things would print properly no matter the code page or locale.

Although it also appears setting the console output to UTF-8 (65001) (SetConsoleCP which of course is separate from the locale) in a Unicode app and outputting UTF16 also has problems displaying the correct characters.

Anyway, does anyone know what value I should be using the LC_CTYPE for UTF16 on Windows Unicode Console Application? Maybe it's as easy as setlocale( LC_CTYPE, "" ); ? TIA!!


Solution

  • Use _setmode() to set the file translation mode to _O_U16TEXT:

    #include <fcntl.h>
    #include <io.h>
    #include <stdio.h>
    
    int main(void)
    {
        _setmode(_fileno(stdout), _O_U16TEXT);
        wprintf(L"ελληνικά\n");
    }