C library functions -fprintf()

C Standard Library - <stdio.h>

Description

C Library Functionsint fprintf(FILE *stream, const char *format, ...)Send formatted output to the stream.

Declaration

Below is the declaration of the fprintf() function.

int fprintf(FILE *stream, const char *format, ...)

Parameter

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

flagsDescription
-Left-align within the given field width; the default is right-aligned (see the width sub-specifier).
+Forces a plus or minus sign (+ or -) to be displayed before the result, i.e., a + sign is displayed before positive numbers. By default, only negative numbers have a - sign displayed before them.
(space)If no sign is written, insert a space 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, if there are no digits after it, the decimal point is not displayed.
When used with g or G, the result is the same as when using e or E, but trailing zeros are not removed.
0Place zeros (0) to the left of the number when padding is specified, instead of spaces (see the width sub-specifier).

widthDescription
(number)The 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.
*The width is not specified in the format string, but is provided as an additional integer value argument placed 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 the 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.
.*The precision is not specified in the format string, but is provided as an additional integer value argument placed before the argument to be formatted.

lengthDescription
hThe argument is interpreted as a short integer or unsigned short integer (only applicable to integer specifiers: i, d, o, u, x, and X).
lThe argument is interpreted as a long integer or unsigned long integer, applicable to integer specifiers (i, d, o, u, x, and X) and to specifiers c (indicating a wide character) and s (indicating a wide character string).
LThe argument is interpreted as a long double (only applicable to floating-point specifiers: e, E, f, g, and G).
  • Additional arguments-- Depending on the format string, the function may require a series of additional arguments, each containing a value to be inserted, replacing each % tag specified in the format parameter. The number of arguments should be the same as the number of % tags.

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 fprintf() function.

Example

#include <stdio.h>
#include <stdlib.h>

int main()
{
   FILE * fp;

   fp = fopen ("file.txt", "w+");
   fprintf(fp, "%s %s %s %d", "We", "are", "in", 2014);
   
   fclose(fp);
   
   return(0);
}

Let us compile and run the above program, which will create a filefile.txt, and its content is as follows:

We are in 2014

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

Example

#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