1.\" $NetBSD: stdarg.3,v 1.15 2002/08/18 08:57:07 yamt 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 August 18, 2002 41.Dt STDARG 3 42.Os 43.Sh NAME 44.Nm stdarg , 45.Nm va_arg , 46.Nm va_copy , 47.Nm va_end , 48.Nm va_start 49.Nd variable argument lists 50.Sh SYNOPSIS 51.Fd #include \*[Lt]stdarg.h\*[Gt] 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_copy "va_list dest" "va_list src" 58.Ft void 59.Fn va_end "va_list ap" 60.Sh DESCRIPTION 61A function may be called with a varying number of arguments of varying 62types. 63The include file 64.Aq Pa stdarg.h 65declares a type 66.Pq Em va_list 67and defines three macros for stepping 68through a list of arguments whose number and types are not known to 69the called function. 70.Pp 71The called function must declare an object of type 72.Em va_list 73which is used by the macros 74.Fn va_start , 75.Fn va_arg , 76.Fn va_end , 77and, optionally, 78.Fn va_copy . 79.Pp 80The 81.Fn va_start 82macro initializes 83.Fa ap 84for subsequent use by 85.Fn va_arg , 86.Fn va_copy 87and 88.Fn va_end , 89and must be called first. 90.Pp 91The parameter 92.Fa last 93is the name of the last parameter before the variable argument list, 94i.e. the last parameter of which the calling function knows the type. 95.Pp 96Because the address of this parameter is used in the 97.Fn va_start 98macro, it should not be declared as a register variable, or as a 99function or an array type. 100.Pp 101The 102.Fn va_start 103macro returns no value. 104.Pp 105The 106.Fn va_arg 107macro expands to an expression that has the type and value of the next 108argument in the call. 109The parameter 110.Fa ap 111is the 112.Em va_list Fa ap 113initialized by 114.Fn va_start . 115Each call to 116.Fn va_arg 117modifies 118.Fa ap 119so that the next call returns the next argument. 120The parameter 121.Fa type 122is a type name specified so that the type of a pointer to an 123object that has the specified type can be obtained simply by 124adding a * 125to 126.Fa type . 127.Pp 128If there is no next argument, or if 129.Fa type 130is not compatible with the type of the actual next argument 131(as promoted according to the default argument promotions), 132random errors will occur. 133.Pp 134If the type in question is one that gets promoted, the promoted type 135should be used as the argument to 136.Fn va_arg . 137The following describes which types are promoted (and to what): 138.Bl -dash -compact 139.It 140.Va short 141is promoted to 142.Va int 143.It 144.Va float 145is promoted to 146.Va double 147.It 148.Va char 149is promoted to 150.Va int 151.El 152.Pp 153The first use of the 154.Fn va_arg 155macro after that of the 156.Fn va_start 157macro returns the argument after 158.Fa last . 159Successive invocations return the values of the remaining 160arguments. 161.Pp 162The 163.Fn va_copy 164macro makes 165.Fa dest 166a copy of 167.Fa src 168as if the 169.Fn va_start 170macro had been applied to it followed by the same sequence of uses of the 171.Fn va_arg 172macro as had previously been used to reach the present state of 173.Fa src . 174.Pp 175The 176.Fn va_copy 177macro returns no value. 178.Pp 179The 180.Fn va_end 181macro handles a normal return from the function whose variable argument 182list was initialized by 183.Fn va_start 184or 185.Fn va_copy . 186.Pp 187The 188.Fn va_end 189macro returns no value. 190.Sh EXAMPLES 191The function 192.Fn foo 193takes a string of format characters and prints out the argument 194associated with each format character based on the type. 195.Bd -literal -offset indent 196void 197foo(char *fmt, ...) 198{ 199 va_list ap; 200 int d, c; 201 char *s; 202 double f; 203 204 va_start(ap, fmt); 205 while (*fmt) 206 switch (*fmt++) { 207 case 's': /* string */ 208 s = va_arg(ap, char *); 209 printf("string %s\en", s); 210 break; 211 case 'd': /* int */ 212 d = va_arg(ap, int); 213 printf("int %d\en", d); 214 break; 215 case 'c': /* char */ 216 c = va_arg(ap, int); /* promoted */ 217 printf("char %c\en", c); 218 break; 219 case 'f': /* float */ 220 f = va_arg(ap, double); /* promoted */ 221 printf("float %f\en", f); 222 } 223 va_end(ap); 224} 225.Ed 226.Sh STANDARDS 227The 228.Fn va_start , 229.Fn va_arg , 230.Fn va_copy , 231and 232.Fn va_end 233macros conform to 234.St -isoC99 . 235.Sh HISTORY 236The 237.Fn va_start , 238.Fn va_arg 239and 240.Fn va_end 241macros were introduced in 242.St -ansiC . 243The 244.Fn va_copy 245macro was introduced in 246.St -isoC99 . 247.Sh COMPATIBILITY 248These macros are 249.Em not 250compatible with the historic macros they replace. 251A backward compatible version can be found in the include 252file 253.Aq Pa varargs.h . 254.Sh BUGS 255Unlike the 256.Em varargs 257macros, the 258.Nm stdarg 259macros do not permit programmers to 260code a function with no fixed arguments. 261This problem generates work mainly when converting 262.Em varargs 263code to 264.Nm stdarg 265code, 266but it also creates difficulties for variadic functions that 267wish to pass all of their arguments on to a function 268that takes a 269.Em va_list 270argument, such as 271.Xr vfprintf 3 . 272