1STDARG(3) Linux Programmer's Manual STDARG(3)
2
3
4
6 stdarg, va_start, va_arg, va_end, va_copy - variable argument lists
7
9 #include <stdarg.h>
10
11 void va_start(va_list ap, last);
12 type va_arg(va_list ap, type);
13 void va_end(va_list ap);
14 void va_copy(va_list dest, va_list src);
15
17 A function may be called with a varying number of arguments of varying
18 types. The include file <stdarg.h> declares a type va_list and defines
19 three macros for stepping through a list of arguments whose number and
20 types are not known to the called function.
21
22 The called function must declare an object of type va_list which is
23 used by the macros va_start(), va_arg(), and va_end().
24
25 va_start()
26 The va_start() macro initializes ap for subsequent use by va_arg() and
27 va_end(), and must be called first.
28
29 The argument last is the name of the last argument before the variable
30 argument list, that is, the last argument of which the calling function
31 knows the type.
32
33 Because the address of this argument may be used in the va_start()
34 macro, it should not be declared as a register variable, or as a func‐
35 tion or an array type.
36
37 va_arg()
38 The va_arg() macro expands to an expression that has the type and value
39 of the next argument in the call. The argument ap is the va_list ap
40 initialized by va_start(). Each call to va_arg() modifies ap so that
41 the next call returns the next argument. The argument type is a type
42 name specified so that the type of a pointer to an object that has the
43 specified type can be obtained simply by adding a * to type.
44
45 The first use of the va_arg() macro after that of the va_start() macro
46 returns the argument after last. Successive invocations return the
47 values of the remaining arguments.
48
49 If there is no next argument, or if type is not compatible with the
50 type of the actual next argument (as promoted according to the default
51 argument promotions), random errors will occur.
52
53 If ap is passed to a function that uses va_arg(ap,type), then the value
54 of ap is undefined after the return of that function.
55
56 va_end()
57 Each invocation of va_start() must be matched by a corresponding invo‐
58 cation of va_end() in the same function. After the call va_end(ap) the
59 variable ap is undefined. Multiple traversals of the list, each brack‐
60 eted by va_start() and va_end() are possible. va_end() may be a macro
61 or a function.
62
63 va_copy()
64 The va_copy() macro copies the (previously initialized) variable argu‐
65 ment list src to dest. The behavior is as if va_start() were applied
66 to dest with the same last argument, followed by the same number of
67 va_arg() invocations that was used to reach the current state of src.
68
69 An obvious implementation would have a va_list be a pointer to the
70 stack frame of the variadic function. In such a setup (by far the most
71 common) there seems nothing against an assignment
72
73 va_list aq = ap;
74
75 Unfortunately, there are also systems that make it an array of pointers
76 (of length 1), and there one needs
77
78 va_list aq;
79 *aq = *ap;
80
81 Finally, on systems where arguments are passed in registers, it may be
82 necessary for va_start() to allocate memory, store the arguments there,
83 and also an indication of which argument is next, so that va_arg() can
84 step through the list. Now va_end() can free the allocated memory
85 again. To accommodate this situation, C99 adds a macro va_copy(), so
86 that the above assignment can be replaced by
87
88 va_list aq;
89 va_copy(aq, ap);
90 ...
91 va_end(aq);
92
93 Each invocation of va_copy() must be matched by a corresponding invoca‐
94 tion of va_end() in the same function. Some systems that do not supply
95 va_copy() have __va_copy instead, since that was the name used in the
96 draft proposal.
97
99 For an explanation of the terms used in this section, see
100 attributes(7).
101
102 ┌──────────────────────┬───────────────┬─────────────────┐
103 │Interface │ Attribute │ Value │
104 ├──────────────────────┼───────────────┼─────────────────┤
105 │va_start(), va_end(), │ Thread safety │ MT-Safe │
106 │va_copy() │ │ │
107 ├──────────────────────┼───────────────┼─────────────────┤
108 │va_arg() │ Thread safety │ MT-Safe race:ap │
109 └──────────────────────┴───────────────┴─────────────────┘
111 The va_start(), va_arg(), and va_end() macros conform to C89. C99
112 defines the va_copy() macro.
113
115 Unlike the historical varargs macros, the stdarg macros do not permit
116 programmers to code a function with no fixed arguments. This problem
117 generates work mainly when converting varargs code to stdarg code, but
118 it also creates difficulties for variadic functions that wish to pass
119 all of their arguments on to a function that takes a va_list argument,
120 such as vfprintf(3).
121
123 The function foo takes a string of format characters and prints out the
124 argument associated with each format character based on the type.
125
126 #include <stdio.h>
127 #include <stdarg.h>
128
129 void
130 foo(char *fmt, ...) /* '...' is C syntax for a variadic function */
131
132 {
133 va_list ap;
134 int d;
135 char c, *s;
136
137 va_start(ap, fmt);
138 while (*fmt)
139 switch (*fmt++) {
140 case 's': /* string */
141 s = va_arg(ap, char *);
142 printf("string %s\n", s);
143 break;
144 case 'd': /* int */
145 d = va_arg(ap, int);
146 printf("int %d\n", d);
147 break;
148 case 'c': /* char */
149 /* need a cast here since va_arg only
150 takes fully promoted types */
151 c = (char) va_arg(ap, int);
152 printf("char %c\n", c);
153 break;
154 }
155 va_end(ap);
156 }
157
159 This page is part of release 5.02 of the Linux man-pages project. A
160 description of the project, information about reporting bugs, and the
161 latest version of this page, can be found at
162 https://www.kernel.org/doc/man-pages/.
163
164
165
166 2019-05-09 STDARG(3)