A variadic macro is a feature of some computer
programming languages, especially the
C preprocessor, whereby a
macro
Macro (or MACRO) may refer to:
Science and technology
* Macroscopic, subjects visible to the eye
* Macro photography, a type of close-up photography
* Image macro, a picture with text superimposed
* Monopole, Astrophysics and Cosmic Ray Observat ...
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. Support for variadic macros with no arguments was added in
C++20 and will be added in
C23.
[
]
Declaration syntax
The declaration syntax is similar to that of variadic functions: a sequence of three full stop
The full stop ( Commonwealth English), period ( North American English), or full point , is a punctuation mark. It is used for several purposes, most often to mark the end of a declarative sentence (as distinguished from a question or exclam ...
s "" is used to indicate that one or more arguments must be passed. During macro expansion each occurrence of the special identifier in the macro replacement list is replaced by the passed arguments.
Additionally, regular macro arguments may be listed before the ...
, 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.
Both the C99 and C++11 standards require at least one argument, but since C++20 this limitation has been lifted through the functional macro. The macro is replaced by its argument when arguments are present, and omitted otherwise. Common compilers also permit passing zero arguments before this addition, however.[Variadic Macros – Using the GNU Compiler Collection (GCC)](_blank)
/ref>[Variadic Macros (C++)](_blank)
/ref>
The C preprocessor rules prevent macro names in the argument of from expanding recursively. It is possible to work around this limitation up to an arbitrary fixed number of recursive expansions, however.
/ref>
Support
Several compiler
In computing, a compiler is a computer program that translates computer code written in one programming language (the ''source'' language) into another language (the ''target'' language). The name "compiler" is primarily used for programs that ...
s support variable-argument macros when compiling C and C++ code: the GNU Compiler Collection
The GNU Compiler Collection (GCC) is an optimizing compiler produced by the GNU Project supporting various programming languages, hardware architectures and operating systems. The Free Software Foundation (FSF) distributes GCC as free sof ...
3.0, Clang (all versions), Visual Studio 2005, C++Builder 2006, and Oracle Solaris Studio
Oracle Developer Studio, formerly named Oracle Solaris Studio, Sun Studio, Sun WorkShop, Forte Developer, and SunPro Compilers, is Oracle Corporation's flagship software development product for the Solaris and Linux operating systems. It inclu ...
(formerly Sun Studio) Forte Developer 6 update 2 (C++ version 5.3). GCC also supports such macros when compiling Objective-C
Objective-C is a general-purpose, object-oriented programming language that adds Smalltalk-style messaging to the C programming language. Originally developed by Brad Cox and Tom Love in the early 1980s, it was selected by NeXT for its N ...
.
Support for the macro to support zero arguments has been added in GNU Compiler Collection
The GNU Compiler Collection (GCC) is an optimizing compiler produced by the GNU Project supporting various programming languages, hardware architectures and operating systems. The Free Software Foundation (FSF) distributes GCC as free sof ...
8, Clang 6, and Visual Studio 2019.
Example
If a printf
The printf format string is a control parameter used by a class of functions in the input/output libraries of C and many other programming languages. The string is written in a simple template language: characters are usually copied liter ...
-like function were desired, which would take the file and line number from which it was called as arguments, the following solution applies.
// Our implemented function
void realdbgprintf (const char *SourceFilename,
int SourceLineno,
const char *CFormatString,
...);
// Due to limitations of the variadic macro support in C++11 the following
// straightforward solution can fail and should thus be avoided:
//
// #define dbgprintf(cformat, ...) \
// realdbgprintf (__FILE__, __LINE__, cformat, __VA_ARGS__)
//
// The reason is that
//
// dbgprintf("Hallo")
//
// gets expanded to
//
// realdbgprintf (__FILE__, __LINE__, "Hallo", )
//
// where the comma before the closing brace will result in a syntax error.
//
// GNU C++ supports a non-portable extension which solves this.
//
// #define dbgprintf(cformat, ...) \
// realdbgprintf (__FILE__, __LINE__, cformat, ##__VA_ARGS__)
//
// C++20 eventually supports the following syntax.
//
// #define dbgprintf(cformat, ...) \
// realdbgprintf (__FILE__, __LINE__, cformat __VA_OPT__(,) __VA_ARGS__)
//
// By using the 'cformat' string as part of the variadic arguments we can
// circumvent the abovementioned incompatibilities. This is tricky but
// portable.
#define dbgprintf(...) realdbgprintf (__FILE__, __LINE__, __VA_ARGS__)
could then be called as
dbgprintf ("Hello, world");
which expands to
realdbgprintf (__FILE__, __LINE__, "Hello, world");
Another example is
dbgprintf("%d + %d = %d", 2, 2, 5);
which expands to
realdbgprintf(__FILE__, __LINE__, "%d + %d = %d", 2, 2, 5);
Without variadic macros, writing wrappers to printf
The printf format string is a control parameter used by a class of functions in the input/output libraries of C and many other programming languages. The string is written in a simple template language: characters are usually copied liter ...
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) will silently eliminate the trailing comma. Other compilers (e.g.: GCC) support putting in front of .
# define MYLOG(FormatLiteral, ...) fprintf (stderr, "%s(%u): " FormatLiteral "\n", __FILE__, __LINE__, __VA_ARGS__)
The following application works
MYLOG("Too many balloons %u", 42);
which expands to
fprintf (stderr, "%s(%u): " "Too many balloons %u" "\n", __FILE__, __LINE__, 42);
which is equivalent to
fprintf (stderr, "%s(%u): Too many balloons %u\n", __FILE__, __LINE__, 42);
But look at this application:
MYLOG("Attention!");
which expands to
fprintf (stderr, "%s(%u): " "Attention!" "\n", __FILE__, __LINE__, );
which generates a syntax error with GCC.
GCC supports the following (non-portable) extension:
# define MYLOG(FormatLiteral, ...) fprintf (stderr, "%s(%u): " FormatLiteral "\n", __FILE__, __LINE__, ##__VA_ARGS__)
which removes the trailing comma when is empty.
C23 solves this problem by introducing ''__VA_OPT__'' like C++.
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 function:
#define dbgprintf(x) realdbgprintf x
could then be called as:
dbgprintf (("Hello, world %d", 27));
which expands to:
realdbgprintf ("Hello, world %d", 27);
References
{{Reflist
See also
* Variadic function
* Variadic template
C (programming language)
C++
ja:可変長引数