liveBook · Manning

__VA_ARGS__

As explained in Modern C, Third Edition

__VA_ARGS__ is a predefined C preprocessor identifier that expands to the arguments supplied for a variadic macro's ellipsis parameter.

__VA_ARGS__ is a predefined C preprocessor identifier used inside a variadic macro. It expands to the arguments supplied after the named parameters and the ellipsis (...), preserving the comma-separated argument list. For example, TRACE_PRINT6("a collection: %g, %i", sum, argc) can forward sum and argc through __VA_ARGS__ to fprintf. section 17.4.1

Forwarding variadic arguments

#define TRACE_PRINT(FORMAT, ...) \ fprintf(stderr, FORMAT, __VA_ARGS__) TRACE_PRINT("value: %i\n", 42) Here, __VA_ARGS__ expands to 42, producing a call equivalent to fprintf(stderr, "value: %i\n", 42). The macro requires at least one variadic argument: if the list is absent, the resulting fprintf call contains an invalid empty argument after FORMAT. section 17.4.1

Use __VA_ARGS__ with these constraints and extensions:

  • Combine __VA_ARGS__ with C23 __VA_OPT__(,) when the separating comma must appear only for a nonempty variadic list. section 17.4.1
  • Stringify __VA_ARGS__ like another macro parameter; the resulting text includes commas separating the supplied arguments. section 17.4.1
  • Forward arbitrary initialization arguments through a type-specific macro such as P99_NEW(T, ...), where __VA_ARGS__ supplies the arguments to T_init. section 13.4
  • Use __VA_ARGS__ in dispatch patterns that select macro variants according to whether optional arguments are present. section 17.4.1 section 17.5

How Modern C, Third Edition covers it

How deep it goes
Explains expansion, forwarding, stringification, empty-list limitations, and __VA_OPT__-based dispatch through concrete macro definitions.
Examples
Uses TRACE_PRINT, fprintf, P99_NEW, strtoul wrappers, and macro-expansion traces as examples.
What it assumes
Assumes familiarity with function-like macros and introduces variadic syntax, ellipses, and macro expansion behavior through examples.
What you can do after
Build variadic macros that forward, stringify, or dispatch supplied arguments while handling empty lists explicitly.
How it connects
Connects variadic macros with __VA_OPT__, stringification, token concatenation, and default arguments.

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.