vfprintf C Library Function

The function int vfprintf(FILE *stream, const char *format, va_list arg); sends formatted output to a stream using an argument list passed to it.

Function prototype of vfprintf

int vfprintf(FILE *stream, const char *format, va_list arg);
  • stream : This is the pointer to a FILE object that identifies the stream.
  • format : This is a null-terminated string containing the text to be written to the stream. It may contains some embedded format specifiers.
  • additional arguments : These arguments will substitute the value of format specifiers in output string.

Format is a null terminated string that contains the string to be written to the stream, 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 vfprintf

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

C program using vfprintf function

The following program shows the use of vfprintf function to write a formatted string on a stream. First program creates an empty text file and writes a formatted string using vfprintf function.

vfprintf C Library Function

#include <stdio.h>
#include <stdarg.h>
 
void writeFormatedString(FILE *stream, char *format, ...)
{
   va_list arguments;
 
   va_start(arguments, format);
   vfprintf(stream, format, arguments);
   va_end(arguments);
}
 
int main (){
   FILE *file;
    
   file = fopen("textFile.txt", "w");
   /* Write formatted string in textFile.txt */
   writeFormatedString(file, "%d is not a palindrome number\n", 12345);
   fclose(file);
    
   return(0);
}

The following program opens “texyFile.txt” and prints it’s content which was populated by above program.

C program using vfprintf function

#include <stdio.h>
 
int main ()
{
   FILE *file;
   int ch;
   
   file = fopen("textFile.txt","r");
   while(!feof(file)){
       ch = fgetc(file);
       printf("%c", ch);
   }   
   fclose(file);
 
   return(0);
}

Output

12345 is not a palindrome number