wprintf, fwprintf, swprintf, wprintf_s, fwprintf_s, swprintf_s, snwprintf_s
Defined in header <wchar.h> | ||
---|---|---|
(1) | ||
int wprintf( const wchar_t *format, ... ); | (since C95) (until C99) | |
int wprintf( const wchar_t *restrict format, ... ); | (since C99) | |
(2) | ||
int fwprintf( FILE *stream, const wchar_t* format, ... ); | (since C95) (until C99) | |
int fwprintf( FILE *restrict stream, const wchar_t *restrict format, ... ); | (since C99) | |
(3) | ||
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, ...); | (4) | (since C11) |
int fwprintf_s( FILE *restrict stream, const wchar_t *restrict format, ...); | (5) | (since C11) |
int swprintf_s( wchar_t *restrict buffer, rsize_t bufsz, const wchar_t* restrict format, ...); | (6) | (since C11) |
int snwprintf_s( wchar_t * restrict s, rsize_t n, const wchar_t * restrict format, ...); | (7) | (since C11) |
Loads the data from the given locations, converts them to wide string equivalents and writes the results to a variety of sinks.
stdout
.stream
.bufsz
is greater than zero, writes the results to a wide string buffer
. At most bufsz-1
wide characters are written followed by null wide character. If bufsz
is zero, nothing is written (and buffer
may be a null pointer), however the return value (number of wide characters that would be written) is still calculated and returned.- the conversion specifier
%n
is present informat
- any of the arguments corresponding to
%s
is a null pointer -
format
orbuffer
is a null pointer -
bufsz
is zero or greater thanRSIZE_MAX/sizeof(wchar_t)
- encoding errors occur in any of string and character conversion specifiers
- (only for
swprintf_s
) the number of wide characters to be written, including the null, would exceedbufsz
.
wprintf_s
, fwprintf_s
, swprintf_s
, and snwprintf_s
are only guaranteed to be available if __STDC_LIB_EXT1__
is defined by the implementation and if the user defines __STDC_WANT_LIB_EXT1__
to the integer constant 1 before including stdio.h
.Parameters
stream | - | output file stream to write to | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
buffer | - | pointer to a wide character string to write to | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
bufsz | - | up to bufsz-1 wide characters may be written, plus the null terminator |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
format | - | pointer to a null-terminated wide string specifying how to interpret the data. The format string consists of ordinary wide characters (except
The following format specifiers are available:
The floating point conversion functions convert infinity to Not-a-number is converted to The conversions Even though The correct conversion specifications for the fixed-width character types ( The memory-writing conversion specifier There is a sequence point after the action of each conversion specifier; this permits storing multiple If a conversion specification is invalid, the behavior is undefined. |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
... | - | arguments specifying data to print. If any argument after default argument promotions is not the type expected by the corresponding conversion specifier, or if there are fewer arguments than required by format , the behavior is undefined. If there are more arguments than required by format , the extraneous arguments are evaluated and ignored |
Return value
size
(including when size
is zero).buffer
. Returns a negative value on encoding errors and on overflow. Returns zero on all other errors.buffer
had bufsz
been sufficiently large, or a negative value if an error occurs. (meaning, write was successful and complete only if the return is nonnegative and less than bufsz
)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 C11's snwprintf_s), 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); }
Output:
Converted from UTF-8: 'zß水????'
References
- C11 standard (ISO/IEC 9899:2011):
- 7.29.2.1 The fwprintf function (p: 403-410)
- 7.29.2.3 The swprintf function (p: 416)
- 7.29.2.11 The wprintf function (p: 421)
- K.3.9.1.1 The fwprintf_s function (p: 628)
- K.3.9.1.4 The swprintf_s function (p: 630-631)
- K.3.9.1.13 The wprintf_s function (p: 637-638)
- C99 standard (ISO/IEC 9899:1999):
- 7.24.2.1 The fwprintf function (p: 349-356)
- 7.24.2.3 The swprintf function (p: 362)
- 7.24.2.11 The wprintf function (p: 366)
See also
(C99)(C11)(C11)(C11)(C11) | prints formatted output to stdout , a file stream or a buffer (function) |
(C95)(C95)(C95)(C11)(C11)(C11)(C11) | prints formatted wide character output to stdout , a file streamor a buffer using variable argument list (function) |
(C95) | writes a wide string to a file stream (function) |
© cppreference.com
Licensed under the Creative Commons Attribution-ShareAlike Unported License v3.0.
http://en.cppreference.com/w/c/io/fwprintf