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 55A function may be called with a varying number of arguments of varying 56types. 57The include file 58.Aq Pa stdarg.h 59declares a type 60.Pq Em va_list 61and defines three macros for stepping 62through a list of arguments whose number and types are not known to 63the called function. 64.Pp 65The called function must declare an object of type 66.Em va_list 67which is used by the macros 68.Fn va_start , 69.Fn va_arg , 70and 71.Fn va_end . 72.Pp 73The 74.Fn va_start 75macro initializes 76.Fa ap 77for subsequent use by 78.Fn va_arg 79and 80.Fn va_end , 81and must be called first. 82.Pp 83The parameter 84.Fa last 85is the name of the last parameter before the variable argument list, 86i.e. the last parameter of which the calling function knows the type. 87.Pp 88Because the address of this parameter is used in the 89.Fn va_start 90macro, it should not be declared as a register variable, or as a 91function or an array type. 92.Pp 93The 94.Fn va_start 95macro returns no value. 96.Pp 97The 98.Fn va_arg 99macro expands to an expression that has the type and value of the next 100argument in the call. 101The parameter 102.Fa ap 103is the 104.Em va_list Fa ap 105initialized by 106.Fn va_start . 107Each call to 108.Fn va_arg 109modifies 110.Fa ap 111so that the next call returns the next argument. 112The parameter 113.Fa type 114is a type name specified so that the type of a pointer to an 115object that has the specified type can be obtained simply by 116adding a * 117to 118.Fa type . 119.Pp 120If there is no next argument, or if 121.Fa type 122is not compatible with the type of the actual next argument 123(as promoted according to the default argument promotions), 124random errors will occur. 125.Pp 126The first use of the 127.Fn va_arg 128macro after that of the 129.Fn va_start 130macro returns the argument after 131.Fa last . 132Successive invocations return the values of the remaining 133arguments. 134.Pp 135The 136.Fn va_end 137macro handles a normal return from the function whose variable argument 138list was initialized by 139.Fn va_start . 140.Pp 141The 142.Fn va_end 143macro returns no value. 144.Sh EXAMPLES 145The function 146.Em foo 147takes a string of format characters and prints out the argument 148associated with each format character based on the type. 149.Bd -literal -offset indent 150void 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 176The 177.Fn va_start , 178.Fn va_arg , 179and 180.Fn va_end 181macros conform to 182.St -ansiC . 183.Sh COMPATIBILITY 184These macros are 185.Em not 186compatible with the historic macros they replace. 187A backward compatible version can be found in the include 188file 189.Aq Pa varargs.h . 190.Sh BUGS 191Unlike the 192.Em varargs 193macros, the 194.Nm stdarg 195macros do not permit programmers to 196code a function with no fixed arguments. 197This problem generates work mainly when converting 198.Em varargs 199code to 200.Nm stdarg 201code, 202but it also creates difficulties for variadic functions that 203wish to pass all of their arguments on to a function 204that takes a 205.Em va_list 206argument, such as 207.Xr vfprintf 3 . 208