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>Working draft changes for C99 preprocessor synchronization – http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2004/n1653.htm</ref> Support for variadic macros with no arguments was added in C++20 and will be added in C23.<ref>{{#invoke:citation/CS1|citation |CitationClass=web }}</ref><ref name=N3033/>
Declaration syntaxEdit
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 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>
SupportEdit
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=38770</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>
ExampleEdit
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.
Template:Olist
<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 commaEdit
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>
AlternativesEdit
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>