]> git.saurik.com Git - apple/libc.git/blob - man/stdarg.3
Libc-498.1.7.tar.gz
[apple/libc.git] / man / stdarg.3
1 .\" $NetBSD: stdarg.3,v 1.3 1994/11/30 15:24:37 jtc 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 .Nd variable argument lists
46 .Sh SYNOPSIS
47 .Fd #include <stdarg.h>
48 .Ft void
49 .Fn va_start "va_list ap" last
50 .Ft type
51 .Fn va_arg "va_list ap" type
52 .Ft void
53 .Fn va_end "va_list ap"
54 .Sh DESCRIPTION
55 A function may be called with a varying number of arguments of varying
56 types.
57 The include file
58 .Aq Pa stdarg.h
59 declares a type
60 .Pq Em va_list
61 and defines three macros for stepping
62 through a list of arguments whose number and types are not known to
63 the called function.
64 .Pp
65 The called function must declare an object of type
66 .Em va_list
67 which is used by the macros
68 .Fn va_start ,
69 .Fn va_arg ,
70 and
71 .Fn va_end .
72 .Pp
73 The
74 .Fn va_start
75 macro initializes
76 .Fa ap
77 for subsequent use by
78 .Fn va_arg
79 and
80 .Fn va_end ,
81 and must be called first.
82 .Pp
83 The parameter
84 .Fa last
85 is the name of the last parameter before the variable argument list,
86 i.e. the last parameter of which the calling function knows the type.
87 .Pp
88 Because the address of this parameter is used in the
89 .Fn va_start
90 macro, it should not be declared as a register variable, or as a
91 function or an array type.
92 .Pp
93 The
94 .Fn va_start
95 macro returns no value.
96 .Pp
97 The
98 .Fn va_arg
99 macro expands to an expression that has the type and value of the next
100 argument in the call.
101 The parameter
102 .Fa ap
103 is the
104 .Em va_list Fa ap
105 initialized by
106 .Fn va_start .
107 Each call to
108 .Fn va_arg
109 modifies
110 .Fa ap
111 so that the next call returns the next argument.
112 The parameter
113 .Fa type
114 is a type name specified so that the type of a pointer to an
115 object that has the specified type can be obtained simply by
116 adding a *
117 to
118 .Fa type .
119 .Pp
120 If there is no next argument, or if
121 .Fa type
122 is not compatible with the type of the actual next argument
123 (as promoted according to the default argument promotions),
124 random errors will occur.
125 .Pp
126 The first use of the
127 .Fn va_arg
128 macro after that of the
129 .Fn va_start
130 macro returns the argument after
131 .Fa last .
132 Successive invocations return the values of the remaining
133 arguments.
134 .Pp
135 The
136 .Fn va_end
137 macro handles a normal return from the function whose variable argument
138 list was initialized by
139 .Fn va_start .
140 .Pp
141 The
142 .Fn va_end
143 macro returns no value.
144 .Sh EXAMPLES
145 The function
146 .Em foo
147 takes a string of format characters and prints out the argument
148 associated with each format character based on the type.
149 .Bd -literal -offset indent
150 void foo(char *fmt, ...)
151 {
152 va_list ap;
153 int d;
154 char c, *p, *s;
155
156 va_start(ap, fmt);
157 while (*fmt)
158 switch(*fmt++) {
159 case 's': /* string */
160 s = va_arg(ap, char *);
161 printf("string %s\en", s);
162 break;
163 case 'd': /* int */
164 d = va_arg(ap, int);
165 printf("int %d\en", d);
166 break;
167 case 'c': /* char */
168 c = va_arg(ap, char);
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 -ansiC .
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 stdarg
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 stdarg
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 .