Remove advertising header from man pages.
[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 .\" 4. 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 .\"     @(#)stdarg.3    8.1 (Berkeley) 6/5/93
33 .\" $FreeBSD: src/share/man/man3/stdarg.3,v 1.15 2005/01/21 08:36:36 ru Exp $
34 .\" $DragonFly: src/share/man/man3/stdarg.3,v 1.3 2005/11/20 11:05:44 swildner Exp $
35 .\"
36 .Dd October 25, 2002
37 .Dt STDARG 3
38 .Os
39 .Sh NAME
40 .Nm stdarg
41 .Nd variable argument lists
42 .Sh SYNOPSIS
43 .In stdarg.h
44 .Ft void
45 .Fn va_start "va_list ap" last
46 .Ft type
47 .Fn va_arg "va_list ap" type
48 .Ft void
49 .Fn va_copy "va_list dest" "va_list src"
50 .Ft void
51 .Fn va_end "va_list ap"
52 .Sh DESCRIPTION
53 A function may be called with a varying number of arguments of varying
54 types.
55 The include file
56 .In stdarg.h
57 declares a type
58 .Pq Em va_list
59 and defines three macros for stepping
60 through a list of arguments whose number and types are not known to
61 the called function.
62 .Pp
63 The called function must declare an object of type
64 .Em va_list
65 which is used by the macros
66 .Fn va_start ,
67 .Fn va_arg ,
68 .Fn va_copy ,
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_copy
136 macro copies a variable argument list, previously initialized by
137 .Fn va_start ,
138 from
139 .Fa src
140 to
141 .Fa dest .
142 The state is preserved such that it is equivalent to calling
143 .Fn va_start
144 with the same second argument used with
145 .Fa src ,
146 and calling
147 .Fn va_arg
148 the same number of times as called with
149 .Fa src .
150 .Pp
151 The
152 .Fn va_copy
153 macro returns no value.
154 .Pp
155 The
156 .Fn va_end
157 macro handles a normal return from the function whose variable argument
158 list was initialized by
159 .Fn va_start .
160 .Pp
161 The
162 .Fn va_end
163 macro returns no value.
164 .Sh EXAMPLES
165 The function
166 .Em foo
167 takes a string of format characters and prints out the argument
168 associated with each format character based on the type.
169 .Bd -literal -offset indent
170 void foo(char *fmt, ...)
171 {
172         va_list ap;
173         int d;
174         char c, *s;
175
176         va_start(ap, fmt);
177         while (*fmt)
178                 switch(*fmt++) {
179                 case 's':                       /* string */
180                         s = va_arg(ap, char *);
181                         printf("string %s\en", s);
182                         break;
183                 case 'd':                       /* int */
184                         d = va_arg(ap, int);
185                         printf("int %d\en", d);
186                         break;
187                 case 'c':                       /* char */
188                         /* Note: char is promoted to int. */
189                         c = va_arg(ap, int);
190                         printf("char %c\en", c);
191                         break;
192                 }
193         va_end(ap);
194 }
195 .Ed
196 .Sh COMPATIBILITY
197 These macros are
198 .Em not
199 compatible with the historic macros they replace.
200 A backward compatible version can be found in the include
201 file
202 .In varargs.h .
203 .Sh STANDARDS
204 The
205 .Fn va_start ,
206 .Fn va_arg ,
207 .Fn va_copy ,
208 and
209 .Fn va_end
210 macros conform to
211 .St -isoC-99 .
212 .Sh BUGS
213 Unlike the
214 .Em varargs
215 macros, the
216 .Nm
217 macros do not permit programmers to
218 code a function with no fixed arguments.
219 This problem generates work mainly when converting
220 .Em varargs
221 code to
222 .Nm
223 code,
224 but it also creates difficulties for variadic functions that
225 wish to pass all of their arguments on to a function
226 that takes a
227 .Em va_list
228 argument, such as
229 .Xr vfprintf 3 .