1 | <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
|
---|
2 | <html>
|
---|
3 | <!-- Copyright (C) 2022 Richard Stallman and Free Software Foundation, Inc.
|
---|
4 |
|
---|
5 | (The work of Trevis Rothwell and Nelson Beebe has been assigned or
|
---|
6 | licensed to the FSF.)
|
---|
7 |
|
---|
8 | Permission is granted to copy, distribute and/or modify this document
|
---|
9 | under the terms of the GNU Free Documentation License, Version 1.3 or
|
---|
10 | any later version published by the Free Software Foundation; with the
|
---|
11 | Invariant Sections being "GNU General Public License," with the
|
---|
12 | Front-Cover Texts being "A GNU Manual," and with the Back-Cover
|
---|
13 | Texts as in (a) below. A copy of the license is included in the
|
---|
14 | section entitled "GNU Free Documentation License."
|
---|
15 |
|
---|
16 | (a) The FSF's Back-Cover Text is: "You have the freedom to copy and
|
---|
17 | modify this GNU manual. Buying copies from the FSF supports it in
|
---|
18 | developing GNU and promoting software freedom." -->
|
---|
19 | <!-- Created by GNU Texinfo 6.7, http://www.gnu.org/software/texinfo/ -->
|
---|
20 | <head>
|
---|
21 | <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
|
---|
22 | <title>Variadic Macros (GNU C Language Manual)</title>
|
---|
23 |
|
---|
24 | <meta name="description" content="Variadic Macros (GNU C Language Manual)">
|
---|
25 | <meta name="keywords" content="Variadic Macros (GNU C Language Manual)">
|
---|
26 | <meta name="resource-type" content="document">
|
---|
27 | <meta name="distribution" content="global">
|
---|
28 | <meta name="Generator" content="makeinfo">
|
---|
29 | <link href="index.html" rel="start" title="Top">
|
---|
30 | <link href="Symbol-Index.html" rel="index" title="Symbol Index">
|
---|
31 | <link href="index.html#SEC_Contents" rel="contents" title="Table of Contents">
|
---|
32 | <link href="Macros.html" rel="up" title="Macros">
|
---|
33 | <link href="Predefined-Macros.html" rel="next" title="Predefined Macros">
|
---|
34 | <link href="Concatenation.html" rel="prev" title="Concatenation">
|
---|
35 | <style type="text/css">
|
---|
36 | <!--
|
---|
37 | a.summary-letter {text-decoration: none}
|
---|
38 | blockquote.indentedblock {margin-right: 0em}
|
---|
39 | div.display {margin-left: 3.2em}
|
---|
40 | div.example {margin-left: 3.2em}
|
---|
41 | div.lisp {margin-left: 3.2em}
|
---|
42 | kbd {font-style: oblique}
|
---|
43 | pre.display {font-family: inherit}
|
---|
44 | pre.format {font-family: inherit}
|
---|
45 | pre.menu-comment {font-family: serif}
|
---|
46 | pre.menu-preformatted {font-family: serif}
|
---|
47 | span.nolinebreak {white-space: nowrap}
|
---|
48 | span.roman {font-family: initial; font-weight: normal}
|
---|
49 | span.sansserif {font-family: sans-serif; font-weight: normal}
|
---|
50 | ul.no-bullet {list-style: none}
|
---|
51 | -->
|
---|
52 | </style>
|
---|
53 |
|
---|
54 |
|
---|
55 | </head>
|
---|
56 |
|
---|
57 | <body lang="en">
|
---|
58 | <span id="Variadic-Macros"></span><div class="header">
|
---|
59 | <p>
|
---|
60 | Next: <a href="Predefined-Macros.html" accesskey="n" rel="next">Predefined Macros</a>, Previous: <a href="Concatenation.html" accesskey="p" rel="prev">Concatenation</a>, Up: <a href="Macros.html" accesskey="u" rel="up">Macros</a> [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="Symbol-Index.html" title="Index" rel="index">Index</a>]</p>
|
---|
61 | </div>
|
---|
62 | <hr>
|
---|
63 | <span id="Variadic-Macros-1"></span><h4 class="subsection">26.5.6 Variadic Macros</h4>
|
---|
64 | <span id="index-variable-number-of-arguments"></span>
|
---|
65 | <span id="index-macros-with-variable-arguments"></span>
|
---|
66 | <span id="index-variadic-macros"></span>
|
---|
67 |
|
---|
68 | <p>A macro can be declared to accept a variable number of arguments much as
|
---|
69 | a function can. The syntax for defining the macro is similar to that of
|
---|
70 | a function. Here is an example:
|
---|
71 | </p>
|
---|
72 | <div class="example">
|
---|
73 | <pre class="example">#define eprintf(…) fprintf (stderr, __VA_ARGS__)
|
---|
74 | </pre></div>
|
---|
75 |
|
---|
76 | <p>This kind of macro is called <em>variadic</em>. When the macro is invoked,
|
---|
77 | all the tokens in its argument list after the last named argument (this
|
---|
78 | macro has none), including any commas, become the <em>variable
|
---|
79 | argument</em>. This sequence of tokens replaces the identifier
|
---|
80 | <code><span class="nolinebreak">__VA_ARGS__</span><!-- /@w --></code> in the macro body wherever it appears. Thus, we
|
---|
81 | have this expansion:
|
---|
82 | </p>
|
---|
83 | <div class="example">
|
---|
84 | <pre class="example">eprintf ("%s:%d: ", input_file, lineno)
|
---|
85 | → fprintf (stderr, "%s:%d: ", input_file, lineno)
|
---|
86 | </pre></div>
|
---|
87 |
|
---|
88 | <p>The variable argument is completely macro-expanded before it is inserted
|
---|
89 | into the macro expansion, just like an ordinary argument. You may use
|
---|
90 | the <code>#</code> and <code>##</code> operators to stringify the variable argument
|
---|
91 | or to paste its leading or trailing token with another token. (But see
|
---|
92 | below for an important special case for <code>##</code>.)
|
---|
93 | </p>
|
---|
94 | <p><strong>Warning:</strong> don’t use the identifier <code><span class="nolinebreak">__VA_ARGS__</span><!-- /@w --></code>
|
---|
95 | for anything other than this.
|
---|
96 | </p>
|
---|
97 | <p>If your macro is complicated, you may want a more descriptive name for
|
---|
98 | the variable argument than <code><span class="nolinebreak">__VA_ARGS__</span><!-- /@w --></code>. You can write an
|
---|
99 | argument name immediately before the ‘<samp>…</samp>’; that name is used
|
---|
100 | for the variable argument.<a id="DOCF9" href="#FOOT9"><sup>9</sup></a> The
|
---|
101 | <code>eprintf</code> macro above could be written thus:
|
---|
102 | </p>
|
---|
103 | <div class="example">
|
---|
104 | <pre class="example">#define eprintf(args…) fprintf (stderr, args)
|
---|
105 | </pre></div>
|
---|
106 |
|
---|
107 | <p>A variadic macro can have named arguments as well as variable
|
---|
108 | arguments, so <code>eprintf</code> can be defined like this, instead:
|
---|
109 | </p>
|
---|
110 | <div class="example">
|
---|
111 | <pre class="example">#define eprintf(format, …) \
|
---|
112 | fprintf (stderr, format, __VA_ARGS__)
|
---|
113 | </pre></div>
|
---|
114 |
|
---|
115 | <p>This formulation is more descriptive, but what if you want to specify
|
---|
116 | a format string that takes no arguments? In GNU C, you can omit the
|
---|
117 | comma before the variable arguments if they are empty, but that puts
|
---|
118 | an extra comma in the expansion:
|
---|
119 | </p>
|
---|
120 | <div class="example">
|
---|
121 | <pre class="example">eprintf ("success!\n")
|
---|
122 | → fprintf(stderr, "success!\n", );
|
---|
123 | </pre></div>
|
---|
124 |
|
---|
125 | <p>That’s an error in the call to <code>fprintf</code>.
|
---|
126 | </p>
|
---|
127 | <p>To get rid of that comma, the <code>##</code> token paste operator has a
|
---|
128 | special meaning when placed between a comma and a variable
|
---|
129 | argument.<a id="DOCF10" href="#FOOT10"><sup>10</sup></a> If you write
|
---|
130 | </p>
|
---|
131 | <div class="example">
|
---|
132 | <pre class="example">#define eprintf(format, …) \
|
---|
133 | fprintf (stderr, format, ##__VA_ARGS__)
|
---|
134 | </pre></div>
|
---|
135 |
|
---|
136 | <p>then use the macro <code>eprintf</code> with empty variable arguments,
|
---|
137 | <code>##</code> deletes the preceding comma.
|
---|
138 | </p>
|
---|
139 | <div class="example">
|
---|
140 | <pre class="example">eprintf ("success!\n")
|
---|
141 | → fprintf(stderr, "success!\n");
|
---|
142 | </pre></div>
|
---|
143 |
|
---|
144 | <p>This does <em>not</em> happen if you pass an empty argument, nor does it
|
---|
145 | happen if the token preceding <code>##</code> is anything other than a
|
---|
146 | comma.
|
---|
147 | </p>
|
---|
148 | <p>When the only macro parameter is a variable arguments parameter, and
|
---|
149 | the macro call has no argument at all, it is not obvious whether that
|
---|
150 | means an empty argument or a missing argument. Should the comma be
|
---|
151 | kept, or deleted? The C standard says to keep the comma, but the
|
---|
152 | preexisting GNU C extension deleted the comma. Nowadays, GNU C
|
---|
153 | retains the comma when implementing a specific C standard, and deletes
|
---|
154 | it otherwise.
|
---|
155 | </p>
|
---|
156 | <p>C99 mandates that the only place the identifier <code><span class="nolinebreak">__VA_ARGS__</span><!-- /@w --></code>
|
---|
157 | can appear is in the replacement list of a variadic macro. It may not
|
---|
158 | be used as a macro name, macro parameter name, or within a different
|
---|
159 | type of macro. It may also be forbidden in open text; the standard is
|
---|
160 | ambiguous. We recommend you avoid using that name except for its
|
---|
161 | special purpose.
|
---|
162 | </p>
|
---|
163 | <p>Variadic macros where you specify the parameter name is a GNU C
|
---|
164 | feature that has been supported for a long time. Standard C, as of
|
---|
165 | C99, supports only the form where the parameter is called
|
---|
166 | <code><span class="nolinebreak">__VA_ARGS__</span><!-- /@w --></code>. For portability to previous versions of GNU C
|
---|
167 | you should use only named variable argument parameters. On the other
|
---|
168 | hand, for portability to other C99 compilers, you should use only
|
---|
169 | <code><span class="nolinebreak">__VA_ARGS__</span><!-- /@w --></code>.
|
---|
170 | </p>
|
---|
171 | <div class="footnote">
|
---|
172 | <hr>
|
---|
173 | <h4 class="footnotes-heading">Footnotes</h4>
|
---|
174 |
|
---|
175 | <h5><a id="FOOT9" href="#DOCF9">(9)</a></h3>
|
---|
176 | <p>GNU C extension.</p>
|
---|
177 | <h5><a id="FOOT10" href="#DOCF10">(10)</a></h3>
|
---|
178 | <p>GNU C extension.</p>
|
---|
179 | </div>
|
---|
180 | <hr>
|
---|
181 | <div class="header">
|
---|
182 | <p>
|
---|
183 | Next: <a href="Predefined-Macros.html" accesskey="n" rel="next">Predefined Macros</a>, Previous: <a href="Concatenation.html" accesskey="p" rel="prev">Concatenation</a>, Up: <a href="Macros.html" accesskey="u" rel="up">Macros</a> [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="Symbol-Index.html" title="Index" rel="index">Index</a>]</p>
|
---|
184 | </div>
|
---|
185 |
|
---|
186 |
|
---|
187 |
|
---|
188 | </body>
|
---|
189 | </html>
|
---|