std::vwprin...d::vswprintf(3)
π NAME π
std::vwprintf, std::vfwprintf, std::vswprintf β the wide-character printf trio, all three of them, no skips, no cap π«Ά
(this is a page straight out of the C++ Standard Library, section 3, and it's giving "printf but make it πwideπ" energy. obsessed.)
β¨ SYNOPSIS β¨
okay so first things first, bestie: these live in the <cwchar> header. here's the whole squad:
int vwprintf( const wchar_t* format, va_list vlist );int vfwprintf( std::FILE* stream, const wchar_t* format, va_list vlist );int vswprintf( wchar_t* buffer, std::size_t buf_size, const wchar_t* format, va_list vlist );
π DESCRIPTION π
so what's the vibe? it loads the data from locations defined by vlist, converts them into wide string equivalents, and writes the results out to a variety of sinks. literally the whole job π
- Writes the results to
stdout. π₯οΈ - Writes the results to a file stream
stream. π - Writes the results to a wide string buffer. At most
size-1wide characters are written followed by a null wide character. π§Ί
π¦ PARAMETERS π¦
streamβ output wide stream to write to πbufferβ pointer to a wide string to write to π§Ίbuf_sizeβ maximum number of wide characters to write πformatβ pointer to a null-terminated wide string specifying how to interpret the data πΊοΈvlistβ variable argument list containing the data to print. π
π the format string, explained π
the format string is made of ordinary wide characters (everything except %), which get copied unchanged into the output stream, plus conversion specifications. every conversion specification is built like this:
- introductory
%character. - (optional) one or more flags that modify the behavior of the conversion:
-: the result of the conversion is left-justified within the field (by default it is right-justified).+: the sign of signed conversions is always prepended to the result of the conversion (by default the result is preceded by minus only when it is negative).- space: if the result of a signed conversion does not start with a sign character, or is empty, space is prepended to the result. It is ignored if
+flag is present. #: alternative form of the conversion is performed. See the table below for exact effects otherwise the behavior is undefined. β οΈ0: for integer and floating-point number conversions, leading zeros are used to pad the field instead of space characters. For integer numbers it is ignored if the precision is explicitly specified. For other conversions using this flag results in undefined behavior. It is ignored if-flag is present. π
- (optional) integer value or
*that specifies minimum field width. The result is padded with space characters (by default), if required, on the left when right-justified, or on the right if left-justified. In the case when*is used, the width is specified by an additional argument of typeint, which appears before the argument to be converted and the argument supplying precision if one is supplied. If the value of the argument is negative, it results with the-flag specified and positive field width (Note: This is the minimum width: The value is never truncated.). π - (optional)
.followed by integer number or*, or neither that specifies precision of the conversion. In the case when*is used, the precision is specified by an additional argument of typeint, which appears before the argument to be converted, but after the argument supplying minimum field width if one is supplied. If the value of this argument is negative, it is ignored. If neither a number nor*is used, the precision is taken as zero. See the table below for exact effects of precision. π - (optional) length modifier that specifies the size of the argument (in combination with the conversion format specifier, it specifies the type of the corresponding argument).
- conversion format specifier.
π format specifiers (the main character list) π
The following format specifiers are available:
%β Writes literal%. The full conversion specification must be%%.- argument types:
hhN/A,hN/A, (none) N/A,lN/A,llN/A,jN/A,zN/A,tN/A,LN/A
- argument types:
cβ Writes a single character. The argument is first converted towchar_tas if by callingbtowc. If thelmodifier is used, thewint_targument is first converted towchar_t.- argument types:
hhN/A,hN/A, (none)int,lwint_t,llN/A,jN/A,zN/A,tN/A,LN/A
- argument types:
sβ Writes a character string. The argument must be a pointer to the initial element of a character array containing a multibyte character sequence beginning in the initial shift state, which is converted to wide character array as if by a call tombrtowcwith zero-initialized state. Precision specifies the maximum number of wide characters to be written. If Precision is not specified, writes every wide characters up to and not including the first null terminator. If thelspecifier is used, the argument must be a pointer to the initial element of an array ofwchar_t.- argument types:
hhN/A,hN/A, (none)char*,lwchar_t*,llN/A,jN/A,zN/A,tN/A,LN/A
- argument types:
d,iβ Converts a signed integer into decimal representation[-]dddd. Precision specifies the minimum number of digits to appear. The default precision is 1. If both the converted value and the precision are 0 the conversion results in no characters.- argument types:
hhsigned char,hshort, (none)int,llong,lllong long,jintmax_t,zsigned size_t,tptrdiff_t,LN/A
- argument types:
oβ Converts an unsigned integer into octal representationoooo. Precision specifies the minimum number of digits to appear. The default precision is 1. If both the converted value and the precision are 0 the conversion results in no characters. In the alternative implementation precision is increased if necessary, to write one leading zero. In that case if both the converted value and the precision are 0, single 0 is written.- argument types:
hhunsigned char,hunsigned short, (none)unsigned int,lunsigned long,llunsigned long long,juintmax_t,zsize_t,tptrdiff_t,LN/A
- argument types:
x,Xβ Converts an unsigned integer into hexadecimal representationhhhh. For thexconversion lettersabcdefare used. For theXversion of the conversion lettersABCDEFare used. Precision specifies the minimum number of digits to appear. The default precision is 1. If both the converted value and the precision are 0 the conversion results in no characters. In the alternative implementation0xor0Xis prefixed to results if the converted value is nonzero.- argument types:
hhunsigned char,hunsigned short, (none)unsigned int,lunsigned long,llunsigned long long,juintmax_t,zsize_t,tptrdiff_t,LN/A
- argument types:
uβ Converts an unsigned integer into decimal representationdddd. Precision specifies the minimum number of digits to appear. The default precision is 1. If both the converted value and the precision are 0 the conversion results in no characters.- argument types:
hhunsigned char,hunsigned short, (none)unsigned int,lunsigned long,llunsigned long long,juintmax_t,zsize_t,tptrdiff_t,LN/A
- argument types:
f,Fβ Converts floating-point number to the decimal notation in the style[-]ddd.ddd. Precision specifies the exact number of digits to appear after the decimal point character. The default precision is 6. In the alternative implementation decimal point character is written even if no digits follow it. For infinity and not-a-number conversion style see notes.- argument types:
hhN/A,hN/A, (none)double,ldouble,llN/A,jN/A,zN/A,tN/A,Llong double
- argument types:
e,Eβ Converts floating-point number to the decimal exponent notation.
psst β thej,z,t, andLcolumns are all marked (C++11), so keep that in mind, bestie π«Ά
...and we're literally not done yet, there's more coming in part 2 π
So like, picking up right where we left off with the conversion specifiers, bestie π:
the e / E era β¨
e,Eβ For theeconversion style,[-]d.dddeΒ±ddis used. For theEconversion style,[-]d.dddEΒ±ddis used. The exponent contains at least two digits β more digits are used only if necessary. If the value is0, the exponent is also0. Precision specifies the exact number of digits to appear after the decimal point character. The default precision is 6. In the alternative implementation, the decimal point character is written even if no digits follow it. For infinity and not-a-number conversion style, see the notes below π
As for the argument type: it's double, and also double(C++11), and also long double. Every other column in that row? N/A, N/A, N/A, N/A, N/A, N/A. Ghost town π«₯
the a / A hex era π¦
a,Aβ Converts a floating-point number to the hexadecimal exponent notation.
For the a conversion style, [-]0xh.hhhpΒ±d is used. For the A conversion style, [-]0Xh.hhhPΒ±d is used. The first hexadecimal digit is not 0 if the argument is a normalized floating-point value. If the value is 0, the exponent is also 0. Precision specifies the exact number of digits to appear after the hexadecimal point character. The default precision is sufficient for exact representation of the value. In the alternative implementation, the decimal point character is written even if no digits follow it. For infinity and not-a-number conversion style, see the notes πΈ
Argument type is double, double(C++11) (that's the C++11 flavour, mwah π
), and long double. Everything else in the row is N/A Γ6. Iconic of it to not participate.
the g / G era, aka the "it depends" girlie π
g,Gβ Converts a floating-point number to decimal or decimal exponent notation depending on the value and the precision.
For the g conversion style, conversion with style e or f will be performed. For the G conversion style, conversion with style E or F will be performed. Let P equal the precision if nonzero, 6 if the precision is not specified, or 1 if the precision is 0.
Then, if a conversion with style E would have an exponent of X:
- if
P > X β₯ β4, the conversion is with styleforFand precisionP β 1 β X. - otherwise, the conversion is with style
eorEand precisionP β 1.
Unless alternative representation is requested, the trailing zeros are removed β and the decimal point character is removed too if no fractional part is left. For infinity and not-a-number conversion style, see the notes π₯
Argument type: double, double(C++11), long double. All six other columns: N/A. We respect the commitment to the bit π
the n girlie (she is the main character of security advisories) π
nβ Returns the number of characters written so far by this call to the function. The result is written to the value pointed to by the argument, and the specification may not contain any flag, field width, or precision.
Here the argument type actually depends on the length modifier, and the table is giving full buffet: signed char*, short*, int*, long*, long long*, intmax_t*, size_t*, ptrdiff_t*, and one lone N/A at the end. She has range π
the p girlie π§
pβ Writes an implementation defined character sequence defining a pointer.
Argument type is void*, and the remaining eight columns are all N/A, N/A, N/A, N/A, N/A, N/A, N/A, N/A. So demure of it π
Okay so now the actual gossip, the paragraphs under the table that you should NOT skip because they're lowkey important π
The floating-point conversion functions convert infinity to inf or infinity. Which one is used is implementation defined.
Not-a-number is converted to nan or nan(char_sequence). Which one is used is implementation defined. (Yes, both of these are "ask your implementation," not me, I don't make the rules π)
The conversions F, E, G, A output INF, INFINITY, NAN instead. Loud and uppercase, periodt.
Even though %c expects an int argument, it is safe to pass a char because of the integer promotion that takes place when a variadic function is called. Slay, integer promotion π
The correct conversion specifications for the fixed-width character types (int8_t, etc) are defined in the header <cinttypes> (although PRIdMAX, PRIuMAX, etc is synonymous with %jd, %ju, etc). iykyk π
β οΈ The memory-writing conversion specifier%nis a common target of security exploits where format strings depend on user input, and is not supported by the bounds-checkedprintf_sfamily of functions. Bestie. Do not pass user input into a format string. I am begging π
There is a sequence point after the action of each conversion specifier; this permits storing multiple %n results in the same variable or β as an edge case β printing a string modified by an earlier %n within the same call. Weird but true β¨
π If a conversion specification is invalid, the behavior is undefined. No cap. Undefined. Don't do it.
π Return value π
- 1,2) Number of wide characters written if successful, or a negative value if an error occurred.
- 3) Number of wide characters written (not counting the terminating null wide character) if successful, or a negative value if an encoding error occurred, or if the number of characters to be generated was equal or greater than
size.
π Notes π
So here's the struggle: narrow strings get std::vsnprintf, which lets you figure out the required output buffer size. There is no equivalent for wide strings, and in order to determine the buffer size, the program may need to call std::vswprintf, check the result value, and reallocate a larger buffer, trying again until successful.
It's giving "guess and retry" π The audacity. But that's the assignment π«Ά
π Example π
This section is incomplete.
Reason: no example
Truly the "I'll do it later" of documentation sections. Respect honestly π
π See also π
vprintf,vfprintf,vsprintf,vsnprintf(C++11) β prints formatted output tostdout, a file stream or a buffer using variable argument list (function)wprintf,fwprintf,swprintfβ prints formatted wide character output tostdout, a file stream or a buffer (function)- C documentation for
vwprintf,vfwprintf,vswprintf
And that's the whole page, bestie!! πΈ You made it. This one's brought to you fresh from http://cppreference.com, dated 2024.06.10 β the std::vwprin...d::vswprintf(3) girlies are shaking. Go forth and format your wide strings responsibly, mwah πβ¨ππ¦π
π¦ where this page comes from
- nixpkgs unstable: stdman 2024.07.05
- opensuse-leap 16.0: stdman 2024.07.05-bp160.1.9 Β· MIT
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms