How to write macros with variable number of parameters

Source: Internet
Author: User
Tags function definition printf variadic

Reproduced in http://blog.csdn.net/aobai219/article/details/6092292

#if

#ifdef

#if defined

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"); 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.

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 a variable parameter. When this type of macro is called, it (here refers to ' ... ') 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, the GNU 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.





How to write macros with variable number of parameters


A popular technique is to define and invoke macros in a separate bracketed "parameter", which becomes the entire argument list for functions like printf () when the macro expands.

    #define DEBUG (args) (printf ("Debug:"), printf args)

    if (n! = 0) Debug (("N is%d/n", N));

The obvious flaw is that callers must remember to use a pair of extra parentheses.

GCC has an extension that allows a function-type macro to accept a variable number of arguments. But that's not the norm. Another possible solution is to use multiple macros (DEBUG1, DEBUG2, etc.) depending on the number of arguments, or use commas to play a trick like this:

    #define DEBUG (args) (printf ("Debug:"), printf (args))
    #define _,

    debug ("I =%d" _ i);

C99 introduces formal support for functional macros with variable number of parameters. Add a symbol at the end of the macro ' prototype ' ... (as in a variable function definition), the pseudo-macro __va_args__ in the macro definition is replaced with a mutable parameter in the call.

Finally, you can always use real functions to accept well-defined mutable parameters

If you need to replace a macro, use a function and a non-functional macro, such as #define printf myprintf.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.