C Reference ยท Preprocessor
Variadic macros
Reference for C variadic macros: the ... parameter, __VA_ARGS__, the C23 __VA_OPT__ for optional commas, forwarding to printf and counting arguments.
A variadic macro accepts a variable number of arguments. Its parameter list ends with ..., and the replacement list refers to the extra arguments, comma-separated, with __VA_ARGS__. This is the standard way to write logging macros that forward a format string and values to printf.
The classic problem is a call with no variable arguments: the comma before __VA_ARGS__ is left dangling. C23 fixes it with __VA_OPT__(content), which expands to its content only if the variable part is non-empty.
#define LOG(fmt, ...) printf(fmt __VA_OPT__(,) __VA_ARGS__)
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.
Syntax
#define NAME(a, b, ...) replacement /* uses __VA_ARGS__ */ #define NAME(...) replacement #define NAME(a, ...) f(a __VA_OPT__(,) __VA_ARGS__) /* C23 */
| Name | Description |
|---|---|
... |
Last parameter; matches zero or more arguments (C23) or one or more (C99 to C17, strictly). |
__VA_ARGS__ |
Expands to the variable arguments, with their commas. Only valid inside a variadic macro. |
__VA_OPT__(x) |
C23: expands to x if __VA_ARGS__ has tokens, otherwise to nothing. |
#__VA_ARGS__ |
Stringifies the whole variable part. |
Errors: using __VA_ARGS__ in a non-variadic macro, or passing fewer arguments than the named parameters, is a constraint violation.
Usage notes
- Trailing comma problem:
printf(fmt, __VA_ARGS__)fails when called asLOG("x")because it becomesprintf("x",). Use__VA_OPT__(,)in C23, or GCC's older, ##__VA_ARGS__. - The empty variable part is explicitly allowed in C23 and in GCC and Clang before it, with a pedantic warning in old modes.
- Forward, do not copy: variadic macros expand to a call, so the callee still sees ordinary arguments. For a real runtime variable list, see
va_listfunctions. - Counting arguments needs a trick like
NARGS: pad with descending numbers and pick the Nth. It is capped by the number of pads you write. - Compound literals (
(int[]){ __VA_ARGS__ }) turn a macro argument list into an array, avoidingva_argaltogether. - Evaluation: each variable argument appears once per use of
__VA_ARGS__in the replacement, so side effects repeat if you use it twice. - Type safety: none. A wrong format specifier is only caught if the compiler checks
printfcalls, which GCC does for literal formats.
Related
- #define, The # operator, Macro pitfalls
- Tutorial: Macros, Variadic functions