⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 variadic-macros.html

📁 gcc手册
💻 HTML
字号:
<html lang="en">

<head>

<title>The C Preprocessor</title>

<meta http-equiv="Content-Type" content="text/html">

<meta name="description" content="The C Preprocessor">

<meta name="generator" content="makeinfo 4.3">

<link href="http://www.gnu.org/software/texinfo/" rel="generator-home">

<!--

Copyright &copy; 1987, 1989, 1991, 1992, 1993, 1994, 1995, 1996,

1997, 1998, 1999, 2000, 2001, 2002, 2003

Free Software Foundation, Inc.



   <p>Permission is granted to copy, distribute and/or modify this document

under the terms of the GNU Free Documentation License, Version 1.1 or

any later version published by the Free Software Foundation.  A copy of

the license is included in the

section entitled "GNU Free Documentation License".



   <p>This manual contains no Invariant Sections.  The Front-Cover Texts are

(a) (see below), and the Back-Cover Texts are (b) (see below).



   <p>(a) The FSF's Front-Cover Text is:



   <p>A GNU Manual



   <p>(b) The FSF's Back-Cover Text is:



   <p>You have freedom to copy and modify this GNU Manual, like GNU

     software.  Copies published by the Free Software Foundation raise

     funds for GNU development. 

-->

</head>

<body>

<div class="node">

<p>

Node:<a name="Variadic%20Macros">Variadic Macros</a>,

Next:<a rel="next" accesskey="n" href="Predefined-Macros.html#Predefined%20Macros">Predefined Macros</a>,

Previous:<a rel="previous" accesskey="p" href="Concatenation.html#Concatenation">Concatenation</a>,

Up:<a rel="up" accesskey="u" href="Macros.html#Macros">Macros</a>

<hr><br>

</div>



<h3 class="section">Variadic Macros</h3>



   <p>A macro can be declared to accept a variable number of arguments much as

a function can.  The syntax for defining the macro is similar to that of

a function.  Here is an example:



<pre class="example">     #define eprintf(...) fprintf (stderr, __VA_ARGS__)

     </pre>



   <p>This kind of macro is called <dfn>variadic</dfn>.  When the macro is invoked,

all the tokens in its argument list after the last named argument (this

macro has none), including any commas, become the <dfn>variable

argument</dfn>.  This sequence of tokens replaces the identifier

<code>__VA_ARGS__</code> in the macro body wherever it appears.  Thus, we

have this expansion:



<pre class="example">     eprintf ("%s:%d: ", input_file, lineno)

          ==&gt;  fprintf (stderr, "%s:%d: ", input_file, lineno)

     </pre>



   <p>The variable argument is completely macro-expanded before it is inserted

into the macro expansion, just like an ordinary argument.  You may use

the <code>#</code> and <code>##</code> operators to stringify the variable argument

or to paste its leading or trailing token with another token.  (But see

below for an important special case for <code>##</code>.)



   <p>If your macro is complicated, you may want a more descriptive name for

the variable argument than <code>__VA_ARGS__</code>.  CPP permits

this, as an extension.  You may write an argument name immediately

before the <code>...</code>; that name is used for the variable argument. 

The <code>eprintf</code> macro above could be written



<pre class="example">     #define eprintf(args...) fprintf (stderr, args)

     </pre>



<p>using this extension.  You cannot use <code>__VA_ARGS__</code> and this

extension in the same macro.



   <p>You can have named arguments as well as variable arguments in a variadic

macro.  We could define <code>eprintf</code> like this, instead:



<pre class="example">     #define eprintf(format, ...) fprintf (stderr, format, __VA_ARGS__)

     </pre>



<p>This formulation looks more descriptive, but unfortunately it is 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 will get a syntax error, because

there will be an extra comma after the format string.



<pre class="example">     eprintf("success!\n", );

          ==&gt; fprintf(stderr, "success!\n", );

     </pre>



   <p>GNU CPP has a pair of extensions which deal with this problem.  First,

you are allowed to leave the variable argument out entirely:



<pre class="example">     eprintf ("success!\n")

          ==&gt; fprintf(stderr, "success!\n", );

     </pre>



<p>Second, the <code>##</code> token paste operator has a special meaning when

placed between a comma and a variable argument.  If you write



<pre class="example">     #define eprintf(format, ...) fprintf (stderr, format, ##__VA_ARGS__)

     </pre>



<p>and the variable argument is left out when the <code>eprintf</code> macro is

used, then the comma before the <code>##</code> will be deleted.  This does

<em>not</em> happen if you pass an empty argument, nor does it happen if

the token preceding <code>##</code> is anything other than a comma.



<pre class="example">     eprintf ("success!\n")

          ==&gt; fprintf(stderr, "success!\n");

     </pre>



<p>The above explanation is ambiguous about 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 is an empty argument or

a missing argument.  In this case the C99 standard is clear that the

comma must remain, however the existing GCC extension used to swallow

the comma.  So CPP retains the comma when conforming to a specific C

standard, and drops it otherwise.



   <p>C99 mandates that the only place the identifier <code>__VA_ARGS__</code>

can appear is in the replacement list of a variadic macro.  It may not

be used as a macro name, macro argument name, or within a different type

of macro.  It may also be forbidden in open text; the standard is

ambiguous.  We recommend you avoid using it except for its defined

purpose.



   <p>Variadic macros are a new feature in C99.  GNU CPP has supported them

for a long time, but only with a named variable argument

(<code>args...</code>, not <code>...</code> and <code>__VA_ARGS__</code>).  If you are

concerned with portability to previous versions of GCC, you should use

only named variable arguments.  On the other hand, if you are concerned

with portability to other conforming implementations of C99, you should

use only <code>__VA_ARGS__</code>.



   <p>Previous versions of CPP implemented the comma-deletion extension

much more generally.  We have restricted it in this release to minimize

the differences from C99.  To get the same effect with both this and

previous versions of GCC, the token preceding the special <code>##</code> must

be a comma, and there must be white space between that comma and

whatever comes immediately before it:



<pre class="example">     #define eprintf(format, args...) fprintf (stderr, format , ##args)

     </pre>



<p>See <a href="Differences-from-previous-versions.html#Differences%20from%20previous%20versions">Differences from previous versions</a>, for the gory details.



   </body></html>



⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -