xref: /freebsd/share/man/man3/stdarg.3 (revision e0c4386e7e71d93b0edc0c8fa156263fc4a8b0b6)
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 February 25, 2020
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.
205A backward compatible version can be found in the include
206file
207.In varargs.h .
208.Sh STANDARDS
209The
210.Fn va_start ,
211.Fn va_arg ,
212.Fn va_copy ,
213and
214.Fn va_end
215macros conform to
216.St -isoC-99 .
217.Sh HISTORY
218The
219.Fn va_start ,
220.Fn va_arg
221and
222.Fn va_end
223macros were introduced in
224.St -ansiC .
225The
226.Fn va_copy
227macro was introduced in
228.St -isoC-99 .
229.Sh BUGS
230Unlike the
231.Em varargs
232macros, the
233.Nm
234macros do not permit programmers to
235code a function with no fixed arguments.
236This problem generates work mainly when converting
237.Em varargs
238code to
239.Nm
240code,
241but it also creates difficulties for variadic functions that
242wish to pass all of their arguments on to a function
243that takes a
244.Em va_list
245argument, such as
246.Xr vfprintf 3 .
247