wprintf, fwprintf, swprintf, wprintf_s, fwprintf_s, swprintf_s, snwprintf_s

Header: <wchar.h>

Loads the data from the given locations, converts them to wide string equivalents and writes the results to a variety of sinks.

# Declarations

int wprintf( const wchar_t* format, ... );

(since C95) (until C99)

int wprintf( const wchar_t* restrict format, ... );

(since C99)

int fwprintf( FILE* stream, const wchar_t* format, ... );

(since C95) (until C99)

int fwprintf( FILE* restrict stream,
const wchar_t* restrict format, ... );

(since C99)

int swprintf( wchar_t* buffer, size_t bufsz,
const wchar_t* format, ... );

(since C95) (until C99)

int swprintf( wchar_t* restrict buffer, size_t bufsz,
const wchar_t* restrict format, ... );

(since C99)

int wprintf_s( const wchar_t* restrict format, ... );

(since C11)

int fwprintf_s( FILE* restrict stream,
const wchar_t* restrict format, ... );

(since C11)

int swprintf_s( wchar_t* restrict buffer, rsize_t bufsz,
const wchar_t* restrict format, ... );

(since C11)

int snwprintf_s( wchar_t* restrict s, rsize_t n,
const wchar_t* restrict format, ... );

(since C11)

# Parameters

# Notes

While narrow strings provide snprintf, which makes it possible to determine the required output buffer size, there is no equivalent for wide strings(until snwprintf_s)(since C11), and in order to determine the buffer size, the program may need to call swprintf, check the result value, and reallocate a larger buffer, trying again until successful.

snwprintf_s, unlike swprintf_s, will truncate the result to fit within the array pointed to by buffer, even though truncation is treated as an error by most bounds-checked functions.

# Example

#include <locale.h>
#include <wchar.h>
 
int main(void)
{
    char narrow_str[] = "z\u00df\u6c34\U0001f34c";
                  // or "zß水🍌"
                  // or "\x7a\xc3\x9f\xe6\xb0\xb4\xf0\x9f\x8d\x8c";
    wchar_t warr[29]; // the expected string is 28 characters plus 1 null terminator
    setlocale(LC_ALL, "en_US.utf8");
    swprintf(warr, sizeof warr / sizeof* warr,
             L"Converted from UTF-8: '%s'", narrow_str);
    wprintf(L"%ls\n", warr);
}

# See also