Variadic macro
Encyclopedia
A variadic macro is a feature of the C preprocessor
C preprocessor
The C preprocessor is the preprocessor for the C and C++ computer programming languages. The preprocessor handles directives for source file inclusion , macro definitions , and conditional inclusion ....

 whereby a macro may be declared to accept a varying number of arguments
Parameter (computer science)
In computer programming, a parameter is a special kind of variable, used in a subroutine to refer to one of the pieces of data provided as input to the subroutine. These pieces of data are called arguments...

.

Variable-argument macros were introduced in the ISO/IEC 9899:1999 (C99
C99
C99 is a modern dialect of the C programming language. It extends the previous version with new linguistic and library features, and helps implementations make better use of available computer hardware and compiler technology.-History:...

) revision of the C
C (programming language)
C is a general-purpose computer programming language developed between 1969 and 1973 by Dennis Ritchie at the Bell Telephone Laboratories for use with the Unix operating system....

 programming language
Programming language
A programming language is an artificial language designed to communicate instructions to a machine, particularly a computer. Programming languages can be used to create programs that control the behavior of a machine and/or to express algorithms precisely....

 standard in 1999. They were also introduced in ISO/IEC 14882:2011 (C++11) revision of the C++ programming language
Programming language
A programming language is an artificial language designed to communicate instructions to a machine, particularly a computer. Programming languages can be used to create programs that control the behavior of a machine and/or to express algorithms precisely....

 standard in 2011.

Declaration syntax

The declaration syntax is similar to that of variadic function
Variadic function
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....

s: an ellipsis "..." is used to indicate that zero or more arguments may be passed. During macro expansion each occurrence of the special identifier __VA_ARGS__ in the macro replacement list is replaced by the passed arguments. The macro processor prevents dangling commas that might otherwise occur when zero arguments are substituted.

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.

Support

The GNU Compiler Collection
GNU Compiler Collection
The GNU Compiler Collection is a compiler system produced by the GNU Project supporting various programming languages. GCC is a key component of the GNU toolchain...

, since 3.0, C++Builder 2006 and Visual Studio 2005 http://msdn2.microsoft.com/en-US/library/ms177415(VS.80).aspx support variable-argument macros, both when compiling C and C++
C++
C++ is a statically typed, free-form, multi-paradigm, compiled, general-purpose programming language. It is regarded as an intermediate-level language, as it comprises a combination of both high-level and low-level language features. It was developed by Bjarne Stroustrup starting in 1979 at Bell...

 code. In addition, GCC supports variadic macros when compiling Objective-C
Objective-C
Objective-C is a reflective, object-oriented programming language that adds Smalltalk-style messaging to the C programming language.Today, it is used primarily on Apple's Mac OS X and iOS: two environments derived from the OpenStep standard, though not compliant with it...

. Sun Studio C and C++ have had support since Forte Developer 6 update 2 (C++ version 5.3).

Example

If a printf
Printf
Printf format string refers to a control parameter used by a class of functions typically associated with some types of programming languages. The format string specifies a method for rendering an arbitrary number of varied data type parameter into a string...

-like function
Subroutine
In computer science, a subroutine is a portion of code within a larger program that performs a specific task and is relatively independent of the remaining code....

 dprintf were desired, which would take the file and line number from which it was called as arguments, the following macro might be used:

void realdprintf (char const *file, int line, ...);
  1. define dprintf(...) realdprintf(__FILE__, __LINE__, __VA_ARGS__)


dprintf could then be called as:

dprintf("Hello, world");

which expands to:

realdprintf(__FILE__, __LINE__, "Hello, world");

or:

dprintf("%d + %d = %d", 2, 2, 5);

which expands to:

realdprintf(__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
Stdarg.h
stdarg.h is a header in the C standard library of the C programming language that allows functions to accept an indefinite number of arguments. It provides facilities for stepping through a list of function arguments of unknown number and type...

 functionality of C/C++, and have the function call vprintf instead.
The source of this article is wikipedia, the free encyclopedia.  The text of this article is licensed under the GFDL.
 
x
OK