liveBook · Manning

Preprocessor

As explained in Modern C, Third Edition

The C preprocessor is a translation phase that processes preprocessing directives and expands macros before compilation.

The C preprocessor is an early translation phase that replaces identifiers and function-like macro calls with token sequences before compilation. Replacement follows rules specified by the C standard, producing the same intermediate source for compilers on the same platform. section 17.1 section 18.1.3

A function-like macro expands textually

Given #define BYTECOPY(T, S) memcpy(&(T), &(S), MINSIZE(T, S)), an invocation such as BYTECOPY(redA, B) expands its arguments, substitutes them into the replacement text, and recursively expands macros appearing there. The resulting text can become memcpy(&(redA), &(B), (sizeof((redA))<sizeof((B))?sizeof((redA)):sizeof((B)))). section 17.1

Function-like macro expansion applies several constraints:

  • Temporarily disables the macro during its expansion to prevent infinite recursion. section 17.1
  • Scans parentheses and commas to divide the argument list; unmatched parentheses and incorrect argument counts violate the invocation rules. section 17.1
  • Requires parameters to be identifiers and limits their special scope to the replacement text. section 17.1
  • Supports type-generic expressions, declarations, definitions, and statement-like groups, but statement macros can involve ugliness and maintenance costs. section 18.1.3
#define BYTECOPY(T, S) memcpy(&(T), &(S), MINSIZE(T, S))

Inspect the BYTECOPY definition to see how address-taking, nested macros, and a computed size combine in one replacement sequence. section 17.1

How Modern C, Third Edition covers it

How deep it goes
Explains invocation parsing, recursive argument expansion, parameter substitution, and recursion prevention, but stops short of broader preprocessing directives.
Examples
Uses BYTECOPY and MINSIZE expansions, gray-value color macros, generated color declarations, and a mutex-protected statement macro.
What it assumes
Assumes familiarity with C syntax, function-like calls, expressions, declarations, and compilation stages.
What you can do after
Build type-generic expressions, declarations, definitions, and statement-like macros while tracking textual expansion and argument evaluation.
How it connects
Connects macros with compilation, type-generic programming, default arithmetic promotions, inline functions, and translation units.

Also explained in

Compare how 3 books explain preprocessor →

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.