liveBook · Manning

Variadic macro

As explained in Modern C, Third Edition

A variadic macro is a function-like C preprocessor macro that accepts a variable number of arguments through an ellipsis parameter.

A variadic macro is a function-like C preprocessor macro whose parameter list ends with ..., allowing an invocation to provide additional arguments. The expansion accesses those arguments through __VA_ARGS__. For example, TRACE_PRINT6("a collection: %g, %i", sum, argc) forwards the format and variable arguments to a printing operation. section 17.4.1

Allowing an empty argument list

A macro that expands directly to a call such as printf(F, __VA_ARGS__) is invalid when no variable arguments are supplied, because the resulting call contains an empty argument after the comma. C23's __VA_OPT__(,) inserts the comma only when __VA_ARGS__ is nonempty, so one macro can support both TRACE_PRINT8("a collection: %g, %i", sum, argc) and TRACE_PRINT8("another string"). section 17.4.1

Important properties and constraints include:

  • Place ... at the end of the parameter list; the fixed parameters precede the variable argument list. section 17.4.1
  • Stringify __VA_ARGS__ when the textual representation of the complete argument list, including separating commas, is needed. section 17.4.1
  • Use __VA_OPT__ with token pasting to select different target macros for calls with two versus three or more arguments. section 17.4.1
  • Keep format specifiers consistent with the types of the variable arguments; the macro does not remove the checking burden of formatted output. section 17.4.1
  • Prefer an inline function when a fixed number of typed arguments is sufficient; macro programming can obfuscate code and requires restrictive rules. section 17.0
 TRACE_PRINT8("a collection: %g, %i", sum, argc);
 TRACE_PRINT8("another string");

Compare one variadic trace call with arguments against another containing only a format string, then inspect the corresponding output. section 17.4.1

How Modern C, Third Edition covers it

How deep it goes
Explains declaration syntax, expansion, empty-list handling, stringification, and token-pasting dispatch, while deferring complete implementation details.
Examples
Demonstrates TRACE_PRINT6, TRACE_PRINT8, formatted output, __VA_ARGS__ stringification, and fprintf dispatch macros.
What it assumes
Assumes familiarity with function-like macros, formatted output, and macro expansion, while defining the variadic syntax and its main edge cases.
What you can do after
Build it from scratch using ellipsis parameters, __VA_ARGS__, __VA_OPT__, stringification, and token pasting while avoiding empty-call errors.
How it connects
Connects variadic macros with function-like macros, variadic functions, formatted output, stringification, token pasting, and inline functions.

Also explained in

Compare how 2 books explain variadic macro →

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.