Files
2024-02-19 00:21:47 -05:00

144 lines
3.7 KiB
HTML

<html>
<head>
<title>
C Guide--2.10 stdarg.h
</title>
<!-- Changed by: eric huss, 12-Mar-1997 -->
</head>
<body text="#000000" bgcolor="#FFFFFF">
<center>
<table border=0 width=100%>
<tr>
<td align=left width=20% valign=top>
<a href="2.9.html">
<img src="left.gif" border=0>
Previous Section<br>
2.9 signal.h</a></td>
<td align=center width=60% valign=top>
| <a href="index.html">Table of Contents</a> |
<a href="index2.html">Index</a> |</td>
<td align=right width=20% valign=top>
<a href="2.11.html">
Next Section
<img src="right.gif" border=0><br>
2.11 stddef.h</a></td>
</tr>
</table>
</center>
<hr>
<h1> 2.10 stdarg.h</h1>
<p>
The stdarg header defines several macros used to get the arguments in a function when
the number of arguments is not known.
<p>
Macros:
<blockquote><code><b>
va_start();<br>
va_arg();<br>
va_end();<br>
</b></code></blockquote>
<p>
Variables:
<blockquote><code><b>
typedef va_list
</b></code></blockquote>
<a name="variables"></a>
<h2>2.10.1 Variables and Definitions</h2>
<p>
The <code><b>va_list</b></code> type is a type suitable for use in accessing the arguments of a function with
the stdarg macros.
<p>
A function of variable arguments is defined with the ellipsis <code><b>(,...)</b></code> at the end of the
parameter list.
<p>
<a name="va_start"></a>
<h2>2.10.2 va_start</h2>
<p>
Declaration:
<blockquote>
<code><b>void va_start(va_list</b></code><i> ap</i><code><b>,</b></code><i> last_arg</i><code><b>);</b></code>
</blockquote>
Initializes <i>ap</i> for use with the <code><b>va_arg</b></code> and <code><b>va_end</b></code> macros. <i>last_arg</i> is the last known fixed
argument being passed to the function (the argument before the ellipsis).
<p>
Note that <code><b>va_start</b></code> must be called before using <code><b>va_arg</b></code> and <code><b>va_end</b></code>.
<p>
<a name="va_arg"></a>
<h2>2.10.3 va_arg</h2>
<p>
Declaration:
<blockquote>
<i>type</i><code><b> va_arg(va_list</b></code><i> ap</i><code><b>,</b></code><i> type</i><code><b>);</b></code>
</blockquote>
Expands to the next argument in the paramater list of the function with type <i>type</i>. Note
that <i>ap</i> must be initialized with <code><b>va_start</b></code>. If there is no next argument, then the result is
undefined.
<p>
<a name="va_end"></a>
<h2>2.10.4 va_end</h2>
<p>
Declaration:
<blockquote>
<code><b>void va_end(va_list</b></code><i> ap</i><code><b>);</b></code>
</blockquote>
<p>
Allows a function with variable arguments which used the <code><b>va_start</b></code> macro to return. If
<code><b>va_end</b></code> is not called before returning from the function, the result is undefined. The variable
argument list<i> ap</i> may no longer be used after a call to <code><b>va_end</b></code> without a call to <code><b>va_start</b></code>.
<p>
Example:
<blockquote><code><b><pre>
#include&lt;stdarg.h&gt;
#include&lt;stdio.h&gt;
void sum(char *, int, ...);
int main(void)
{
sum("The sum of 10+15+13 is %d.\n",3,10,15,13);
return 0;
}
void sum(char *string, int num_args, ...)
{
int sum=0;
va_list ap;
int loop;
va_start(ap,num_args);
for(loop=0;loop&lt;num_args;loop++)
sum+=va_arg(ap,int);
printf(string,sum);
va_end(ap);
}
</pre></b></code></blockquote>
<hr>
<center>
<table border=0 width=100%>
<tr>
<td align=left width=20% valign=top>
<a href="2.9.html">
<img src="left.gif" border=0>
Previous Section<br>
2.9 signal.h</a></td>
<td align=center width=60% valign=top>
| <a href="index.html">Table of Contents</a> |
<a href="index2.html">Index</a> |</td>
<td align=right width=20% valign=top>
<a href="2.11.html">
Next Section
<img src="right.gif" border=0><br>
2.11 stddef.h</a></td>
</tr>
</table>
</center>
</body></html>