source: public/doc/gnu-c/Variable-Number-of-Arguments.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.6 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>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<!--
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="Variable-Number-of-Arguments"></span><div class="header">
59<p>
60Next: <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> &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="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
71least one fixed argument with an explicitly declared data type.
72Additional arguments can follow, and can vary in both quantity and
73data type.
74</p>
75<p>In the function header, declare the fixed parameters in the normal
76way, then write a comma and an ellipsis: &lsquo;<samp>, ...</samp>&rsquo;. Here is an
77example 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
87names, but the additional arguments have no names. Accessing them in
88the function body uses certain standard macros. They are defined in
89the 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;
96va_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)
100and 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
109used), write this:
110</p>
111<div class="example">
112<pre class="example">va_end (ap);
113</pre></div>
114
115<p>Here&rsquo;s an example of a variadic function definition that adds any
116number of <code>int</code> arguments. The first (fixed) argument says how
117many more arguments follow.
118</p>
119<div class="example">
120<pre class="example">#include &lt;stdarg.h&gt; /* <span class="roman">Defines <code>va</code><span class="roman">&hellip;</span> macros.</span> */
121<span class="roman">&hellip;</span>
122
123int
124add_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 &lt; 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
148might 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>
151first.
152</p>
153<span id="index-va_005fcopy"></span>
154<p>Because of this possible memory allocation, it is risky (in principle)
155to copy one <code>va_list</code> variable to another with assignment.
156Instead, use <code>va_copy</code>, which copies the substance but allocates
157separate 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,
160do <code>va_end</code> on each of these variables before its scope ends.
161</p>
162<p>Since the additional arguments&rsquo; types are not specified in the
163function&rsquo;s definition, the default argument promotions
164(see <a href="Argument-Promotions.html">Argument Promotions</a>) apply to them in function calls. The
165function definition must take account of this; thus, if an argument
166was passed as <code>short</code>, the function should get it as <code>int</code>.
167If an argument was passed as <code>float</code>, the function should get it
168as <code>double</code>.
169</p>
170<p>C has no mechanism to tell the variadic function how many arguments
171were passed to it, so its calling convention must give it a way to
172determine this. That&rsquo;s why <code>add_multiple_values</code> takes a fixed
173argument that says how many more arguments follow. Thus, you can
174call 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.
182In fact, it does nothing. It&rsquo;s used for compatibility with other
183compilers, when that matters.
184</p>
185<p>It is a mistake to access variables declared as <code>va_list</code> except
186in the specific ways described here. Just what that type consists of
187is an implementation detail, which could vary from one platform to
188another.
189</p>
190<hr>
191<div class="header">
192<p>
193Next: <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> &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>
194</div>
195
196
197
198</body>
199</html>
Note: See TracBrowser for help on using the repository browser.