Variadic Macro Extensions

Variadic macro extensions refer to two extensions to C99 related to macros with variable number of arguments. One extension is a mechanism for renaming the variable argument identifier from __VA_ARGS__ to a user-defined identifier. This extension is orthogonal to C99. The other extension provides a way to remove the dangling comma in a variadic macro when no variable arguments are specified. This extension is non-orthogonal. Both extensions have been implemented to facilitate porting programs developed with GNU C and C++.

C++ This implementation of C++ extends Standard C++ and C++98 to support the variadic macro extensions for compatibility with C.

An Identifier Instead of __VA_ARGS__

The following examples demonstrate the use of an identifier in place of __VA_ARGS__. The first definition of the macro debug exemplifies the usual usage of __VA_ARGS__. The second definition shows the use of the identifier args in place of __VA_ARGS__.

#define debug1(format, ...)  printf(format, __VA_ARGS__)
#define debug2(format, args ...)  printf(format, args)

Invocation Result of Macro Expansion
debug1("Hello %s\n","World"); printf("Hello %s\n","World");
debug2("Hello %s\n","World"); printf("Hello %s\n","World");

Trailing Comma Removal

The preprocessor removes the trailing comma if the variable arguments to a function macro are omitted or empty and the comma followed by ## precedes the variable argument identifier in the function macro definition. IBM Copyright 2003