Variadic macros/functions

Variadic Macros
Variadic functions or functions taking a variable argument list are rather common in C. For example:
int printf(const char * fmt,…);
C99 also supports variadic macros. A variadic macro looks like this:
#define Trace(…) printf( __VA_ARGS__)
The ellipsis represents a macro’s variable argument list. The reserved __VA_ARGS__ name is used for traversing the arguments passed to the macro. When you call Trace() like this:
Trace(“Y = %dn”, y);
The preprocessor replaces the macro call with:
printf(“Y = %dn”, y);
Because Trace is a variadic macro, you can pass a different number of arguments on every call:
Trace(“test”); // expanded as: printf(“test”);


Leave a Reply

Your email address will not be published. Required fields are marked *