Variadic macro in the C preprocessor
Template:Short description A variadic macro is a feature of some computer programming languages, especially the C preprocessor, whereby a macro may be declared to accept a varying number of arguments.
Variable-argument macros were introduced in 1999 in the ISO/IEC 9899:1999 (C99) revision of the C language standard, and in 2011 in ISO/IEC 14882:2011 (C++11) revision of the C++ language standard.<ref>{{#invoke:citation/CS1|citation |CitationClass=web }}</ref> Support for variadic macros with no arguments was added in C++20 and C23.<ref>{{#invoke:citation/CS1|citation |CitationClass=web }}</ref><ref name=N3033/>
Declaration syntax
The declaration syntax is similar to that of variadic functions: a sequence of three full stops "Template:Var" is used to indicate that one or more arguments must be passed. During macro expansion each occurrence of the special identifier Template:Var in the macro replacement list is replaced by the passed arguments.
Additionally, regular macro arguments may be listed before the ...,<ref name="gcc" /> but regular arguments may not be listed after the ....
No means is provided to access individual arguments in the variable argument list, nor to find out how many were passed. However, macros can be written to count the number of arguments that have been passed.<ref>Template:Cite newsgroup</ref>
Both the C99 and C++11 standards require at least one argument, but since C++20 and C23 this limitation has been lifted through the Template:Var functional macro. The Template:Var macro is replaced by its argument when arguments are present, and omitted otherwise. Common compilers also permit passing zero arguments before this addition, however.<ref name="gcc">Variadic Macros – Using the GNU Compiler Collection (GCC)</ref><ref name="msvc">Variadic Macros (C++)</ref>
The C preprocessor rules prevent macro names in the argument of Template:Var from expanding recursively. It is possible to work around this limitation up to an arbitrary fixed number of recursive expansions, however.<ref name="recursive-vaopt">Recursive macros with C++20 __VA_OPT__</ref>
Support
Several compilers support variable-argument macros when compiling C and C++ code: the GNU Compiler Collection 3.0,<ref name="gcc" /> Clang (all versions),<ref>Clang source code change that mentions __VA_ARGS__ support (2006-07-29), note that Clang was open-sourced in 2007. http://llvm.org/viewvc/llvm-project?view=revision&revision=38770Template:Dead link</ref> Visual Studio 2005,<ref name="msvc" /> C++Builder 2006, and Oracle Solaris Studio (formerly Sun Studio) Forte Developer 6 update 2 (C++ version 5.3).<ref>Sun Studio feature comparison – http://developers.sun.com/sunstudio/support/CCcompare.html</ref> GCC also supports such macros when compiling Objective-C.
Support for the Template:Var macro to support zero arguments has been added in GNU Compiler Collection 8,<ref>{{#invoke:citation/CS1|citation |CitationClass=web }}</ref> Clang 6,<ref>{{#invoke:citation/CS1|citation |CitationClass=web }}</ref> and Visual Studio 2019.<ref>{{#invoke:citation/CS1|citation |CitationClass=web }}</ref>
Example
If a printf-like function <syntaxhighlight lang="text" class="" style="" inline="1">dbgprintf()</syntaxhighlight> were desired, which would take the file and line number from which it was called as arguments, the following solution applies.
Our implemented function: <syntaxhighlight lang="c"> void realdbgprintf(const char* fileName, int line, const char* fmt, ...); </syntaxhighlight>
Due to limitations of the variadic macro support in C++11 the following straightforward solution can fail and should thus be avoided: <syntaxhighlight lang="cpp">
- define dbgprintf(cformat, ...) realdbgprintf(__FILE__, __LINE__, cformat, __VA_ARGS__)
</syntaxhighlight>
The reason is that <syntaxhighlight lang="cpp"> dbgprintf("Hallo") </syntaxhighlight>
gets expanded to <syntaxhighlight lang="cpp"> realdbgprintf(__FILE__, __LINE__, "Hallo", ) </syntaxhighlight>
where the comma before the closing brace will result in a syntax error.
GNU C++ supports a non-portable extension which solves this: <syntaxhighlight lang="c">
- define dbgprintf(cformat, ...) realdbgprintf(__FILE__, __LINE__, cformat, ##__VA_ARGS__)
</syntaxhighlight>
C++20 supports the following syntax. <syntaxhighlight lang="cpp">
- define dbgprintf(cformat, ...) realdbgprintf(__FILE__, __LINE__, cformat __VA_OPT__(,) __VA_ARGS__)
</syntaxhighlight>
By using the cformat string as part of the variadic arguments we can circumvent the abovementioned incompatibilities. This is tricky but portable.
<syntaxhighlight lang="c">
- define dbgprintf(...) realdbgprintf(__FILE__, __LINE__, __VA_ARGS__)
</syntaxhighlight>
<syntaxhighlight lang="text" class="" style="" inline="1">dbgprintf()</syntaxhighlight> could then be called as <syntaxhighlight lang="c"> dbgprintf("Hello, world"); </syntaxhighlight>
which expands to <syntaxhighlight lang="c"> realdbgprintf(__FILE__, __LINE__, "Hello, world"); </syntaxhighlight>
Another example is: <syntaxhighlight lang="c"> dbgprintf("%d + %d = %d", 2, 2, 5); </syntaxhighlight>
which expands to <syntaxhighlight lang="c"> realdbgprintf(__FILE__, __LINE__, "%d + %d = %d", 2, 2, 5); </syntaxhighlight>
Without variadic macros, writing wrappers to printf is not directly possible. The standard workaround is to use the stdargs functionality of C/C++, and have the function call vprintf instead.
Trailing comma
There is a portability issue with generating a trailing comma with empty args for variadic macros in C99. Some compilers (e.g., Visual Studio when not using the new standard-conformant preprocessor<ref name="msvc" />) will silently eliminate the trailing comma. Other compilers (e.g.: GCC<ref name="gcc" />) support putting <syntaxhighlight lang="text" class="" style="" inline="1">##</syntaxhighlight> in front of Template:Var.
<syntaxhighlight lang="c">
- define MYLOG(FormatLiteral, ...) fprintf(stderr, "%s(%u): " FormatLiteral "\n", __FILE__, __LINE__, __VA_ARGS__)
</syntaxhighlight> The following application works <syntaxhighlight lang="c"> MYLOG("Too many balloons %u", 42); </syntaxhighlight> which expands to <syntaxhighlight lang="c"> fprintf(stderr, "%s(%u): " "Too many balloons %u" "\n", __FILE__, __LINE__, 42); </syntaxhighlight> which is equivalent to <syntaxhighlight lang="c"> fprintf(stderr, "%s(%u): Too many balloons %u\n", __FILE__, __LINE__, 42); </syntaxhighlight> But look at this application: <syntaxhighlight lang="c"> MYLOG("Attention!"); </syntaxhighlight> which expands to <syntaxhighlight lang="c"> fprintf(stderr, "%s(%u): " "Attention!" "\n", __FILE__, __LINE__, ); </syntaxhighlight> which generates a syntax error with GCC.
GCC supports the following (non-portable) extension: <syntaxhighlight lang="c">
- define MYLOG(FormatLiteral, ...) fprintf(stderr, "%s(%u): " FormatLiteral "\n", __FILE__, __LINE__, ##__VA_ARGS__)
</syntaxhighlight>
which removes the trailing comma when Template:Var is empty.
C23 solves this problem by introducing Template:Var like C++.<ref name=N3033>{{#invoke:citation/CS1|citation
|CitationClass=web
}}</ref>
Alternatives
Before the existence of variable-arguments in C99, it was quite common to use doubly nested parentheses to exploit the variable number of arguments that could be supplied to the <syntaxhighlight lang="text" class="" style="" inline="1">printf()</syntaxhighlight> function:
<syntaxhighlight lang="c">
- define dbgprintf(x) realdbgprintf x
</syntaxhighlight> <syntaxhighlight lang="text" class="" style="" inline="1">dbgprintf()</syntaxhighlight> could then be called as: <syntaxhighlight lang="c"> dbgprintf(("Hello, world %d", 27)); </syntaxhighlight> which expands to: <syntaxhighlight lang="c"> realdbgprintf("Hello, world %d", 27); </syntaxhighlight>