A variadic macro is a feature of some computer
programming language
A programming language is a system of notation for writing computer programs.
Programming languages are described in terms of their Syntax (programming languages), syntax (form) and semantics (computer science), semantics (meaning), usually def ...
s, especially the
C preprocessor, whereby a
macro may be declared to accept a varying number of
arguments
An argument is a series of sentences, statements, or propositions some of which are called premises and one is the conclusion. The purpose of an argument is to give reasons for one's conclusion via justification, explanation, and/or persua ...
.
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
C++11 is a version of a joint technical standard, ISO/IEC 14882, by the International Organization for Standardization (ISO) and International Electrotechnical Commission (IEC), for the C++ programming language. C++11 replaced the prior vers ...
) revision of the
C++ language standard. Support for variadic macros with no arguments was added in
C++20 C20 or C-20 may refer to:
Science and technology
* Carbon-20 (C-20 or 20C), an isotope of carbon
* C20, the smallest possible fullerene (a carbon molecule)
* C20 (engineering), a mix of concrete that has a compressive strength of 20 newtons per squ ...
and will be added in
C23.
[
]
Declaration syntax
The declaration syntax is similar to that of variadic function
In mathematics and in computer programming, a variadic function is a function of indefinite arity, i.e., one which accepts a variable number of arguments. Support for variadic functions differs widely among programming languages.
The term ''var ...
s: a sequence of three full stop
The full stop ( Commonwealth English), period (North American English), or full point is a punctuation mark used for several purposes, most often to mark the end of a declarative sentence (as distinguished from a question or exclamation).
A ...
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
C++11 is a version of a joint technical standard, ISO/IEC 14882, by the International Organization for Standardization (ISO) and International Electrotechnical Commission (IEC), for the C++ programming language. C++11 replaced the prior vers ...
standards require at least one argument, but since C++20 C20 or C-20 may refer to:
Science and technology
* Carbon-20 (C-20 or 20C), an isotope of carbon
* C20, the smallest possible fullerene (a carbon molecule)
* C20 (engineering), a mix of concrete that has a compressive strength of 20 newtons per squ ...
and C23 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 Translator (computing), translates computer code written in one programming language (the ''source'' language) into another language (the ''target'' language). The name "compiler" is primaril ...
s support variable-argument macros when compiling C and C++ code: the GNU Compiler Collection
The GNU Compiler Collection (GCC) is a collection of compilers from the GNU Project that support various programming languages, Computer architecture, hardware architectures, and operating systems. The Free Software Foundation (FSF) distributes ...
3.0, Clang
Clang () is a compiler front end for the programming languages C, C++, Objective-C, Objective-C++, and the software frameworks OpenMP, OpenCL, RenderScript, CUDA, SYCL, and HIP. It acts as a drop-in replacement for the GNU Compiler ...
(all versions), Visual Studio 2005
Visual Studio is an integrated development environment (IDE) developed by Microsoft. It is used to develop computer programs including web site, websites, web apps, web services and mobile apps. Visual Studio uses Microsoft software development ...
, C++Builder
C++Builder is a rapid application development (RAD) environment for developing software in the C++ programming language. Originally developed by Borland, it is owned by Embarcadero Technologies, a subsidiary of Idera. C++Builder can compile ...
2006, and Oracle Solaris Studio (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 high-level general-purpose, object-oriented programming language that adds Smalltalk-style message passing (messaging) to the C programming language. Originally developed by Brad Cox and Tom Love in the early 1980s, it was ...
.
Support for the macro to support zero arguments has been added in GNU Compiler Collection
The GNU Compiler Collection (GCC) is a collection of compilers from the GNU Project that support various programming languages, Computer architecture, hardware architectures, and operating systems. The Free Software Foundation (FSF) distributes ...
8, Clang
Clang () is a compiler front end for the programming languages C, C++, Objective-C, Objective-C++, and the software frameworks OpenMP, OpenCL, RenderScript, CUDA, SYCL, and HIP. It acts as a drop-in replacement for the GNU Compiler ...
6, and Visual Studio 2019.
Example
If a printf
-like function were desired, which would take the file and line number from which it was called as arguments, the following solution applies.
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
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
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
In mathematics and in computer programming, a variadic function is a function of indefinite arity, i.e., one which accepts a variable number of arguments. Support for variadic functions differs widely among programming languages.
The term ''var ...
* Variadic template
C (programming language)
C++
ja:可変長引数