liveBook · Manning

Preprocessor

As explained in Modern C

A preprocessor is a source-processing component or stage that transforms input, often handling directives or expansions, before primary processing.

Consider `#define BYTECOPY(T, S) memcpy(&(T), &(S), MINSIZE(T, S))`. Before primary compilation, preprocessing replaces a function-like macro call with text according to rules for arguments, parameters, parentheses, commas, and recursive expansion. section 16.1

The replacement process performs several distinct operations:

  • Temporarily disables the macro to avoid infinite recursion. section 16.1
  • Scans parentheses and commas to divide the argument list, requiring balanced parentheses and the correct argument count. section 16.1
  • Recursively expands macros appearing in arguments, then substitutes the resulting text into a copied replacement list. section 16.1
  • Keeps macro parameters as identifiers whose special scope is limited to replacement text. section 16.1
#define BYTECOPY(T, S) memcpy(&(T), &(S), MINSIZE(T, S))


!@%STYLE%@!
{"css":"{\"css\": \"font-weight: bold;\"}","target":"[[{\"line\":0,\"ch\":0},{\"line\":0,\"ch\":7}],[{\"line\":0,\"ch\":23},{\"line\":0,\"ch\":29}]]"}
!@%STYLE%@!

Inspect the complete BYTECOPY definition and its textual replacement structure. section 16.1

A misleading function-like macro

The name `string_literal` can denote both a macro and a function. Parenthesizing the function name, as in `(string_literal)`, prevents the function-like macro from being invoked, allowing declarations and function-pointer use without textual substitution. section 16.1

How Modern C covers it

How deep it goes
Traces argument parsing, recursive expansion, substitution, and recursion prevention, but stops short of broader preprocessing stages.
Examples
Uses MINSIZE, BYTECOPY, memcpy, and string_literal to demonstrate expansion, substitution, and deliberate macro avoidance.
What it assumes
Assumes basic C source and macro definitions while defining function-like macro parameters and argument parsing in context.
What you can do after
Evaluate function-like macro expansion by tracing arguments, recursive substitutions, balanced delimiters, and resulting source text.
How it connects
Connects preprocessing with macros, compilation, function declarations, function pointers, and textual replacement.

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. The section links point to the parts of the book it draws on.