liveBook · Manning

Format string

As explained in Modern C, Third Edition

A format string is a character sequence containing literal text and format specifiers that controls formatted output conversion.

Printing an array element

printf("element %zu is %g, \tits square is %g\n", i, A[i], A[i]*A[i]); The literal text is printed directly. The specifiers %zu, %g, and %g select conversions for i, A[i], and A[i]*A[i], respectively. The escapes \t and \n produce a tab and a newline. section 1.1

A format string combines literal characters with format specifications that control conversions. In formatted output, printf writes to the standard output; fprintf also takes a stream argument. Each argument-consuming specification must match its corresponding trailing argument in number and type. The specification %% prints a percent character and consumes no argument. section 1.1 section 8.4.4

Table 8.9 Format specifications for printf and similar functions with the general syntax "%[FF][WW][.PP][LL]SS", where [] surrounding a field denotes that it is optional
Name Description
FF Flags Special form of conversion
WW Field width Minimum width
PP Precision
LL Modifier Select width of type
SS Specifier Select conversion

An output specification has the form %[FF][WW][.PP][LL]SS. Only the final conversion specifier is mandatory; flags, field width, precision, and the type modifier are optional. section 8.4.4

Choose a conversion to suit both the value and its intended meaning:

  • Use %d for signed decimal integers, %u for unsigned decimal integers, and %g for generic floating-point output. section 8.4.4
  • Use %b or %x for unsigned bit patterns; hexadecimal is better suited to larger sets. section 8.4.4
  • Use a modifier to identify the relevant type: %zu prints a size_t, and %tu prints a ptrdiff_t. To print a pointer, cast it to void* and use %p. section 8.4.4 section 11.1.3

A format in modifiable memory can become invalid through an erroneous or malicious change, potentially crashing the program or divulging secrets. String literals also allow modern compilers to check calls such as fprintf for mismatches. A debug macro that discards its arguments when NDEBUG is set can leave such mismatches unchecked. section 17.2

Formatted input differs

double a[3]; if (scanf(" %lg %lg %lg ", &a[0], &a[1], &a[2]) < 3) { printf("not enough input values!\n"); } Input conversions use pointers to their destination values. Whitespace in a scanf format matches any sequence of whitespace, including none or several newlines, so these values need not come from one line. Input and output type conventions differ; %lg for double and %Lg for long double can be used consistently in both families. section 14.2 section 14.2

For scanf, %c reads a character array of fixed length, by default one character; %s reads non-whitespace characters and appends a terminating zero. A scan set such as %[aeiouAEIOU] matches characters from a specified class. section 14.2

How Modern C, Third Edition covers it

How deep it goes
Explains specification components, conversion choices, argument matching, input differences, and format-string safety.
Examples
Uses array-element output, pointer debugging, and scanning three double values to illustrate formats.
What you can do after
Apply formatted output with printf and fprintf while matching conversions to arguments and recognizing scanf conventions.
How it connects
Connects format strings to string literals, variadic arguments, streams, type modifiers, and compiler argument checking.

Also explained in

Compare how 7 books explain format string →

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.