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>Variable Number of Arguments (GNU C Language Manual)</title>
|
---|
23 |
|
---|
24 | <meta name="description" content="Variable Number of Arguments (GNU C Language Manual)">
|
---|
25 | <meta name="keywords" content="Variable Number of Arguments (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="Advanced-Definitions.html" rel="up" title="Advanced Definitions">
|
---|
33 | <link href="Nested-Functions.html" rel="next" title="Nested Functions">
|
---|
34 | <link href="Variable_002dLength-Array-Parameters.html" rel="prev" title="Variable-Length Array Parameters">
|
---|
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="Variable-Number-of-Arguments"></span><div class="header">
|
---|
59 | <p>
|
---|
60 | Next: <a href="Nested-Functions.html" accesskey="n" rel="next">Nested Functions</a>, Previous: <a href="Variable_002dLength-Array-Parameters.html" accesskey="p" rel="prev">Variable-Length Array Parameters</a>, Up: <a href="Advanced-Definitions.html" accesskey="u" rel="up">Advanced Definitions</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="Variable_002dLength-Parameter-Lists"></span><h4 class="subsection">22.7.2 Variable-Length Parameter Lists</h4>
|
---|
64 | <span id="index-variable_002dlength-parameter-lists"></span>
|
---|
65 | <span id="index-parameters-lists_002c-variable-length"></span>
|
---|
66 | <span id="index-function-parameter-lists_002c-variable-length"></span>
|
---|
67 |
|
---|
68 | <span id="index-variadic-function"></span>
|
---|
69 | <p>A function that takes a variable number of arguments is called a
|
---|
70 | <em>variadic function</em>. In C, a variadic function must specify at
|
---|
71 | least one fixed argument with an explicitly declared data type.
|
---|
72 | Additional arguments can follow, and can vary in both quantity and
|
---|
73 | data type.
|
---|
74 | </p>
|
---|
75 | <p>In the function header, declare the fixed parameters in the normal
|
---|
76 | way, then write a comma and an ellipsis: ‘<samp>, ...</samp>’. Here is an
|
---|
77 | example of a variadic function header:
|
---|
78 | </p>
|
---|
79 | <div class="example">
|
---|
80 | <pre class="example">int add_multiple_values (int number, ...)
|
---|
81 | </pre></div>
|
---|
82 |
|
---|
83 | <span id="index-va_005flist"></span>
|
---|
84 | <span id="index-va_005fstart"></span>
|
---|
85 | <span id="index-va_005fend"></span>
|
---|
86 | <p>The function body can refer to fixed arguments by their parameter
|
---|
87 | names, but the additional arguments have no names. Accessing them in
|
---|
88 | the function body uses certain standard macros. They are defined in
|
---|
89 | the library header file <samp>stdarg.h</samp>, so the code must
|
---|
90 | <code>#include</code> that file.
|
---|
91 | </p>
|
---|
92 | <p>In the body, write
|
---|
93 | </p>
|
---|
94 | <div class="example">
|
---|
95 | <pre class="example">va_list ap;
|
---|
96 | va_start (ap, <var>last_fixed_parameter</var>);
|
---|
97 | </pre></div>
|
---|
98 |
|
---|
99 | <p>This declares the variable <code>ap</code> (you can use any name for it)
|
---|
100 | and then sets it up to point before the first additional argument.
|
---|
101 | </p>
|
---|
102 | <p>Then, to fetch the next consecutive additional argument, write this:
|
---|
103 | </p>
|
---|
104 | <div class="example">
|
---|
105 | <pre class="example">va_arg (ap, <var>type</var>)
|
---|
106 | </pre></div>
|
---|
107 |
|
---|
108 | <p>After fetching all the additional arguments (or as many as need to be
|
---|
109 | used), write this:
|
---|
110 | </p>
|
---|
111 | <div class="example">
|
---|
112 | <pre class="example">va_end (ap);
|
---|
113 | </pre></div>
|
---|
114 |
|
---|
115 | <p>Here’s an example of a variadic function definition that adds any
|
---|
116 | number of <code>int</code> arguments. The first (fixed) argument says how
|
---|
117 | many more arguments follow.
|
---|
118 | </p>
|
---|
119 | <div class="example">
|
---|
120 | <pre class="example">#include <stdarg.h> /* <span class="roman">Defines <code>va</code><span class="roman">…</span> macros.</span> */
|
---|
121 | <span class="roman">…</span>
|
---|
122 |
|
---|
123 | int
|
---|
124 | add_multiple_values (int argcount, ...)
|
---|
125 | {
|
---|
126 | int counter, total = 0;
|
---|
127 |
|
---|
128 | /* <span class="roman">Declare a variable of type <code>va_list</code>.</span> */
|
---|
129 | va_list argptr;
|
---|
130 |
|
---|
131 | /* <span class="roman">Initialize that variable..</span> */
|
---|
132 | va_start (argptr, argcount);
|
---|
133 |
|
---|
134 | for (counter = 0; counter < argcount; counter++)
|
---|
135 | {
|
---|
136 | /* <span class="roman">Get the next additional argument.</span> */
|
---|
137 | total += va_arg (argptr, int);
|
---|
138 | }
|
---|
139 |
|
---|
140 | /* <span class="roman">End use of the <code>argptr</code> variable.</span> */
|
---|
141 | va_end (argptr);
|
---|
142 |
|
---|
143 | return total;
|
---|
144 | }
|
---|
145 | </pre></div>
|
---|
146 |
|
---|
147 | <p>With GNU C, <code>va_end</code> is superfluous, but some other compilers
|
---|
148 | might make <code>va_start</code> allocate memory so that calling
|
---|
149 | <code>va_end</code> is necessary to avoid a memory leak. Before doing
|
---|
150 | <code>va_start</code> again with the same variable, do <code>va_end</code>
|
---|
151 | first.
|
---|
152 | </p>
|
---|
153 | <span id="index-va_005fcopy"></span>
|
---|
154 | <p>Because of this possible memory allocation, it is risky (in principle)
|
---|
155 | to copy one <code>va_list</code> variable to another with assignment.
|
---|
156 | Instead, use <code>va_copy</code>, which copies the substance but allocates
|
---|
157 | separate memory in the variable you copy to. The call looks like
|
---|
158 | <code>va_copy (<var>to</var>, <var>from</var>)</code>, where both <var>to</var> and
|
---|
159 | <var>from</var> should be variables of type <code>va_list</code>. In principle,
|
---|
160 | do <code>va_end</code> on each of these variables before its scope ends.
|
---|
161 | </p>
|
---|
162 | <p>Since the additional arguments’ types are not specified in the
|
---|
163 | function’s definition, the default argument promotions
|
---|
164 | (see <a href="Argument-Promotions.html">Argument Promotions</a>) apply to them in function calls. The
|
---|
165 | function definition must take account of this; thus, if an argument
|
---|
166 | was passed as <code>short</code>, the function should get it as <code>int</code>.
|
---|
167 | If an argument was passed as <code>float</code>, the function should get it
|
---|
168 | as <code>double</code>.
|
---|
169 | </p>
|
---|
170 | <p>C has no mechanism to tell the variadic function how many arguments
|
---|
171 | were passed to it, so its calling convention must give it a way to
|
---|
172 | determine this. That’s why <code>add_multiple_values</code> takes a fixed
|
---|
173 | argument that says how many more arguments follow. Thus, you can
|
---|
174 | call the function like this:
|
---|
175 | </p>
|
---|
176 | <div class="example">
|
---|
177 | <pre class="example">sum = add_multiple_values (3, 12, 34, 190);
|
---|
178 | /* <span class="roman">Value is 12+34+190.</span> */
|
---|
179 | </pre></div>
|
---|
180 |
|
---|
181 | <p>In GNU C, there is no actual need to use the <code>va_end</code> function.
|
---|
182 | In fact, it does nothing. It’s used for compatibility with other
|
---|
183 | compilers, when that matters.
|
---|
184 | </p>
|
---|
185 | <p>It is a mistake to access variables declared as <code>va_list</code> except
|
---|
186 | in the specific ways described here. Just what that type consists of
|
---|
187 | is an implementation detail, which could vary from one platform to
|
---|
188 | another.
|
---|
189 | </p>
|
---|
190 | <hr>
|
---|
191 | <div class="header">
|
---|
192 | <p>
|
---|
193 | Next: <a href="Nested-Functions.html" accesskey="n" rel="next">Nested Functions</a>, Previous: <a href="Variable_002dLength-Array-Parameters.html" accesskey="p" rel="prev">Variable-Length Array Parameters</a>, Up: <a href="Advanced-Definitions.html" accesskey="u" rel="up">Advanced Definitions</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>
|
---|
194 | </div>
|
---|
195 |
|
---|
196 |
|
---|
197 |
|
---|
198 | </body>
|
---|
199 | </html>
|
---|