liveBook · Manning

Stringification operator

As explained in Modern C

Stringification is the preprocessing technique of converting a macro argument's tokens into a string literal.

Consider a trace call such as TRACE_PRINT5("my favorite variable: %g", sum). The macro can print both the value and the source expression that produced it, yielding output such as main:25:(sum): my favorite variable: 889. section 16.3

In a function-like macro, # placed before a parameter converts that argument's tokens into a string literal. The preprocessing phase treats the argument textually, preserving its source form with possible whitespace adjustment. section 16.3

TAKEAWAY

  • Stringification does not expand macros in the directly stringified argument. section 16.3
  • Use two macro levels when the argument itself must expand first: STRGY(__LINE__) expands the line number, then STRINGIFY converts it to text. section 16.3
  • Stringification also applies to __VA_ARGS__, retaining the textual representation and separating commas. section 16.5.1

Expressions become trace labels

TRACE_PRINT9("a collection: %g, %i", sum*acos(0), argc) produces main:31:(sum*acos(0), argc) a collection: 1396.44, 2. The displayed expression is text; the arguments are still evaluated separately for formatting.

How Modern C covers it

How deep it goes
Explains textual replacement, nonexpansion, and two-level indirection through trace macros, while deferring broader preprocessing details.
Examples
Uses TRACE_PRINT5 and TRACE_PRINT9 to label sum, sum*argc, line numbers, and variadic expressions in diagnostic output.
What it assumes
Assumes function-like macro parameters and preprocessing, then develops direct stringification and controlled expansion from concrete trace examples.
What you can do after
Apply it via C preprocessing to build trace macros that print source expressions and expanded line numbers alongside evaluated values.
How it connects
Connects stringification with function-like macros, macro expansion, variadic macros, __LINE__, __func__, and token concatenation.

Also explained in

Compare how 2 books explain stringification operator →

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.