liveBook · Manning

Fprintf

As explained in Modern C, Third Edition

fprintf is the standard C formatted-output function that writes formatted text and converted argument values to a specified stream.

fprintf performs formatted output like printf, but adds a stream parameter that selects where the text and converted argument values are written. section 8.4.4

Matching arguments to specifiers

fprintf(stream, "%d %u", signed_value, unsigned_value) supplies two trailing arguments for two conversion specifiers. The counts must correspond exactly. section 8.4.4

Choose format components according to the value and the intended representation:

  • Use %d or %u for signed or unsigned decimal integers. section 8.4.4
  • Use %b or %x for unsigned bit patterns, preferring hexadecimal for larger sets. section 8.4.4
  • Use %g for general floating-point decimal output; modifiers select the corresponding argument type. section 8.4.4
int printf(char const format[static 1], ...);
int fprintf(FILE* stream, char const format[static 1], ...);

The declaration makes the stream parameter and variadic trailing arguments explicit. section 8.4.4

How Modern C, Third Edition covers it

How deep it goes
Explains the stream parameter, exact argument matching, format structure, conversions, flags, and modifiers.
Examples
Uses integer, bit-pattern, floating-point, hexadecimal, and format-modifier examples, supported by format tables.
What it assumes
Assumes familiarity with printf and teaches stream selection, format specifications, conversions, flags, and modifiers.
What you can do after
Apply it via fprintf by choosing suitable conversions and matching every trailing argument to its format specifier.
How it connects
Connects formatted output with printf, streams, variadic arguments, format specifiers, flags, and type modifiers.

Also explained in

Compare how 2 books explain fprintf →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Modern C, Third Edition. The section links point to the parts of the book it draws on.