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