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