liveBook · Manning

Format modifier

As explained in Modern C, Third Edition

A format modifier is an optional format-specification component that identifies the size or semantic type of the corresponding argument.

A format modifier is the optional part of a printf format specification that selects the corresponding argument’s exact type or width. In "%ld", l selects a long integer; the final specifier still selects decimal conversion. section 8.4.4

Matching modifiers to arguments

long count = 42; printf("%ld\n", count); Here, l identifies the long integer argument, while d requests decimal output. Other examples include "%zu" for size_t, "%jd" for intmax_t, and "%Lf" for long double. section 8.4.4

Common integer and floating-point modifiers include:

  • hh selects char types; h selects short types; no modifier selects signed or unsigned int. section 8.4.4
  • l and ll select long and long long integer types. section 8.4.4
  • j, z, and t select intmax_t, size_t, and ptrdiff_t. section 8.4.4
  • L selects long double; float arguments are first converted to double. section 8.4.4
Table 8.12 Format modifiers for printf and similar functions. float arguments are first converted to double.
Modifier Type Conversion
"L" long double Floating point
"hh" char types Integer
"h" short types Integer
"" signed, unsigned Integer
"l" long integer types Integer
"ll" long long integer types Integer
"j" intmax_t, uintmax_t Integer
"z" size_t Integer
"t" ptrdiff_t Integer
Since C23, for 𝑁, usually 8, 16, 32, 64, or 128
"w𝑁" uint𝑁_t, int𝑁_t, uint_least𝑁_t, or int_least𝑁_t for 𝑁, usually 8, 16, 32, 64, or 128 Integer
"wf𝑁" uint_fast𝑁_t or int_fast𝑁_t Integer

Use the modifier table as a lookup for matching printf conversions to standard C types, including C23 width-specific integer types. section 8.4.4

How Modern C, Third Edition covers it

How deep it goes
Defines modifier placement and type mappings, but does not develop variadic argument mechanics beyond exact correspondence.
Examples
Uses long, size_t, intmax_t, ptrdiff_t, long double, and C23 width-specific integer types as modifier examples.
What it assumes
Assumes familiarity with printf format specifiers and C scalar types.
What you can do after
Apply printf modifiers to select conversions matching signed, unsigned, floating-point, and standard-width argument types.
How it connects
Connects format modifiers with printf, format specifiers, variadic arguments, and standard integer types.

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.