Question

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?

Was it helpful?

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.

OTHER TIPS

It is correct that %ls the standard way to use wide strings, and %s is the standard way to use narrow strings. I don't know about newlib, but Microsoft's wprintf, which also incorrectly treats %s as meaning a wide string, accepts %hs to explicitly specify a narrow string. It also works in glibc because the h is ignored. If you are targeting MSVC at all, you might want to use %hs.

Source: http://en.chys.info/2009/06/wprintfs/

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top