2 .\" Copyright (c) 1990, 1991 The Regents of the University of California.
3 .\" All rights reserved.
5 .\" This code is derived from software contributed to Berkeley by
6 .\" the American National Standards Committee X3, on Information
7 .\" Processing Systems.
9 .\" SPDX-License-Identifier: BSD-4-Clause-UC
11 .\" @(#)stdarg.3 6.8 (Berkeley) 6/29/91
13 .\" Converted for Linux, Mon Nov 29 15:11:11 1993, faith@cs.unc.edu
14 .\" Additions, 2001-10-14, aeb
16 .TH stdarg 3 (date) "Linux man-pages (unreleased)"
18 stdarg, va_start, va_arg, va_end, va_copy \- variable argument lists
21 .RI ( libc ", " \-lc )
24 .B #include <stdarg.h>
26 .BI "void va_start(va_list " ap ", " last );
27 .IB type " va_arg(va_list " ap ", " type );
28 .BI "void va_end(va_list " ap );
29 .BI "void va_copy(va_list " dest ", va_list " src );
32 A function may be called with a varying number of arguments of varying
38 and defines three macros for stepping through a list of arguments whose
39 number and types are not known to the called function.
41 The called function must declare an object of type
43 which is used by the macros
57 and must be called first.
61 is the name of the last argument before the variable argument list, that is,
62 the last argument of which the calling function knows the type.
64 Because the address of this argument may be used in the
66 macro, it should not be declared as a register variable,
67 or as a function or an array type.
71 macro expands to an expression that has the type and value of the next
84 so that the next call returns the next argument.
87 is a type name specified so that the type of a pointer to an object that
88 has the specified type can be obtained simply by adding a * to
93 macro after that of the
95 macro returns the argument after
97 Successive invocations return the values of the remaining arguments.
99 If there is no next argument, or if
101 is not compatible with the type of the actual next argument (as promoted
102 according to the default argument promotions), random errors will occur.
106 is passed to a function that uses
107 .BI va_arg( ap , type ),
110 is undefined after the return of that function.
114 must be matched by a corresponding invocation of
116 in the same function.
122 Multiple traversals of the list, each
129 may be a macro or a function.
133 macro copies the (previously initialized) variable argument list
137 The behavior is as if
143 argument, followed by the same number of
145 invocations that was used to reach the current state of
148 .\" Proposal from clive@demon.net, 1997-02-28
149 An obvious implementation would have a
151 be a pointer to the stack frame of the variadic function.
152 In such a setup (by far the most common) there seems
153 nothing against an assignment
161 Unfortunately, there are also systems that make it an
162 array of pointers (of length 1), and there one needs
171 Finally, on systems where arguments are passed in registers,
172 it may be necessary for
174 to allocate memory, store the arguments there, and also
175 an indication of which argument is next, so that
177 can step through the list.
180 can free the allocated memory again.
181 To accommodate this situation, C99 adds a macro
183 so that the above assignment can be replaced by
196 must be matched by a corresponding invocation of
198 in the same function.
199 Some systems that do not supply
203 instead, since that was the name used in the draft proposal.
205 For an explanation of the terms used in this section, see
213 Interface Attribute Value
218 T} Thread safety MT-Safe
221 T} Thread safety MT-Safe race:ap
240 Unlike the historical
244 macros do not permit programmers to code a function with no fixed
246 This problem generates work mainly when converting
250 code, but it also creates difficulties for variadic functions that wish to
251 pass all of their arguments on to a function that takes a
258 takes a string of format characters and prints out the argument associated
259 with each format character based on the type.
266 foo(char *fmt, ...) /* \[aq]...\[aq] is C syntax for a variadic function */
277 case \[aq]s\[aq]: /* string */
278 s = va_arg(ap, char *);
279 printf("string %s\en", s);
281 case \[aq]d\[aq]: /* int */
283 printf("int %d\en", d);
285 case \[aq]c\[aq]: /* char */
286 /* need a cast here since va_arg only
287 takes fully promoted types */
288 c = (char) va_arg(ap, int);
289 printf("char %c\en", c);