source: public/doc/gnu-c/Variadic-Macros.html@ 02598c2

Last change on this file since 02598c2 was 02598c2, checked in by Mikhail Kirillov <w96k@…>, on Oct 6, 2022 at 12:36:29 PM

Add gnu-c

  • Property mode set to 100644
File size: 8.4 KB
Line 
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
6licensed to the FSF.)
7
8Permission is granted to copy, distribute and/or modify this document
9under the terms of the GNU Free Documentation License, Version 1.3 or
10any later version published by the Free Software Foundation; with the
11Invariant Sections being "GNU General Public License," with the
12Front-Cover Texts being "A GNU Manual," and with the Back-Cover
13Texts as in (a) below. A copy of the license is included in the
14section entitled "GNU Free Documentation License."
15
16(a) The FSF's Back-Cover Text is: "You have the freedom to copy and
17modify this GNU manual. Buying copies from the FSF supports it in
18developing 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<!--
37a.summary-letter {text-decoration: none}
38blockquote.indentedblock {margin-right: 0em}
39div.display {margin-left: 3.2em}
40div.example {margin-left: 3.2em}
41div.lisp {margin-left: 3.2em}
42kbd {font-style: oblique}
43pre.display {font-family: inherit}
44pre.format {font-family: inherit}
45pre.menu-comment {font-family: serif}
46pre.menu-preformatted {font-family: serif}
47span.nolinebreak {white-space: nowrap}
48span.roman {font-family: initial; font-weight: normal}
49span.sansserif {font-family: sans-serif; font-weight: normal}
50ul.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>
60Next: <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> &nbsp; [<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
69a function can. The syntax for defining the macro is similar to that of
70a function. Here is an example:
71</p>
72<div class="example">
73<pre class="example">#define eprintf(&hellip;) fprintf (stderr, __VA_ARGS__)
74</pre></div>
75
76<p>This kind of macro is called <em>variadic</em>. When the macro is invoked,
77all the tokens in its argument list after the last named argument (this
78macro has none), including any commas, become the <em>variable
79argument</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
81have this expansion:
82</p>
83<div class="example">
84<pre class="example">eprintf (&quot;%s:%d: &quot;, input_file, lineno)
85 &rarr; fprintf (stderr, &quot;%s:%d: &quot;, input_file, lineno)
86</pre></div>
87
88<p>The variable argument is completely macro-expanded before it is inserted
89into the macro expansion, just like an ordinary argument. You may use
90the <code>#</code> and <code>##</code> operators to stringify the variable argument
91or to paste its leading or trailing token with another token. (But see
92below for an important special case for <code>##</code>.)
93</p>
94<p><strong>Warning:</strong> don&rsquo;t use the identifier <code><span class="nolinebreak">__VA_ARGS__</span><!-- /@w --></code>
95for anything other than this.
96</p>
97<p>If your macro is complicated, you may want a more descriptive name for
98the variable argument than <code><span class="nolinebreak">__VA_ARGS__</span><!-- /@w --></code>. You can write an
99argument name immediately before the &lsquo;<samp>&hellip;</samp>&rsquo;; that name is used
100for 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&hellip;) fprintf (stderr, args)
105</pre></div>
106
107<p>A variadic macro can have named arguments as well as variable
108arguments, so <code>eprintf</code> can be defined like this, instead:
109</p>
110<div class="example">
111<pre class="example">#define eprintf(format, &hellip;) \
112 fprintf (stderr, format, __VA_ARGS__)
113</pre></div>
114
115<p>This formulation is more descriptive, but what if you want to specify
116a format string that takes no arguments? In GNU C, you can omit the
117comma before the variable arguments if they are empty, but that puts
118an extra comma in the expansion:
119</p>
120<div class="example">
121<pre class="example">eprintf (&quot;success!\n&quot;)
122 &rarr; fprintf(stderr, &quot;success!\n&quot;, );
123</pre></div>
124
125<p>That&rsquo;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
128special meaning when placed between a comma and a variable
129argument.<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, &hellip;) \
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 (&quot;success!\n&quot;)
141 &rarr; fprintf(stderr, &quot;success!\n&quot;);
142</pre></div>
143
144<p>This does <em>not</em> happen if you pass an empty argument, nor does it
145happen if the token preceding <code>##</code> is anything other than a
146comma.
147</p>
148<p>When the only macro parameter is a variable arguments parameter, and
149the macro call has no argument at all, it is not obvious whether that
150means an empty argument or a missing argument. Should the comma be
151kept, or deleted? The C standard says to keep the comma, but the
152preexisting GNU C extension deleted the comma. Nowadays, GNU C
153retains the comma when implementing a specific C standard, and deletes
154it otherwise.
155</p>
156<p>C99 mandates that the only place the identifier <code><span class="nolinebreak">__VA_ARGS__</span><!-- /@w --></code>
157can appear is in the replacement list of a variadic macro. It may not
158be used as a macro name, macro parameter name, or within a different
159type of macro. It may also be forbidden in open text; the standard is
160ambiguous. We recommend you avoid using that name except for its
161special purpose.
162</p>
163<p>Variadic macros where you specify the parameter name is a GNU C
164feature that has been supported for a long time. Standard C, as of
165C99, 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
167you should use only named variable argument parameters. On the other
168hand, 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>
183Next: <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> &nbsp; [<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>
Note: See TracBrowser for help on using the repository browser.