C library functionvfprintf()

C Standard Library - <stdio.h>

Description

C Library Functionsint vfprintf(FILE *stream, const char *format, va_list arg)Send formatted output to the stream using an argument list.

Declaration

Below is the declaration of the vfprintf() function.

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

Parameter

  • stream-- This is a pointer to a FILE object, which identifies the stream.
  • format-- This is a C string containing the text to be written to the stream. It may contain embedded format tags, which are replaced by the values specified in subsequent additional arguments and formatted as required. The format tag attributes are: %[flags][width][.precision][length]specifier, the specific explanation is as follows:
specifierOutput
cCharacter
d or isigned decimal integer
eScientific notation (mantissa and exponent) using the e character
EScientific notation (mantissa and exponent) using the E character
fdecimal floating-point number
gAutomatically select the appropriate representation from %e or %f
GAutomatically select the appropriate representation from %E or %f
osigned octal
scharacter string
uunsigned decimal integer
xunsigned hexadecimal integer
XUnsigned hexadecimal integer (uppercase letters)
pPointer address
nNo output
%Character

flagsDescription
-Left-justify within the given field width; default is right-justify (see width sub-specifier).
+Forces a plus or minus sign (+ or -) to be displayed before the result, i.e., positive numbers will show a + sign in front. By default, only negative numbers show a - sign in front.
(space)If no sign is written, a space is inserted before the value.
#When used with the o, x, or X specifiers, a nonzero value is preceded by 0, 0x, or 0X, respectively.
When used with e, E, and f, it forces the output to contain a decimal point even if there are no digits after it. By default, the decimal point is not displayed if there are no digits following it.
When used with g or G, the result is the same as when using e or E, but trailing zeros are not removed.
0For specified padding, place zeros (0) to the left of the number instead of spaces (see width sub-specifier).

widthDescription
(number)Minimum number of characters to output. If the output value is shorter than this number, the result is padded with spaces. If the output value is longer than this number, the result is not truncated.
*Width is not specified in the format string, but is placed as an additional integer value argument before the argument to be formatted.

.precisionDescription
.numberFor integer specifiers (d, i, o, u, x, X): precision specifies the minimum number of digits to be written. If the written value is shorter than this number, the result is padded with leading zeros. If the written value is longer than this number, the result is not truncated. A precision of 0 means no characters are written.
For e, E, and f specifiers: the number of digits to be output after the decimal point.
For g and G specifiers: the maximum number of significant digits to be output.
For s: the maximum number of characters to be output. By default, all characters are output until the terminating null character is encountered.
For c type: has no effect.
When no precision is specified, the default is 1. If specified without an explicit value, it is assumed to be 0.
.*Precision is not specified in the format string, but is placed as an additional integer value argument before the argument to be formatted.

lengthDescription
hThe argument is interpreted as a short int or unsigned short int (only applies to integer specifiers: i, d, o, u, x, and X).
lThe argument is interpreted as a long int or unsigned long int, applicable to integer specifiers (i, d, o, u, x, and X) and specifiers c (indicating a wide character) and s (indicating a wide-character string).
LThe argument is interpreted as a long double (only applies to floating-point specifiers: e, E, f, g, and G).
  • arg-- An object representing the variable argument list. This should be initialized with the va_start macro defined in <stdarg>.

Return Value

If successful, the total number of characters written is returned; otherwise, a negative number is returned.

Example

The following example demonstrates the usage of the vfprintf() function.

#include <stdio.h>
#include <stdarg.h>

void WriteFrmtd(FILE *stream, char *format, ...)
{
   va_list args;

   va_start(args, format);
   vfprintf(stream, format, args);
   va_end(args);
}

int main ()
{
   FILE *fp;

   fp = fopen("file.txt","w");

   WriteFrmtd(fp, "This is just one argument %d \n", 10);

   fclose(fp);
   
   return(0);
}

Let us compile and run the above program, which will open the file in the current directoryfile.txt, and write the following content:

This is just one argument 10

Now let's use the following program to view the contents of the above file:

#include <stdio.h>

int main ()
{
   FILE *fp;
   int c;

   fp = fopen("file.txt","r");
   while(1)
   {
      c = fgetc(fp);
      if( feof(fp) )
      {
          break ;
      }
      printf("%c", c);
   }
   fclose(fp);
   return(0);
}

C Standard Library - <stdio.h>

other extensions