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$ 38.\" 39.Dd September 18, 2002 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_copy "va_list dest" "va_list src" 53.Ft void 54.Fn va_end "va_list ap" 55.Sh DESCRIPTION 56A function may be called with a varying number of arguments of varying 57types. 58The include file 59.Aq Pa stdarg.h 60declares a type 61.Pq Em va_list 62and defines three macros for stepping 63through a list of arguments whose number and types are not known to 64the called function. 65.Pp 66The called function must declare an object of type 67.Em va_list 68which is used by the macros 69.Fn va_start , 70.Fn va_arg , 71and 72.Fn va_end . 73.Pp 74The 75.Fn va_start 76macro initializes 77.Fa ap 78for subsequent use by 79.Fn va_arg 80and 81.Fn va_end , 82and must be called first. 83.Pp 84The parameter 85.Fa last 86is the name of the last parameter before the variable argument list, 87i.e. the last parameter of which the calling function knows the type. 88.Pp 89Because the address of this parameter is used in the 90.Fn va_start 91macro, it should not be declared as a register variable, or as a 92function or an array type. 93.Pp 94The 95.Fn va_start 96macro returns no value. 97.Pp 98The 99.Fn va_arg 100macro expands to an expression that has the type and value of the next 101argument in the call. 102The parameter 103.Fa ap 104is the 105.Em va_list Fa ap 106initialized by 107.Fn va_start . 108Each call to 109.Fn va_arg 110modifies 111.Fa ap 112so that the next call returns the next argument. 113The parameter 114.Fa type 115is a type name specified so that the type of a pointer to an 116object that has the specified type can be obtained simply by 117adding a * 118to 119.Fa type . 120.Pp 121If there is no next argument, or if 122.Fa type 123is not compatible with the type of the actual next argument 124(as promoted according to the default argument promotions), 125random errors will occur. 126.Pp 127The first use of the 128.Fn va_arg 129macro after that of the 130.Fn va_start 131macro returns the argument after 132.Fa last . 133Successive invocations return the values of the remaining 134arguments. 135.Pp 136The 137.Fn va_copy 138macro copies a variable argument list, previously initialized by 139.Fn va_start , 140from 141.Va src 142to 143.Va dest . 144The state is preserved such that it is equivalent to calling 145.Fn va_start 146with the same second argument used with 147.Va src , 148and calling 149.Fn va_arg 150the same number of times as called with 151.Va src . 152.Pp 153The 154.Fn va_copy 155macro returns no value. 156.Pp 157The 158.Fn va_end 159macro handles a normal return from the function whose variable argument 160list was initialized by 161.Fn va_start . 162.Pp 163The 164.Fn va_end 165macro returns no value. 166.Sh EXAMPLES 167The function 168.Em foo 169takes a string of format characters and prints out the argument 170associated with each format character based on the type. 171.Bd -literal -offset indent 172void foo(char *fmt, ...) 173{ 174 va_list ap; 175 int d; 176 char c, *s; 177 178 va_start(ap, fmt); 179 while (*fmt) 180 switch(*fmt++) { 181 case 's': /* string */ 182 s = va_arg(ap, char *); 183 printf("string %s\en", s); 184 break; 185 case 'd': /* int */ 186 d = va_arg(ap, int); 187 printf("int %d\en", d); 188 break; 189 case 'c': /* char */ 190 /* Note: char is promoted to int. */ 191 c = va_arg(ap, int); 192 printf("char %c\en", c); 193 break; 194 } 195 va_end(ap); 196} 197.Ed 198.Sh STANDARDS 199The 200.Fn va_start , 201.Fn va_arg , 202and 203.Fn va_end 204macros conform to 205.St -isoC . 206.Sh COMPATIBILITY 207These macros are 208.Em not 209compatible with the historic macros they replace. 210A backward compatible version can be found in the include 211file 212.Aq Pa varargs.h . 213.Sh BUGS 214Unlike the 215.Em varargs 216macros, the 217.Nm 218macros do not permit programmers to 219code a function with no fixed arguments. 220This problem generates work mainly when converting 221.Em varargs 222code to 223.Nm 224code, 225but it also creates difficulties for variadic functions that 226wish to pass all of their arguments on to a function 227that takes a 228.Em va_list 229argument, such as 230.Xr vfprintf 3 . 231