std::vprint...d::vsnprintf(3)
π std::vprintf, std::vfprintf, std::vsprintf, std::vsnprintf β C++ Standard Library (3) π
okay so this is the C++ Standard Library manual page, section (3), and the whole squad is std::vprintf, std::vfprintf, std::vsprintf, and std::vsnprintf. Bestie, this is the v-girlie family: they take a std::va_list and do the printf thing. No cap.
πΈ NAME πΈ
std::vprintf, std::vfprintf, std::vsprintf, std::vsnprintf β literally the same list as the title, because that's how NAME works: std::vprintf,std::vfprintf,std::vsprintf,std::vsnprintf and the description is also std::vprintf,std::vfprintf,std::vsprintf,std::vsnprintf. Iconic.
β¨ Synopsis β¨
Defined in header <cstdio>:
int vprintf( const char* format, std::va_list vlist );
(1)
int vfprintf( std::FILE* stream, const char* format, std::va_list vlist );
(2)
int vsprintf( char* buffer, const char* format, std::va_list vlist );
(3)
int vsnprintf( char* buffer, std::size_t buf_size, const char* format, std::va_list vlist );
(4) (since C++11)
These functions load the data from the locations defined by vlist, convert them to character string equivalents, and write the results to a variety of sinks. The numbered girlies are:
- Writes the results to
stdout. - Writes the results to a file stream
stream. - Writes the results to a character string
buffer. - Writes the results to a character string
buffer. At mostbuf_size - 1characters are written. The resulting character string will be terminated with a null character, unlessbuf_sizeis zero. Ifbuf_sizeis zero, nothing is written andbuffermay be a null pointer, however the return value (number of bytes that would be written not including the null terminator) is still calculated and returned. That return value is still doing main character energy, even when nothing is written.
π Parameters π
streamβ output file stream to write to.bufferβ pointer to a character string to write to.buf_sizeβ maximum number of characters to write.formatβ pointer to a null-terminated character string specifying how to interpret the data.vlistβ variable argument list containing the data to print.
π
The format string π
The format string consists of ordinary byte characters (except %), which are copied unchanged into the output stream, and conversion specifications. Each conversion specification has the following format:
- 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.
π Length-modifier vibe check π
The table's length-modifier columns are hh, h, (none), l, ll, j, z, t, and L. The hh, ll, j, z, and t columns are marked (C++11) in the original table. Yes bestie, that's the whole alphabet-adjacent situation. Where the original table says N/A, I'm keeping it as N/A β no guessing.
The following format specifiers are available:
%β Writes literal%. The full conversion specification must be%%. Expected argument type for every length modifier (hh,h, none,l,ll,j,z,t,L):N/A.cβ Writes a single character. The argument is first converted tounsigned char. If thelmodifier is used, the argument is first converted to a character string as if by%lswith awchar_t[2]argument. Expected argument types:hhβN/A;hβN/A; no modifier βint;lβwint_t;llβN/A;jβN/A;zβN/A;tβN/A;LβN/A.sβ Writes a character string. The argument must be a pointer to the initial element of an array of characters. Precision specifies the maximum number of bytes to be written. If Precision is not specified, writes every byte 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, which is converted tochararray as if by a call towcrtombwith zero-initialized conversion state. Expected argument types:hhβN/A;hβN/A; no modifier βchar*;lβwchar_t*;llβN/A;jβN/A;zβN/A;tβN/A;LβN/A.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. Expected argument types:hhβsigned char;hβshort; no modifier βint;lβlong;llβlong long;jβintmax_t;zβsigned size_t;tβptrdiff_t;LβN/A.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, single0is written. Expected argument types:hhβunsigned char;hβunsigned short; no modifier βunsigned int;lβunsigned long;llβunsigned long long;jβuintmax_t;zβsize_t;tβptrdiff_t;LβN/A.x,Xβ Converts an unsigned integer into hexadecimal representationhhhh. For thexconversion lettersabcdefare used. For theXversion of 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. Expected argument types:hhβunsigned char;hβunsigned short; no modifier βunsigned int;lβunsigned long;llβunsigned long long;jβuintmax_t;zβsize_t;tβptrdiff_t;LβN/A.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. Expected argument types:hhβunsigned char;hβunsigned short; no modifier βunsigned int;lβunsigned long;llβunsigned long long;jβuintmax_t;zβsize_t;tβptrdiff_t;LβN/A.fβ Converts floating-point number to the decimal notation in the style[-]ddd.ddd.
Right, so β floating-point conversions, letβs go π
Quick heads-up: wherever the original table has N/A in the extra type cells, it stays N/A β Iβm not inventing argument types, bestie π
f,Fβ precision specifies the exact number of digits to appear after the decimal point character. The default precision is6. 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.e,Eβ converts a floating-point number to decimal exponent notation. 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 is6. 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.a,Aβ converts a floating-point number to hexadecimal exponent notation. For theaconversion style,[-]0xh.hhhpΒ±dis used. For theAconversion style,[-]0Xh.hhhPΒ±dis used. The first hexadecimal digit is not0if the argument is a normalized floating-point value. If the value is0, the exponent is also0. 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 below. The original rowβs type cells includedouble,double(C++11), andlong double, with the rest shown asN/A.g,Gβ converts a floating-point number to decimal or decimal exponent notation depending on the value and the precision. For thegconversion style, conversion with styleeorfwill be performed. For theGconversion style, conversion with styleEorFwill be performed. LetPequal the precision if nonzero,6if the precision is not specified, or1if the precision is0. Then, if a conversion with styleEwould have an exponent ofX:- if
P > X β₯ β4, the conversion is with styleforFand precisionP β 1 β X. - otherwise, the conversion is with style
eorEand precisionP β 1.
- if
Unless alternative representation is requested, the trailing zeros are removed, and the decimal point character is removed if no fractional part is left. For infinity and not-a-number conversion style, see the notes below.
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. The specification may not contain any flag, field width, or precision. The original rowβs argument types aresigned char*,signed short*,int*,long*,long long*,intmax_t*,size_t*,ptrdiff_t*, andN/A.pβ writes an implementation-defined character sequence defining a pointer. The argument type isvoid*, and the extra cells in the original row areN/A.
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.
The conversions F, E, G, A output INF, INFINITY, NAN instead.
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.
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.).
The memory-writing conversion specifier %n is a common target of security exploits where format strings depend on user input and is not supported by the bounds-checked printf_s family of functions.
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.
If a conversion specification is invalid, the behavior is undefined.
π¦ Return value π¦
1-3) Number of characters written if successful, or a negative value if an error occurred.
4) Number of characters written if successful, or a negative value if an error occurred. If the resulting string gets truncated due to the buf_size limit, the function returns the total number of characters (not including the terminating null-byte) which would have been written if the limit was not imposed.
π§ Notes π§
All these functions invoke va_arg at least once; the value of arg is indeterminate after the return. These functions do not invoke va_end, and it must be done by the caller.
β¨ Example β¨
// Run this code
#include <cstdarg>
#include <cstdio>
#include <ctime>
#include <vector>
void debug_log(const char *fmt, ...)
{
std::time_t t = std::time(nullptr);
char time_buf[100];
std::strftime(time_buf, sizeof time_buf, "%D %T", std::gmtime(&t));
std::va_list args1;
va_start(args1, fmt);
std::va_list args2;
va_copy(args2, args1);
std::vector<char> buf(1 + std::vsnprintf(nullptr, 0, fmt, args1));
va_end(args1);
std::vsnprintf(buf.data(), buf.size(), fmt, args2);
va_end(args2);
std::printf("%s [debug]: %s\n", time_buf, buf.data());
}
int main()
{
debug_log("Logging, %d, %d, %d", 1, 2, 3);
}
Output:
04/13/15 15:09:18 [debug]: Logging, 1, 2, 3
π See also π
printf,fprintf,sprintfβ prints formatted output tostdout, a file stream or a buffer (function).snprintf(C++11) β prints formatted output tostdout, a file stream or a buffer (function).vscanf(C++11),vfscanf(C++11),vsscanf(C++11) β reads formatted input fromstdin, a file stream or a buffer using variable argument list (function).vprint_unicode(C++23),vprint_unicode_locking(C++23) β prints to Unicode capablestdoutor a file stream using type-erased argument representation (function).vprint_nonunicode(C++23),vprint_nonunicode_locking(C++23) β prints tostdoutor a file stream using type-erased argument representation (function).- C documentation for
vprintf,vfprintf,vsprintf,vsnprintfβ the C girlies said βsame energyβ π
source/version/date: http://cppreference.com, 2024.06.10, std::vprint...d::vsnprintf(3) β thatβs the whole page, bestie, go format something iconic πβ¨
π¦ 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