vprintf C Library Function

The function int vprintf(const char *format, va_list arg); sends formatted output to a stdout using an argument list passed to it. Function vprintf writes the null terminated string pointed by format to stdout, replacing any format specifier in the same way as printf does, but using elements in the variable argument list identified by arg.

Function prototype of vprintf

int vprintf(const char *format, va_list arg);
  • format : This is a null-terminated string containing the text to be written to stdout. It may contains some embedded format specifiers.
  • additional arguments : A value representing a variable arguments list initialized with va_start(a special type defined in stdarg library).

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

The syntax of the format specifier is as follows:
%[flags][width][.precision][length]specifier

Below is the description of each component of format specifier.

Flags

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 : “vfprintf”).
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 vprintf

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

C program using vprintf function

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

vprintf C Library Function

#include <stdio.h>
#include <stdarg.h>
 
void writeFormatedString(char *format, ...)
{
   va_list arguments;
 
   va_start(arguments, format);
   vprintf(format, arguments);
   va_end(arguments);
}
 
int main (){
    
   /* Write formatted string to stdout */
   writeFormatedString("%d is not a palindrome number\n", 12345);
 
   return(0);
}

Output

12345 is not a palindrome number