Explanation of three points in C language: variadic

Source: Internet
Author: User
Tags variadic

3.6 Variadic Macros

A macro can is declared to accept a variable number of arguments much as a function can. The syntax for defining the macro was similar to that of a function. Here are an example:

     #define EPRINTF (...) fprintf (stderr, __va_args__)

This kind of the macro is called variadic. When the macro was invoked, all of the tokens in it argument list after the last named argument (this macro has none), includ ing any commas, become the variable argument. This sequence of tokens replaces the identifier in the __VA_ARGS__ macro body wherever it appears. Thus, we have this expansion:

     eprintf ("%s:%d:", Input_file, Lineno)          ==>  fprintf (stderr, "%s:%d:", Input_file, Lineno)

The variable argument is completely macro-expanded before it's inserted into the macro expansion, just as an ordinary a Rgument. # ## operators to stringify the variable argument or to paste its leading or trailing token with another token. (but see below for a important special case for " ## .)

IF your macro is complicated, your may want a more descriptive name for the variable argument than __VA_ARGS__ . CPP permits this, as an extension. You may write a argument name immediately before the " ... ; that name is used for the variable argument. The eprintf macro above could be written

     #define EPRINTF (args ...) fprintf (stderr, args)

Using this extension. You cannot use and this extension in the __VA_ARGS__ same macro.

You can has named arguments as well as variable arguments in a Variadic macro. We could define like this eprintf , instead:

     #define EPRINTF (format, ...) fprintf (stderr, format, __va_args__)

This formulation looks more descriptive, but unfortunately it was less flexible:you must now supply at least one argument After the format string. In standard C, you cannot omit the comma separating the named argument from the variable arguments. Furthermore, if you leave the variable argument empty, you'll get a syntax error, because there'll be a extra comma a fter the format string.

     eprintf ("success!\n",);          ==> fprintf (stderr, "success!\n",);

GNU CPP have a pair of extensions which deal with this problem. First, you is allowed to leave the variable argument out entirely:

     eprintf ("success!\n")          ==> fprintf (stderr, "success!\n",);

Second, the ' ## token paste operator have a special meaning when placed between a comma and a variable argument. If you write

     #define EPRINTF (format, ...) fprintf (stderr, format, # #__VA_ARGS__)

And the variable argument is left out when eprintf the macro was used, then the comma before the ' 'll be ## deleted. This does isn't happen if you pass an empty argument, nor does it happen if the token preceding "is ## anything Other than a comma.

     eprintf ("success!\n")          ==> fprintf (stderr, "success!\n");

The above explanation is ambiguous on the case where the only macro parameter is a variable arguments parameter, as it is meaningless to try to distinguish whether no argument at all are an empty argument or a missing argument. CPP retains the comma when conforming to a specific C. Otherwise the comma is dropped as a extension to the standard.

The C standard mandates, the only place the identifier __VA_ARGS__ can appear are in the replacement list of a Variadic macro . It May is used as a macro name, macro argument name, or within a different type of macro. It may also is forbidden in open text; The standard is ambiguous. We recommend you avoid the using it except for its defined purpose.

Variadic macros became a standard part of the C language with C99. GNU CPP previously supported them with a named variable argument (" args... , not" and ... __VA_ARGS__ ), which is still supporte D for backward compatibility.

Explanation of three points in C language: variadic

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.