In GNU C, a macro can accept a variable number of arguments, just like a function, such as:
#define PR_DEBUG (Fmt,arg ...) \
PRINTK (Kern_debug fmt,# #arg)
Passing variable parameter tables with variable-parameter macros (Variadic macros)
You may be familiar with the use of variable parameter tables in functions such as:
void printf (const char* format, ...);
Until recently, a mutable parameter table could only be applied to a real function and could not be used in a macro.
The C99 compiler standard has finally changed this situation, allowing you to define variable parameter macros (Variadic macros) so that you can use a macro that has a parameter table that can be changed. The variable-parameter macro looks like this:
#define DEBUG (...) printf (__va_args__)
The default number represents a table of parameters that can be changed. Use the reserved name __va_args__ to pass parameters to the macro. When the macro's call is expanded, the actual parameters are passed to printf () . For example:
Debug ("y =%d\n", y);
The processor replaces the macro call with the following:
printf ("y =%d\n", y);
Because debug () is a variable parameter macro, you can pass a different number of arguments in each call:
Debug ("Test"); //One 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.
easily print debug information with GCC and C99 variable-parameter macros
The variable-parameter macro definition provided by GCC preprocessing is really useful:
#ifdef DEBUG
#define DBGPRINT (Format,args ...) \
fprintf (stderr, format, # #args)
#else
#define DBGPRINT (Format,args ...)
#endif
After this definition, the code can be used Dbgprint, such as Dbgprint ("AAA%s", __file__);. Feel this function compare COOL:EM11:
Here's how to C99:
#define DGBMSG (FMT,...) \
printf (fmt,__va_args__)
The new C99 specification supports variable-parameter macros
The specific use is as follows:
The following is the program code:
#include <stdarg.h> #include <stdio.h>
#define LOGSTRINGS (FM, ...) printf (fm,__va_args__)
int main () {logstrings ("Hello,%d", 10); return 0; }
But now it seems that only GCC supports it.
' # # ' operating instructions in a variable-parameter macro
Macros with variable parameters (Macros with a Variable number of Arguments)
In the 1999 version of the ISO C standard, macros can be defined as functions with variable parameters. The syntax of a macro is similar to the syntax of a function. Here's an example:
#define DEBUG (format, ...) fprintf (stderr, format, __va_args__)
Here, '... ' refers to variable parameters. When such a macro is called, it (here means '... ') is represented as 0 or more symbols, including the comma inside, until the end of the right parenthesis. When called, in the macro body, those symbol sequence sets will replace the __va_args__ identifier inside. For more information, refer to the CPP manual.
GCC always supports complex macros, and it uses a different syntax so that you can give a variable parameter a name, just like any other parameter. For example, the following example:
#define DEBUG (format, args ...) fprintf (stderr, format, args)
This is exactly the same as the macro example defined by the ISO C above, but it is more readable and easier to describe.
GNU CPP also has two more complex macro extensions that support the definition format of the above two formats.
In standard C, you can't omit mutable arguments, but you can pass an empty argument to it. For example, the following macro call is illegal in ISO C because there is no comma behind the string:
Debug ("A message")
GNU CPP allows you to completely ignore mutable parameters in this case. In the example above, the compiler still has a problem (complain), because after the macro expands, there will be an extra comma behind the string.
To solve this problem,CPP uses a special '# # ' operation. The writing format is:
#define DEBUG (format, ...) fprintf (stderr, format, # __va_args__)
Here, if the variable parameter is ignored or empty, the '# # ' operation causes the preprocessor (preprocessor) to remove the comma in front of it. If you do provide some mutable parameters when you call the macro, theGNU CPP will work as well, and it will put the mutable parameters behind the comma. Like other pasted macro parameters, these parameters are not an extension of the macro.
Variable parameter Macro __va_args__