biology daily - the biology and biochemistry encyclopedia
biology daily articles and research Encyclopedia Dictionary Forums biology research links Weblinks Pictures Articles Blogs Newsletter

Variadic macro

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 revision of the C Programming Language standard in 1999.

Declaration Syntax

The declaration syntax is similar to that of variadic functions: an ellipsis "..." is used to indicate that one or more (possibly empty) arguments must be passed. During macro expansion each occurrence of the special identifier __VA_ARGS__ in the macro replacement list is replaced by the passed arguments.

Example

If a printf-like function 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, char const *fmt, ...); 
#define dprintf(...) realdprintf(__FILE__, __LINE__, __VA_ARGS__);

dprintf() could then be called as:

dprintf("Hello, world");
/* Expands to: 
 *     realdprintf(__FILE__, __LINE__, "Hello, world"); 
 */

or:

dprintf("%d + %d = %d", 2, 2, 5);
/* Expands to: 
 *     realdprintf(__FILE__, __LINE__, "%d + %d = %d", 2, 2, 5);
 */

See Also

Variadic Function



07-14-2008 23:18:10
The contents of this article are licensed from Wikipedia.org under the GNU Free Documentation License. How to see transparent copy
BiologyDaily.com 2005. Legal info   Privacy