Unicode strings with printf

Useful tip for other techies out there: I just discovered that printf(”%ls”, (wchar_t pointer goes here)) prints out wide strings. Nice! At least, it does for MinGW.

2 Responses to “Unicode strings with printf”

  1. Day Barr Says:

    I believe also that the format string “%S” is synonymous with “%ls”.

    In Microsoft’s runtime library at least, wprintf(”%S”, …) behaves symmetrically and will print a single-byte-character string into the resulting wide string output.

    If you’ve ever had the misfortune to work with TCHAR strings, then you may see a dilema here:

    _tprintf(_T(”%s”), ptr) allows you put a TCHAR* into the resulting TCHAR output. So what is the type of ptr in the expression _tprintf(_T(”%S”), ptr) ?

    !TCHAR ?

  2. Day Barr Says:

    That should of course be

    wprintf(L”%S”, …)

    and not

    wprintf(”%S”, …)

    Enough geekery!

Leave a Reply