liveBook · Manning

Function-like macro

A function-like macro is a macro invoked with function-like syntax that accepts token input and expands or transforms it into generated source.

How 3 Manning books explain it

Modern C, Third Edition

A function-like macro is a C preprocessor macro invoked with parentheses and arguments that expands its invocation into replacement source text.

How it is used
Positions function-like macros as substitutes for some library interfaces and as tools for checks, tracing, and interfaces functions cannot readily express.
How deep it goes
Works through textual replacement, repeated evaluation, statement-shape pitfalls, compile-time checks, and invocation-site tracing.
Examples
Contrasts `square_macro(count())` with an inline function, repairs a dangling `else`, and checks a string-literal argument.
What you can do after
Build it from scratch for checks or tracing while recognizing when an inline function avoids duplicated evaluation and provides a typed interface.

Read how Modern C, Third Edition explains function-like macro →

Modern C

A function-like macro is a C macro defined with parameters that expands preprocessing tokens when invoked with an argument list resembling a function call.

How it is used
Uses call-shaped preprocessing replacements where functions cannot supply argument checks, defaults, variable argument lists, or type-generic interfaces.
How deep it goes
Works through argument separation, expansion, substitution, and rescanning, then examines evaluation and syntax hazards.
Examples
Contrasts a squaring macro with an inline function and uses a dangling-else repair and string-literal checking to expose practical constraints.
What you can do after
Build it from scratch when needed, while choosing inline functions for ordinary typed calls and checking expansion for duplicated effects or surprising syntax.

Read how Modern C explains function-like macro →

Write Powerful Rust Macros

A function-like macro is a Rust procedural macro invoked with the ! operator that accepts arbitrary token input and replaces that input with generated output.

How it is used
Generates replacement Rust from flexible token input for transformations and mini-DSLs beyond annotated items.
How deep it goes
Works through macro signatures, token parsing, custom parsers, quoted output, block scoping, and compiler-guided debugging.
Examples
Uses a struct-accessor macro, dot-separated function composition, YAML configuration generation, and implementation exercises.
What you can do after
Build it from scratch by parsing token input, generating replacement code, and selecting a narrower macro when possible.

Read how Write Powerful Rust Macros explains function-like macro →

Related concepts

These guides are generated from the text of Manning books by Manning's knowledge graph and link to the sections they draw on. The full books are available on liveBook and at manning.com.