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 .\" SPDX-License-Identifier: BSD-4-Clause-UC
10 .\" @(#)stdarg.3 6.8 (Berkeley) 6/29/91
12 .\" Converted for Linux, Mon Nov 29 15:11:11 1993, faith@cs.unc.edu
13 .\" Additions, 2001-10-14, aeb
15 .TH stdarg 3 (date) "Linux man-pages (unreleased)"
17 stdarg, va_start, va_arg, va_end, va_copy \- variable argument lists
20 .RI ( libc ", " \-lc )
23 .B #include <stdarg.h>
25 .BI "void va_start(va_list " ap ", " last );
26 .IB type " va_arg(va_list " ap ", " type );
27 .BI "void va_end(va_list " ap );
28 .BI "void va_copy(va_list " dest ", va_list " src );
31 A function may be called with a varying number of arguments of varying
37 and defines three macros for stepping through a list of arguments whose
38 number and types are not known to the called function.
40 The called function must declare an object of type
42 which is used by the macros
56 and must be called first.
60 is the name of the last argument before the variable argument list, that is,
61 the last argument of which the calling function knows the type.
63 Because the address of this argument may be used in the
65 macro, it should not be declared as a register variable,
66 or as a function or an array type.
70 macro expands to an expression that has the type and value of the next
83 so that the next call returns the next argument.
86 is a type name specified so that the type of a pointer to an object that
87 has the specified type can be obtained simply by adding a * to
92 macro after that of the
94 macro returns the argument after
96 Successive invocations return the values of the remaining arguments.
98 If there is no next argument, or if
100 is not compatible with the type of the actual next argument (as promoted
101 according to the default argument promotions), random errors will occur.
105 is passed to a function that uses
106 .BI va_arg( ap , type ),
109 is undefined after the return of that function.
113 must be matched by a corresponding invocation of
115 in the same function.
121 Multiple traversals of the list, each
128 may be a macro or a function.
132 macro copies the (previously initialized) variable argument list
136 The behavior is as if
142 argument, followed by the same number of
144 invocations that was used to reach the current state of
147 .\" Proposal from clive@demon.net, 1997-02-28
148 An obvious implementation would have a
150 be a pointer to the stack frame of the variadic function.
151 In such a setup (by far the most common) there seems
152 nothing against an assignment
160 Unfortunately, there are also systems that make it an
161 array of pointers (of length 1), and there one needs
170 Finally, on systems where arguments are passed in registers,
171 it may be necessary for
173 to allocate memory, store the arguments there, and also
174 an indication of which argument is next, so that
176 can step through the list.
179 can free the allocated memory again.
180 To accommodate this situation, C99 adds a macro
182 so that the above assignment can be replaced by
195 must be matched by a corresponding invocation of
197 in the same function.
198 Some systems that do not supply
202 instead, since that was the name used in the draft proposal.
204 For an explanation of the terms used in this section, see
212 Interface Attribute Value
217 T} Thread safety MT-Safe
220 T} Thread safety MT-Safe race:ap
231 macros conform to C89.
236 Unlike the historical
240 macros do not permit programmers to code a function with no fixed
242 This problem generates work mainly when converting
246 code, but it also creates difficulties for variadic functions that wish to
247 pass all of their arguments on to a function that takes a
254 takes a string of format characters and prints out the argument associated
255 with each format character based on the type.
262 foo(char *fmt, ...) /* \(aq...\(aq is C syntax for a variadic function */
273 case \(aqs\(aq: /* string */
274 s = va_arg(ap, char *);
275 printf("string %s\en", s);
277 case \(aqd\(aq: /* int */
279 printf("int %d\en", d);
281 case \(aqc\(aq: /* char */
282 /* need a cast here since va_arg only
283 takes fully promoted types */
284 c = (char) va_arg(ap, int);
285 printf("char %c\en", c);