liveBook · Manning

Macro invocation

As explained in Modern C, Third Edition

A macro invocation is source text consisting of a function-like macro name followed by parentheses containing its arguments.

A macro invocation is a function-like macro name followed by parentheses containing arguments, such as BYTECOPY(A, B). The preprocessor parses balanced parentheses, uses top-level commas to separate arguments, and checks that their number matches the macro parameters. section 17.1

Expansion proceeds in a defined sequence:

  • Disable the macro temporarily to prevent infinite recursive expansion. section 17.1
  • Recursively expand macros appearing inside each argument. section 17.1
  • Substitute the resulting argument text into a copy of the replacement text, then rescan that text for further macro replacement. section 17.1

Expanding BYTECOPY

With #define BYTECOPY(T, S) memcpy(&(T), &(S), MINSIZE(T, S)), an invocation such as BYTECOPY(A, B) expands its arguments, substitutes them into the replacement text, and expands nested MINSIZE calls. A macro invocation can also generate declarations or statement-like code, as in declareColor(color8); or a do ... while(false) wrapper. section 17.1 section 18.1.3

#define BYTECOPY(T, S) memcpy(&(T), &(S), MINSIZE(T, S))

Inspect the BYTECOPY definition to connect invocation syntax with parameter substitution and generated library-call text. section 17.1

How Modern C, Third Edition covers it

How deep it goes
Defines invocation parsing and follows recursive argument expansion, substitution, rescanning, and macro re-enabling.
Examples
Uses BYTECOPY, nested MINSIZE expansion, color declarations, and do ... while(false) statement macros.
What you can do after
Evaluate/understand how arguments are parsed, expanded, substituted, and rescanned in a function-like macro invocation.
How it connects
Connects macro invocation with function-like macros, macro parameters, replacement text, recursive expansion, and type-generic programming.

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.