In GNU C, a macro can accept a variable number of arguments, just like a function, for example:
1 2 |
#define PR_DEBUG (Fmt,arg ...) \ printk (Kern_debug fmt, # #arg) |
Passing a variable parameter table with a variable parameter macro (variadic macros)
You may be familiar with the use of variable parameter tables in a function, such as:
1 |
void printf (const char * format, ...); |
Until recently, the variable parameter table can only be applied to real functions and cannot be used in macros.
The C99 compiler standard has finally changed this situation, allowing you to define variable parameter macros (variadic macros) so you can use macros that have parameter tables that can be changed. Variable parameter macros look like this:
1 |
#define DEBUG (...) printf (__va_args__) |
The default number represents a parameter table that can be changed. Use the reserved name __va_args__ to pass parameters to the macro. When the call to the macro is expanded, the actual arguments are passed to printf (). For example:
The processor replaces the invocation of the macro with the following:
Because Debug () is a variable parameter macro, you can pass a different number of arguments in each call:
1 |
Debug ("Test"); A parameter |
Variable parameter macros are not officially supported by Ansi/iso C + +. Therefore, you should check your compiler to see if it supports this technology.
More easily print debugging information with GCC and C99 variable parameter macros
GCC preprocessing provides variable parameter macro definitions that are really handy: