Jump to content

Variadic macro in the C preprocessor

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by O11c (talk | contribs) at 02:40, 23 December 2011 (Example: Change dprintf to dbgprintf since dprintf is in POSIX with different semantics than implemented here). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.

A variadic macro is a feature of the C preprocessor whereby a macro may be declared to accept a varying number of arguments.

Variable-argument macros were introduced in the ISO/IEC 9899:1999 (C99) revision of the C programming language standard in 1999. They were also introduced in ISO/IEC 14882:2011 (C++11) revision of the C++ programming language standard in 2011.[1]

Declaration syntax

The declaration syntax is similar to that of variadic functions: 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.[2]

Support

The GNU Compiler Collection, since 3.0, C++Builder 2006 and Visual Studio 2005 [1] support variable-argument macros, both when compiling C and C++ code. In addition, GCC supports variadic macros when compiling Objective-C. Sun Studio C and C++ have had support since Forte Developer 6 update 2 (C++ version 5.3).[3]

Example

If a printf-like function dbgprintf() were desired, which would take the file and line number from which it was called as arguments, the following macro might be used:


void realdbgprintf (char const *file, int line, ...); 
#define dbgprintf(...) realdbgprintf(__FILE__, __LINE__, __VA_ARGS__)

dbgprintf() could then be called as:

dbgprintf("Hello, world");

which expands to:

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

or:

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.

References

  1. ^ Working draft changes for C99 preprocessor synchronization - http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2004/n1653.htm
  2. ^ Laurent Deniau (2006-01-16). "__VA_NARG__". Newsgroupcomp.std.c. dqgm2f$ije$1@sunnews.cern.ch.
  3. ^ Sun Studio feature comparison - http://developers.sun.com/sunstudio/support/CCcompare.html

See also