1.\" $NetBSD: stdarg.3,v 1.14 2002/02/13 08:17:30 ross 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 April 14, 2001 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 134The first use of the 135.Fn va_arg 136macro after that of the 137.Fn va_start 138macro returns the argument after 139.Fa last . 140Successive invocations return the values of the remaining 141arguments. 142.Pp 143The 144.Fn va_copy 145macro makes 146.Fa dest 147a copy of 148.Fa src 149as if the 150.Fn va_start 151macro had been applied to it followed by the same sequence of uses of the 152.Fn va_arg 153macro as had previously been used to reach the present state of 154.Fa src . 155.Pp 156The 157.Fn va_copy 158macro returns no value. 159.Pp 160The 161.Fn va_end 162macro handles a normal return from the function whose variable argument 163list was initialized by 164.Fn va_start 165or 166.Fn va_copy . 167.Pp 168The 169.Fn va_end 170macro returns no value. 171.Sh EXAMPLES 172The function 173.Em foo 174takes a string of format characters and prints out the argument 175associated with each format character based on the type. 176.Bd -literal -offset indent 177void foo(char *fmt, ...) 178{ 179 va_list ap; 180 int d; 181 char c, *p, *s; 182 183 va_start(ap, fmt); 184 while (*fmt) { 185 switch (*fmt++) { 186 case 's': /* string */ 187 s = va_arg(ap, char *); 188 printf("string %s\en", s); 189 break; 190 case 'd': /* int */ 191 d = va_arg(ap, int); 192 printf("int %d\en", d); 193 break; 194 case 'c': /* char */ 195 c = va_arg(ap, char); 196 printf("char %c\en", c); 197 break; 198 } 199 } 200 va_end(ap); 201} 202.Ed 203.Sh STANDARDS 204The 205.Fn va_start , 206.Fn va_arg , 207.Fn va_copy , 208and 209.Fn va_end 210macros conform to 211.St -isoC99 . 212.Sh HISTORY 213The 214.Fn va_start , 215.Fn va_arg 216and 217.Fn va_end 218macros were introduced in 219.St -ansiC . 220The 221.Fn va_copy 222macro was introduced in 223.St -isoC99 . 224.Sh COMPATIBILITY 225These macros are 226.Em not 227compatible with the historic macros they replace. 228A backward compatible version can be found in the include 229file 230.Aq Pa varargs.h . 231.Sh BUGS 232Unlike the 233.Em varargs 234macros, the 235.Nm stdarg 236macros do not permit programmers to 237code a function with no fixed arguments. 238This problem generates work mainly when converting 239.Em varargs 240code to 241.Nm stdarg 242code, 243but it also creates difficulties for variadic functions that 244wish to pass all of their arguments on to a function 245that takes a 246.Em va_list 247argument, such as 248.Xr vfprintf 3 . 249