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