C vfprintf() function

C library function - vfprintf()

The vfprintf() function is used to format and write a series of characters and values to the output stream.


int vfprintf(FILE *stream, const char *format, va_list arg)


Name Description
stream Identifies an address for a file descriptor, which is an area of memory associated with an input or output stream.
format The format argument is a string containing C language conversion specifications. The conversion specification specifies the notation, alignment, significant digits, field width, and other aspects of the output format. To represent non-printing characters, such as newlines and tabs, the format string may contain escape characters. Below are the details.
va_list Type to hold information about variable arguments. It shall have already been initialized by the va_start macro defined in <cstdarg>.

The vfprintf() function converts each entry in the argument list according to the corresponding format specifier in format. The format has the same form and function as the format string for the printf() function.

Format strings specify notation, alignment, significant digits, field width, and other aspects of output formats. It can contain ordinary alphanumeric characters; along with escape characters, conversion specifiers, and other characters. Format specifications are made up of a the percent sign (%) followed by one of the following conversion operators, which determine what printf does with its arguments.

Conversion Specifiers:

Specifier Description
%% Print a single % character
%c Convert an int to an unsigned character and print the resulting character
%d or %i Print an int as a signed decimal number
%u Print an unsigned as an unsigned decimal number
%o Print an unsigned as an unsigned octal number
%x Hexadecimal notation (using lowercase letters a-f)
%X Hexadecimal notation (using uppercase letters A-F)
%e Exponential notation (using a lowercase e as in 3.1415e+00)
%E Exponential notation (using an uppercase E as in 3.1415E+00)
%f Print a double using a decimal format like [-]ddd.ddd
%g The more compact of %e or %f, insignificant zeros do not print.
%G Same as g, but using an uppercase E
%s Print the string pointed to by a char *
%p Print a void * argument in hexadecimal (ANSI C only)
%n Store the number of characters printed at this point in the integer pointed to by the int * argument. Nothing is printed. (ANSI C only).

Conversion Flags:

You can control the alignment of the output using any of these optional flags.

Flag Format Example
+ (plus) Always prints a sign character (+ or -). %+7.2d
- (minus) Left-justifies the converted argument in its field. %-7.2d
0 (Zero) Pad with zeros rather than spaces. %07.2d
# (hash)

Use an alternate form for the output. The effect differs depending on the conversion specifier.

  • For o, the precision (described below) is increased so that the first digit printed is a 0
  • For x or X, a non-zero value has a 0x prepended (or 0X for the X specification)
  • For eEfg or G, the result will always contain a decimal point, even if no digits follow it. Additionally, trailing zeros are not removed from numbers formatted with g or G

Conversion width and precision:

By including these options in the format string, you can control the width and precision of the output:

Character Description Example
Field width A digit string specifying the minimum number of digits to be printed. %5f
Precision A digit string including a period (.) specifying the number of digits to be printed to the right of the decimal point. %5.2f

Some conversion operators may also be preceded by a size specification:

  • h indicates that the argument associated with a dioux or X operator is a short or unsigned short.
  • l indicates that the argument associated with a dioux or X operator is a long or unsigned long.
  • L indicates that the argument associated with a eEfg or G operator is a long double (ANSI C only)

Escape sequences in C:

An escape sequence is a series of characters that represents a special character. It begins with a backslash character (\), which indicates that the character(s) that follow the backslash character should be treated in a special way. C uses escape sequences within a format string to print certain special characters. For example \n moves the output position to the beginning of the next line. The following is a list of escape sequences.

Escape sequence Action
\n prints a new line
\b backs up one character
\t moves the output position to the next tab stop
\\ prints a backslash
\" prints a double quote
\' prints a single quote

Return value: Upon successful completion, the printf() functions shall return the number of bytes transmitted.

Example: vfprintf() function

Following example prints out a variable number of strings to the file test.txt and then display the content of the file.

#include <stdio.h>
#include <stdarg.h>
void WriteFormatted(FILE *stream, char *fmt, ...);
char fmt1 [] = "%d %s  %s  %s  %s\n";

void WriteFormatted(FILE *stream, char *fmt, ...)
   va_list arg_ptr; 
   va_start(arg_ptr, fmt);
   vfprintf(stream, fmt, arg_ptr);
int main(void)
   FILE *fp;
   fp = fopen("test.txt", "w");
   WriteFormatted(fp, fmt1, 3, "colors:", "Red,", "Green,", "White.");

  int c;
  fp = fopen("test.txt", "r");
  while (1) 
    c = fgetc(fp);
    if (feof(fp)) {
    printf("%c", c);


3 colors:  Red,  Green,  White.

C Programming Code Editor:

Previous C Programming: C sprintf()
Next C Programming: C vsprintf()

Follow us on Facebook and Twitter for latest update.