Macro in C Language (translation) 7

Source: Internet
Author: User

The original Article is here

Variable Parameter macro

Macros can receive variable parameter lists, just like functions. The syntax for defining variable parameters is similar to that for a function. Here is an example:

 

 
# Define eprintf (...) fprintf (stderr, _ va_args __)
 
 
 
These macros are called Variable Parameter macros. When a macro is called, all symbols after the first named parameter, including commas, are changed to the parameter list. When the macro is expanded, the _ va_args _ symbol in the macro content is replaced. Therefore, we will expand the following:
 
 
 
Eprintf ("% s: % d:", input_file, lineno) ==> fprintf (stderr, "% s: % d:", input_file, lineno)
 
 
 
Variable parameters are expanded before they are inserted into a macro, just like common parameters. You can use the "#" and "#" operators to character and paste variable parameters (however, "#" requires an important special example, as described below)
 
If your macro is complex, you may need a more descriptive parameter name than _ va_args. CPP allows such objects to exist as extensions. You can write a parameter name before.... This name will be treated as a variable parameter. Therefore, the eprintf macro can be written as follows:
 
 
 
# Define eprintf (ARGs...) fprintf (stderr, argS)
 
 
In this case, you cannot use _ va_args __.
 
In a variable parameter macro, you can use both named parameters and variable parameters. We can define eprintf as follows:
 
 
 
# Define eprintf (format,...) fprintf (stderr, format, _ va_args __)
 
 
 
The formula looks more intuitive. Unfortunately, it is not flexible enough: You must format the string with at least one parameter. In standard C, the comma (,) used to distinguish between named and variable parameters cannot be omitted. If your variable parameter list is empty, you will get a syntax error because a redundant comma is left after the formatted string.
 
 
 
Eprintf ("success! \ N ",) ;==> fprintf (stderr," success! \ N ",);
 
 
 
Gnu cpp has two extensions for processing such events. First, you can leave the variable parameter blank:
 
 
 
Eprintf ("success! \ N ") ==> fprintf (stderr," success! \ N ",);
 
 
 
Second, when you paste the comma and Variable Parameter'##The 'symbol has a special meaning. If you write such a statement
 
 
# Define eprintf (format,...) fprintf (stderr, format, ##__ va_args __)
 
 
In addition, if the variable parameter list is empty when eprintf is used, extra commas will be deleted by the field. If you leave all the parameters blank, or use'##', The extra comma will not be deleted.
 
 
 
Eprintf ("success! \ N ") ==> fprintf (stderr," success! \ N ");
 
 
 
The above expression is ambiguous: the unique parameter "success! \ N "is a variable parameter or a named parameter. It is meaningless to distinguish between null parameters and missing parameters. The c99 Standard specifies that the comma must be retained, but the existing GCC extension does not. Therefore, CPP should keep the comma when c99 is observed. In other cases, it should be deleted.
 
The only situation that c99 requires _ va_args _ to appear is in the replacement list of Variable Parameter macros. It cannot be used as macro name, macro parameter name, or in two different types of macros. It may also be not allowed to be used in open files. In this regard, the C standard is not clearly defined. We recommend that you do not use _ va_args _ unless necessary __.
 
Variable Parameter macros are new features in c99. Gnu cpp has supported this feature for a long time, but it is only in the case of a named parameter (similarARGs...', Not'...'Or_ Va_args __). If you are concerned about the compatibility with the old version of GCC, you must use the named parameter list. On the other hand, if you are very concerned aboutProgramIf it is compatible, nothing except _ va_args _ is used.
In earlier versions, CPP is more widely used with the comma Delete feature. In the current version, this feature has been restricted because it must comply with the c99 standard as much as possible. To meet both c99 and previous GCC'##'The previous symbol must be a comma and there must be spaces before the comma:
 
 
# Define eprintf (format, argS...) fprintf (stderr, format, # ARGs)
 
 

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.