C Language Macro Definition # # for variable parameters

Source: Internet
Author: User

GCC supports complex macros, which use a different syntax so that you can give a variable parameter a name, like any other parameter, such as:

Reference # define debug (format, args ...) fprintf (stderr, format, args)


This definition is more readable and easier to describe. Complete test Code:

cite # include <stdio.h>

#define DEBUG (format, args ...) fprintf (stderr, format, args)

int Main ()
{
char a[20] = "Hello world \ n";
int i = 10;
Debug ("I =%d,%s", I, a);

return 0;
}


Run output :

reference [email protected]:~/c/micro>./mic.exe
i = ten, Hello
world


But the above definition still has a little problem, if the above code is changed to the following:

cite # include <stdio.h>

#define DEBUG (format, args ...) fprintf (stderr, format, args)

int Main ()
{

Debug ("Hello World \ n");

return 0;
}


the following error will be prompted at compile time :

references [email protected]:~/c/micro> gcc-g mic.c-o Mic.exe
mic.c:in function ' main ':
mic.c:10:error:expected expression before ') ' token


Tip Missing right parenthesis. This is because, when the macro expands, "Hello world\n" is inserted into format, however, followed by a comma, but this comma is expected to have the args parameter, but here is not, so the macro cannot be expanded completely, and therefore cannot be compiled through. Then, change the macro definition:

Cite # include <stdio.h>

#define DEBUG (format, args ...) fprintf (stderr, format, # #args)

int Main ()
{
Debug ("Hello World \ n");

return 0;
}


At this point, the compiler runs and outputs:

references [email protected]:~/c/micro> gcc-g mic.c-o Mic.exe
[Email protected]:~/c/micro>./mic.exe
Hello
World


The compilation passes, and the output is normal. The above code adds two # # # # in front of the args in fprintf ().
## the role of the number is :
If the variable parameter section (args ...) is ignored or empty, then the "# #" Operation causes the preprocessor (preprocessor) to remove the comma in front of it. If you do provide some mutable parameters when calling a macro, GNU C will also work, and it will put the mutable parameters behind the comma, and if it is not provided, it will automatically remove the preceding comma so that the macro ends up expanding----the right parenthesis is added.

In addition, if you follow the definition of C99, change the macro to:

Reference # define debug (format, args ...) fprintf (stderr, format, # #__VA_ARGS__)


Then the compilation will go wrong:

references [email protected]:~/c/micro> gcc-g mic.c-o Mic.exe
Mic.c:3:58:warning: __va_args__ can only appear in the expansion of a C99 variadic macro
Mic.c:9:1:error:pasting "," and "__va_args__" does not give a valid preprocessing token
mic.c:in function ' main ':
Mic.c:9: Error: ' __va_args__ ' undeclared (first use of this function)
Mic.c:9: Error: (each undeclared identifier was reported only once
Mic.c:9: Error:for Each function it appears in.)


The reason is that args ... and # #__VA_ARGS__ are mismatched, the correct match should be:

Reference # define debug (format, ...) fprintf (stderr, format, # #__VA_ARGS__)


Attention... The ellipsis corresponds to the __va_args__.

In general, a popular method for defining variable-parameter macros, such as:

Reference # define DEBUG (args) (printf ("Debug:"), printf args)
if (n! = 0) DEBUG (("n is%d \n");


One drawback of this approach is to remember a pair of extra parentheses.

C Language Macro Definition # # for variable parameters

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.