liveBook · Manning

Macro parameter

As explained in Modern C

A macro parameter is a named placeholder in a function-like macro definition that receives replacement tokens from a macro invocation.

A macro parameter is an identifier named in a function-like macro definition. During preprocessing, arguments from an invocation are expanded and substituted for matching occurrences in the replacement text. TAKEAWAY: macro parameters belong to the replacement text, not to a function's runtime scope. section 16.1

Substituting parameters

#define BYTECOPY(T, S) memcpy(&(T), &(S), MINSIZE(T, S)) BYTECOPY(A, B) The parameters T and S receive the two invocation arguments. If A expands to redA, preprocessing produces text equivalent to memcpy(&(redA), &(B), MINSIZE(redA, B)). The argument count must match the parameter count, and commas inside additional parentheses do not separate arguments. section 16.1

Macro parameters have several precise preprocessing behaviors:

  • Must be identifiers and are valid only within the replacement text. section 16.1
  • Receive recursively expanded argument text before ordinary replacement. section 16.1
  • Become string literals when preceded by #, preserving the argument's textual form; this use does not expand macros in that argument. section 16.3

How Modern C covers it

How deep it goes
Explains naming, argument parsing, recursive expansion, substitution, and stringification, but stops short of broader macro operators.
Examples
Uses BYTECOPY substitutions and trace macros that stringify expressions such as sum and sum*argc.
What you can do after
Apply it via C preprocessing to predict parameter substitution and preserve or stringify invocation arguments deliberately.
How it connects
Connects macro parameters with preprocessing, macro expansion, function-like macros, argument parsing, and stringification.

Also explained in

Compare how 2 books explain macro parameter →

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.