Search code examples
cunicodeprintfglibcnewlib

Varying behavior of "%s" in wprintf() in different libc implementations


I've noticed that while

#include <stdio.h>
wprintf( L"Hello, %s\n", "world" );

works with standard glibc under Linux, the same thing does not work the same way with newlib. It seems newlib expects wide string for "%s" there, so the following works instead:

wprintf( L"Hello, %s\n", L"world" );

Which behavior is the correct one? Is this a bug in newlib? Also, is there a way to explicitly specify that I want a "narrow" string, not a wide one in the printf string format specifier?


Solution

  • The correct format specifier for wide strings (wchar_t *) is %ls, for normal, 'narrow' C strings (char *) is %s. The implementation of glibc is correct.