1 .\" Copyright (c) 1990, 1991 The Regents of the University of California.
2 .\" All rights reserved.
4 .\" This code is derived from software contributed to Berkeley by
5 .\" the American National Standards Committee X3, on Information
6 .\" Processing Systems.
8 .\" %%%LICENSE_START(BSD_4_CLAUSE_UCB)
9 .\" Redistribution and use in source and binary forms, with or without
10 .\" modification, are permitted provided that the following conditions
12 .\" 1. Redistributions of source code must retain the above copyright
13 .\" notice, this list of conditions and the following disclaimer.
14 .\" 2. Redistributions in binary form must reproduce the above copyright
15 .\" notice, this list of conditions and the following disclaimer in the
16 .\" documentation and/or other materials provided with the distribution.
17 .\" 3. All advertising materials mentioning features or use of this software
18 .\" must display the following acknowledgement:
19 .\" This product includes software developed by the University of
20 .\" California, Berkeley and its contributors.
21 .\" 4. Neither the name of the University nor the names of its contributors
22 .\" may be used to endorse or promote products derived from this software
23 .\" without specific prior written permission.
25 .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
26 .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28 .\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29 .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30 .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31 .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32 .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33 .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34 .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
38 .\" @(#)stdarg.3 6.8 (Berkeley) 6/29/91
40 .\" Converted for Linux, Mon Nov 29 15:11:11 1993, faith@cs.unc.edu
41 .\" Additions, 2001-10-14, aeb
43 .TH STDARG 3 2001-10-14 "" "Linux Programmer's Manual"
45 stdarg, va_start, va_arg, va_end, va_copy \- variable argument lists
47 .B #include <stdarg.h>
49 .BI "void va_start(va_list " ap ", " last );
51 .IB type " va_arg(va_list " ap ", " type );
53 .BI "void va_end(va_list " ap );
55 .BI "void va_copy(va_list " dest ", va_list " src );
57 A function may be called with a varying number of arguments of varying
63 and defines three macros for stepping through a list of arguments whose
64 number and types are not known to the called function.
66 The called function must declare an object of type
68 which is used by the macros
82 and must be called first.
86 is the name of the last argument before the variable argument list, that is,
87 the last argument of which the calling function knows the type.
89 Because the address of this argument may be used in the
91 macro, it should not be declared as a register variable,
92 or as a function or an array type.
96 macro expands to an expression that has the type and value of the next
109 so that the next call returns the next argument.
112 is a type name specified so that the type of a pointer to an object that
113 has the specified type can be obtained simply by adding a * to
118 macro after that of the
120 macro returns the argument after
122 Successive invocations return the values of the remaining arguments.
124 If there is no next argument, or if
126 is not compatible with the type of the actual next argument (as promoted
127 according to the default argument promotions), random errors will occur.
131 is passed to a function that uses
132 .BI va_arg( ap , type )
135 is undefined after the return of that function.
139 must be matched by a corresponding invocation of
141 in the same function.
147 Multiple traversals of the list, each
154 may be a macro or a function.
156 .\" Proposal from clive@demon.net, 1997-02-28
157 An obvious implementation would have a
159 be a pointer to the stack frame of the variadic function.
160 In such a setup (by far the most common) there seems
161 nothing against an assignment
169 Unfortunately, there are also systems that make it an
170 array of pointers (of length 1), and there one needs
179 Finally, on systems where arguments are passed in registers,
180 it may be necessary for
182 to allocate memory, store the arguments there, and also
183 an indication of which argument is next, so that
185 can step through the list.
188 can free the allocated memory again.
189 To accommodate this situation, C99 adds a macro
191 so that the above assignment can be replaced by
204 must be matched by a corresponding invocation of
206 in the same function.
207 Some systems that do not supply
211 instead, since that was the name used in the draft proposal.
218 macros conform to C89.
225 compatible with the historic macros they replace.
226 A backward-compatible version can be found in the include file
229 The historic setup is:
244 x = va_arg(ap, type);
254 contains a closing \(aq}\(aq matching a \(aq{\(aq in
256 so that both macros must occur in the same function, and in a way
263 macros do not permit programmers to code a function with no fixed
265 This problem generates work mainly when converting
269 code, but it also creates difficulties for variadic functions that wish to
270 pass all of their arguments on to a function that takes a
277 takes a string of format characters and prints out the argument associated
278 with each format character based on the type.
294 case \(aqs\(aq: /* string */
295 s = va_arg(ap, char *);
296 printf("string %s\en", s);
298 case \(aqd\(aq: /* int */
300 printf("int %d\en", d);
302 case \(aqc\(aq: /* char */
303 /* need a cast here since va_arg only
304 takes fully promoted types */
305 c = (char) va_arg(ap, int);
306 printf("char %c\en", c);