liveBook · Manning

Format string

As explained in Modern C

A format string is a character sequence containing ordinary output text and conversion directives interpreted by a formatted-output function.

Consider printf("element %zu is %g,\tits square is %g\n", i, A[i], A[i]*A[i]);. The quoted character sequence combines ordinary output, escape characters, and conversion directives. Each directive beginning with % marks where a later argument supplies formatted output. section 1.1

A format specification can contain flags, width, precision, modifiers, and a required specifier: %[FF][WW][.PP][LL]SS. The specifier selects the conversion, while the modifier identifies the corresponding argument type. The number of arguments must exactly match the % specifiers; otherwise behavior is undefined. section 8.3.4

Choose a directive according to the value’s meaning:

  • Use %d or %u for decimal signed or unsigned integers, %x for unsigned bit patterns, and %g for generic floating-point output. section 8.3.4
  • Use %s for strings, %c for characters, %p for addresses, and %% for a literal percent character. section 11.1.3
  • Match type-sensitive modifiers such as z for size_t, t for ptrdiff_t, and L for long double; mismatches can cause problems. section 8.3.4

Formatted input uses the same idea differently

scanf(" %lg %lg %lg ", &a[0], &a[1], &a[2]) reads three double values through pointer arguments. In input formats, whitespace matches any sequence of whitespace, and %s consumes non-whitespace characters while adding a terminating 0. These conventions differ subtly from printf, so scanf formats require extra care. section 14.2

17    printf("element %zu is %g, \tits square is %g\n",
18           i,
19           A[i],
20           A[i]*A[i]);


!@%STYLE%@!
{"css":"{\"css\": \"font-weight: bold;\"}","target":"[[{\"line\":0,\"ch\":0},{\"line\":0,\"ch\":2}],[{\"line\":0,\"ch\":6},{\"line\":0,\"ch\":12}],[{\"line\":1,\"ch\":0},{\"line\":1,\"ch\":2}],[{\"line\":2,\"ch\":0},{\"line\":2,\"ch\":2}],[{\"line\":3,\"ch\":0},{\"line\":3,\"ch\":2}]]"}
!@%STYLE%@!

Inspect the complete printf call alongside its format directives and supplied expressions. section 1.1

How Modern C covers it

How deep it goes
Explains syntax, conversions, modifiers, whitespace, and undefined mismatches, but defers complete scanf conventions and deeper pointer details.
Examples
Demonstrates array-element output, numeric conversion choices, pointer diagnostics, and scanning three double values.
What it assumes
Assumes basic C function calls and arguments, while introducing string literals, format specifiers, escape characters, and pointer arguments in context.
What you can do after
Apply it via C formatted-I/O functions while matching directives, argument types, conversions, and input behavior.
How it connects
Connects printf, fprintf, scanf, fscanf, sscanf, pointers, escape characters, size_t, and ptrdiff_t.

Also explained in

Compare how 5 books explain format string →

Related concepts

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