vsprintf C Library Function

The function int vsprintf(char *str, const char *format, va_list arg); sends formatted output to a string using an argument list passed to it. Function vsprintf is similar to vprintf function but instead of sending format output to stdout it send it to a string.

Function prototype of vsprintf

int vsprintf(char *str, const char *format, va_list arg);
  • str : This is the pointer to an character array where the formatted string to be stored.
  • format : This is a null-terminated string containing the text to be written to stdout. It may contains some embedded format specifiers.
  • arg : A value representing a variable arguments list initialized with va_start(a special type defined in ).

Format is a null terminated string that contains the string to be written on string, it may contains some embedded format specifiers.

The syntax of the format specifier is as follows:

Below is the description of each component of format specifier.


Flags Description
+ Forces to preceed the result with a plus or minus sign (- or +) even for positive numbers. By default, only negative numbers are preceded with a ‘-‘ sign.
Left justify within the given field width. Right justification is the default (see width sub-specifier).
0 Left padding the numbers with zeroes instead of spaces when padding is specified.
# Used with o, x or X specifiers the value to preceeded with 0, 0x or 0X respectively for values other than zero. Used with A, E, F or G it forces the written output to contain a decimal point even if no more digits follow. By default, if no digits follow, no decimal point is written.

Width Specifier

Width Description
* The width is specified as an additional integer value argument preceding the argument that has to be formatted.
(number) Minimum number of characters to be printed on console. If the value to be printed is shorter than this number, the result is padded with blank spaces. The value is result is larger than number it is not truncated.

Precision Specifier

Precision Description
.* The precision is specified as an additional integer value argument preceding the argument that has to be formatted.
.number For integer specifiers precision specifies the minimum number of digits to be displayed. If the value is shorter than number, the result is padded with leading zeros(0). If the value is longer than number the result will not get truncated. A precision of 0 means that no character is written for the value 0. For a, A, e, E, f and F specifiers: this is the number of digits to be printed after the decimal point (this is 6, by default). For g and G specifiers: This is the maximum number of significant digits to be printed. For s: this is the maximum number of characters to be printed. By default all characters gets printed till null character(‘\0’) is encountered. If the period is specified without an explicit value for precision, 0 is assumed by default.

Length Specifier

Length Specifier Description
l The is interpreted as unsigned long int or long int for integer specifiers (i, d, o, u, x and X), and as a wide character or wide character string for specifiers c and s.
L The is interpreted as a long double (only valid for floating point specifiers: e, E, f, g and G).
h The argument is a short int or unsigned short int (only applies to integer specifiers: i, d, o, u, x and X).

Specifier Character

Specifier character is the most significant component, since it defines the data type and it’s interpretation.

Specifier Character Output
i or d Signed decimal integer(653, -236).
u Unsigned decimal integer(7653).
c Character (‘T’).
f Decimal floating point, lowercase (786.425).
F Decimal floating point, uppercase (786.425).
s String of characters (For Example : “vsprintf”).
x Unsigned hexadecimal integer (3ab2).
X Unsigned hexadecimal integer (uppercase) (3AB2).
e Scientific notation (mantissa/exponent), lowercase (7.8634e+3).
E Scientific notation (mantissa/exponent), uppercase (7.8634E+3).
g Use the shortest representation: %e or %f (786.43).
G Use the shortest representation: %E or %F (786.43).
o Unsigned octal (425).
p Pointer address (For Example : b6111111).
% A % Character (%).
n Nothing printed.

Return value of vsprintf

On success, the total number of characters written on string is returned otherwise a negative number is returned in case of an error.

C program using vsprintf function

The following program shows the use of vsprintf function to write a formatted string on string using argument list.

vsprintf C Library Function

#include <stdio.h>
#include <stdarg.h>
char buffer[128];
int writeFormatedString(char *format, ...)
   va_list args;
   int response;
   va_start(args, format);
   response = vsprintf(buffer, format, args);
int main()
   writeFormatedString("%d %f %s", 5, 5.5, "TechCrashCourse.com");
   printf("%s\n", buffer);


5 5.5 TechCrashCourse.com