liveBook · Manning

Replacement text

As explained in Modern C, Third Edition

Replacement text is the source token sequence specified by a macro definition for insertion when the macro expands.

Replacement text is the source token sequence stored by a macro definition. When a function-like macro is invoked, preprocessing copies this sequence and substitutes each parameter with the corresponding expanded argument. section 17.1

A replacement text with parameters

In `#define MINSIZE(X, Y) (sizeof(X)<sizeof(Y) ? sizeof(X) : sizeof(Y))`, the expression after the parameter list is the replacement text. An invocation such as `MINSIZE(redA, B)` substitutes the expanded arguments for `X` and `Y`, producing a larger expression that computes the smaller size. section 17.1

Function-like macro expansion processes replacement text through these steps:

  • Temporarily disable the macro to prevent infinite recursion. section 17.1
  • Parse the invocation arguments, using commas outside nested parentheses as separators. section 17.1
  • Recursively expand macros appearing in the arguments, then substitute the resulting fragments into a copy of the replacement text. section 17.1
#define BYTECOPY(T, S) memcpy(&(T), &(S), MINSIZE(T, S))

Inspect a replacement text that combines parameter substitution with `memcpy` and another macro invocation. section 17.1

How Modern C, Third Edition covers it

How deep it goes
Explains argument parsing, recursive argument expansion, substitution, and recursion prevention, but not later preprocessing stages.
Examples
Uses MINSIZE and BYTECOPY definitions plus expanded listings to show parameter substitution and nested macro expansion.
What you can do after
Evaluate function-like macro expansions by tracing argument parsing and parameter substitution.
How it connects
Connects macro definitions with preprocessing, macro parameters, macro invocation, and recursive expansion.

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.