1stdarg.h(0P) POSIX Programmer's Manual stdarg.h(0P)
2
3
4
6 This manual page is part of the POSIX Programmer's Manual. The Linux
7 implementation of this interface may differ (consult the corresponding
8 Linux manual page for details of Linux behavior), or the interface may
9 not be implemented on Linux.
10
12 stdarg.h — handle variable argument list
13
15 #include <stdarg.h>
16
17 void va_start(va_list ap, argN);
18 void va_copy(va_list dest, va_list src);
19 type va_arg(va_list ap, type);
20 void va_end(va_list ap);
21
23 The functionality described on this reference page is aligned with the
24 ISO C standard. Any conflict between the requirements described here
25 and the ISO C standard is unintentional. This volume of POSIX.1‐2017
26 defers to the ISO C standard.
27
28 The <stdarg.h> header shall contain a set of macros which allows porta‐
29 ble functions that accept variable argument lists to be written. Func‐
30 tions that have variable argument lists (such as printf()) but do not
31 use these macros are inherently non-portable, as different systems use
32 different argument-passing conventions.
33
34 The <stdarg.h> header shall define the va_list type for variables used
35 to traverse the list.
36
37 The va_start() macro is invoked to initialize ap to the beginning of
38 the list before any calls to va_arg().
39
40 The va_copy() macro initializes dest as a copy of src, as if the
41 va_start() macro had been applied to dest followed by the same sequence
42 of uses of the va_arg() macro as had previously been used to reach the
43 present state of src. Neither the va_copy() nor va_start() macro shall
44 be invoked to reinitialize dest without an intervening invocation of
45 the va_end() macro for the same dest.
46
47 The object ap may be passed as an argument to another function; if that
48 function invokes the va_arg() macro with parameter ap, the value of ap
49 in the calling function is unspecified and shall be passed to the
50 va_end() macro prior to any further reference to ap. The parameter
51 argN is the identifier of the rightmost parameter in the variable
52 parameter list in the function definition (the one just before the
53 ...). If the parameter argN is declared with the register storage
54 class, with a function type or array type, or with a type that is not
55 compatible with the type that results after application of the default
56 argument promotions, the behavior is undefined.
57
58 The va_arg() macro shall return the next argument in the list pointed
59 to by ap. Each invocation of va_arg() modifies ap so that the values
60 of successive arguments are returned in turn. The type parameter shall
61 be a type name specified such that the type of a pointer to an object
62 that has the specified type can be obtained simply by postfixing a '*'
63 to type. If there is no actual next argument, or if type is not compat‐
64 ible with the type of the actual next argument (as promoted according
65 to the default argument promotions), the behavior is undefined, except
66 for the following cases:
67
68 * One type is a signed integer type, the other type is the corre‐
69 sponding unsigned integer type, and the value is representable in
70 both types.
71
72 * One type is a pointer to void and the other is a pointer to a char‐
73 acter type.
74
75 * Both types are pointers.
76
77 Different types can be mixed, but it is up to the routine to know what
78 type of argument is expected.
79
80 The va_end() macro is used to clean up; it invalidates ap for use
81 (unless va_start() or va_copy() is invoked again).
82
83 Each invocation of the va_start() and va_copy() macros shall be matched
84 by a corresponding invocation of the va_end() macro in the same func‐
85 tion.
86
87 Multiple traversals, each bracketed by va_start() ... va_end(), are
88 possible.
89
90 The following sections are informative.
91
93 This example is a possible implementation of execl():
94
95
96 #include <stdarg.h>
97
98 #define MAXARGS 31
99
100 /*
101 * execl is called by
102 * execl(file, arg1, arg2, ..., (char *)(0));
103 */
104 int execl(const char *file, const char *args, ...)
105 {
106 va_list ap;
107 char *array[MAXARGS +1];
108 int argno = 0;
109
110 va_start(ap, args);
111 while (args != 0 && argno < MAXARGS)
112 {
113 array[argno++] = args;
114 args = va_arg(ap, const char *);
115 }
116 array[argno] = (char *) 0;
117 va_end(ap);
118 return execv(file, array);
119 }
120
122 It is up to the calling routine to communicate to the called routine
123 how many arguments there are, since it is not always possible for the
124 called routine to determine this in any other way. For example, execl()
125 is passed a null pointer to signal the end of the list. The printf()
126 function can tell how many arguments are there by the format argument.
127
129 None.
130
132 None.
133
135 The System Interfaces volume of POSIX.1‐2017, exec, fprintf()
136
138 Portions of this text are reprinted and reproduced in electronic form
139 from IEEE Std 1003.1-2017, Standard for Information Technology -- Por‐
140 table Operating System Interface (POSIX), The Open Group Base Specifi‐
141 cations Issue 7, 2018 Edition, Copyright (C) 2018 by the Institute of
142 Electrical and Electronics Engineers, Inc and The Open Group. In the
143 event of any discrepancy between this version and the original IEEE and
144 The Open Group Standard, the original IEEE and The Open Group Standard
145 is the referee document. The original Standard can be obtained online
146 at http://www.opengroup.org/unix/online.html .
147
148 Any typographical or formatting errors that appear in this page are
149 most likely to have been introduced during the conversion of the source
150 files to man page format. To report such errors, see https://www.ker‐
151 nel.org/doc/man-pages/reporting_bugs.html .
152
153
154
155IEEE/The Open Group 2017 stdarg.h(0P)