xref: /freebsd/share/man/man3/stdarg.3 (revision 5036d9652a5701d00e9e40ea942c278e9f77d33d)
1.\" Copyright (c) 1990, 1991, 1993
2.\"	The Regents of the University of California.  All rights reserved.
3.\"
4.\" This code is derived from software contributed to Berkeley by
5.\" the American National Standards Committee X3, on Information
6.\" Processing Systems.
7.\"
8.\" Redistribution and use in source and binary forms, with or without
9.\" modification, are permitted provided that the following conditions
10.\" are met:
11.\" 1. Redistributions of source code must retain the above copyright
12.\"    notice, this list of conditions and the following disclaimer.
13.\" 2. Redistributions in binary form must reproduce the above copyright
14.\"    notice, this list of conditions and the following disclaimer in the
15.\"    documentation and/or other materials provided with the distribution.
16.\" 3. Neither the name of the University nor the names of its contributors
17.\"    may be used to endorse or promote products derived from this software
18.\"    without specific prior written permission.
19.\"
20.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23.\" ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30.\" SUCH DAMAGE.
31.\"
32.Dd October 18, 2024
33.Dt STDARG 3
34.Os
35.Sh NAME
36.Nm stdarg
37.Nd variable argument lists
38.Sh SYNOPSIS
39.In stdarg.h
40.Ft void
41.Fn va_start "va_list ap" last
42.Ft type
43.Fn va_arg "va_list ap" type
44.Ft void
45.Fn va_copy "va_list dest" "va_list src"
46.Ft void
47.Fn va_end "va_list ap"
48.Sh DESCRIPTION
49A function may be called with a varying number of arguments of varying
50types.
51The include file
52.In stdarg.h
53declares a type
54.Pq Em va_list
55and defines four macros for stepping
56through a list of arguments whose number and types are not known to
57the called function.
58.Pp
59The called function must declare an object of type
60.Em va_list
61which is used by the macros
62.Fn va_start ,
63.Fn va_arg ,
64.Fn va_copy ,
65and
66.Fn va_end .
67.Pp
68The
69.Fn va_start
70macro initializes
71.Fa ap
72for subsequent use by
73.Fn va_arg ,
74.Fn va_copy ,
75and
76.Fn va_end ,
77and must be called first.
78.Pp
79The parameter
80.Fa last
81is the name of the last parameter before the variable argument list,
82i.e., the last parameter of which the calling function knows the type.
83.Pp
84Because the address of this parameter is used in the
85.Fn va_start
86macro, it should not be declared as a register variable, or as a
87function or an array type.
88.Pp
89The
90.Fn va_arg
91macro expands to an expression that has the type and value of the next
92argument in the call.
93The parameter
94.Fa ap
95is the
96.Em va_list Fa ap
97initialized by
98.Fn va_start
99or
100.Fn va_copy .
101Each call to
102.Fn va_arg
103modifies
104.Fa ap
105so that the next call returns the next argument.
106The parameter
107.Fa type
108is a type name specified so that the type of a pointer to an
109object that has the specified type can be obtained simply by
110adding a *
111to
112.Fa type .
113.Pp
114If there is no next argument, or if
115.Fa type
116is not compatible with the type of the actual next argument
117(as promoted according to the default argument promotions),
118random errors will occur.
119.Pp
120The first use of the
121.Fn va_arg
122macro after that of the
123.Fn va_start
124macro returns the argument after
125.Fa last .
126Successive invocations return the values of the remaining
127arguments.
128.Pp
129The
130.Fn va_copy
131macro copies a variable argument list, previously initialized by
132.Fn va_start ,
133from
134.Fa src
135to
136.Fa dest .
137The state is preserved such that it is equivalent to calling
138.Fn va_start
139with the same second argument used with
140.Fa src ,
141and calling
142.Fn va_arg
143the same number of times as called with
144.Fa src .
145.Pp
146The
147.Fn va_end
148macro cleans up any state associated with the variable argument list
149.Fa ap .
150.Pp
151Each invocation of
152.Fn va_start
153or
154.Fn va_copy
155must be paired with a corresponding invocation of
156.Fn va_end
157in the same function.
158.Sh RETURN VALUES
159The
160.Fn va_arg
161macro returns the value of the next argument.
162.Pp
163The
164.Fn va_start ,
165.Fn va_copy ,
166and
167.Fn va_end
168macros return no value.
169.Sh EXAMPLES
170The function
171.Em foo
172takes a string of format characters and prints out the argument
173associated with each format character based on the type.
174.Bd -literal -offset indent
175void foo(char *fmt, ...)
176{
177	va_list ap;
178	int d;
179	char c, *s;
180
181	va_start(ap, fmt);
182	while (*fmt)
183		switch(*fmt++) {
184		case 's':			/* string */
185			s = va_arg(ap, char *);
186			printf("string %s\en", s);
187			break;
188		case 'd':			/* int */
189			d = va_arg(ap, int);
190			printf("int %d\en", d);
191			break;
192		case 'c':			/* char */
193			/* Note: char is promoted to int. */
194			c = va_arg(ap, int);
195			printf("char %c\en", c);
196			break;
197		}
198	va_end(ap);
199}
200.Ed
201.Sh COMPATIBILITY
202These macros are
203.Em not
204compatible with the historic macros they replace.
205.Sh STANDARDS
206The
207.Fn va_start ,
208.Fn va_arg ,
209.Fn va_copy ,
210and
211.Fn va_end
212macros conform to
213.St -isoC-99 .
214.Sh HISTORY
215The
216.Fn va_start ,
217.Fn va_arg
218and
219.Fn va_end
220macros were introduced in
221.St -ansiC .
222The
223.Fn va_copy
224macro was introduced in
225.St -isoC-99 .
226.Sh BUGS
227Unlike the
228.Em varargs
229macros, the
230.Nm
231macros do not permit programmers to
232code a function with no fixed arguments.
233This problem generates work mainly when converting
234.Em varargs
235code to
236.Nm
237code,
238but it also creates difficulties for variadic functions that
239wish to pass all of their arguments on to a function
240that takes a
241.Em va_list
242argument, such as
243.Xr vfprintf 3 .
244