161577Sbostic.\" Copyright (c) 1990, 1991, 1993 261577Sbostic.\" The Regents of the University of California. All rights reserved. 320385Smckusick.\" 450338Sbostic.\" This code is derived from software contributed to Berkeley by 550338Sbostic.\" the American National Standards Committee X3, on Information 650338Sbostic.\" Processing Systems. 750338Sbostic.\" 841892Sbostic.\" %sccs.include.redist.man% 941892Sbostic.\" 10*69054Sbostic.\" @(#)stdarg.3 8.2 (Berkeley) 04/28/95 1141892Sbostic.\" 1248349Scael.Dd 1348616Sbostic.Dt STDARG 3 1448725Sdonn.Os 1548349Scael.Sh NAME 1648616Sbostic.Nm stdarg 1748349Scael.Nd variable argument lists 1848349Scael.Sh SYNOPSIS 1948349Scael.Fd #include <stdarg.h> 2048349Scael.Ft void 2148349Scael.Fn va_start "va_list ap" last 2248349Scael.Ft type 2348349Scael.Fn va_arg "va_list ap" type 2448349Scael.Ft void 2548349Scael.Fn va_end "va_list ap" 2648349Scael.Sh DESCRIPTION 2741892SbosticA function may be called with a varying number of arguments of varying 2841892Sbostictypes. 2941892SbosticThe include file 3048349Scael.Aq Pa stdarg.h 3148349Scaeldeclares a type 3248349Scael.Pq Em va_list 3348349Scaeland defines three macros for stepping 3441892Sbosticthrough a list of arguments whose number and types are not known to 3541892Sbosticthe called function. 3648349Scael.Pp 3741892SbosticThe called function must declare an object of type 3848349Scael.Em va_list 3941892Sbosticwhich is used by the macros 4048349Scael.Fn va_start , 4148349Scael.Fn va_arg , 4241892Sbosticand 4348349Scael.Fn va_end . 4448349Scael.Pp 4541892SbosticThe 4648349Scael.Fn va_start 4741892Sbosticmacro initializes 4848349Scael.Fa ap 4941892Sbosticfor subsequent use by 5048349Scael.Fn va_arg 5141892Sbosticand 5248349Scael.Fn va_end , 5341892Sbosticand must be called first. 5448349Scael.Pp 5541892SbosticThe parameter 5648349Scael.Fa last 5741892Sbosticis the name of the last parameter before the variable argument list, 5841892Sbostici.e. the last parameter of which the calling function knows the type. 5948349Scael.Pp 6041892SbosticBecause the address of this parameter is used in the 6148349Scael.Fn va_start 6241892Sbosticmacro, it should not be declared as a register variable, or as a 6348349Scaelfunction or an array type. 6448349Scael.Pp 6541892SbosticThe 6648349Scael.Fn va_start 6741892Sbosticmacro returns no value. 6848349Scael.Pp 6941892SbosticThe 7048349Scael.Fn va_arg 7141892Sbosticmacro expands to an expression that has the type and value of the next 7241892Sbosticargument in the call. 7341892SbosticThe parameter 7448349Scael.Fa ap 7541892Sbosticis the 7648349Scael.Em va_list Fa ap 7741892Sbosticinitialized by 7848349Scael.Fn va_start . 7941892SbosticEach call to 8048349Scael.Fn va_arg 8141892Sbosticmodifies 8248349Scael.Fa ap 8341892Sbosticso that the next call returns the next argument. 8441892SbosticThe parameter 8548349Scael.Fa type 8641892Sbosticis a type name specified so that the type of a pointer to an 8741892Sbosticobject that has the specified type can be obtained simply by 8848349Scaeladding a * 8941892Sbosticto 9048349Scael.Fa type . 9148349Scael.Pp 9241892SbosticIf there is no next argument, or if 9348349Scael.Fa type 9441892Sbosticis not compatible with the type of the actual next argument 9541892Sbostic(as promoted according to the default argument promotions), 9641892Sbosticrandom errors will occur. 9748349Scael.Pp 9841892SbosticThe first use of the 9948349Scael.Fn va_arg 10041892Sbosticmacro after that of the 10148349Scael.Fn va_start 10241892Sbosticmacro returns the argument after 10348349Scael.Fa last . 10441892SbosticSuccessive invocations return the values of the remaining 10541892Sbosticarguments. 10648349Scael.Pp 10741892SbosticThe 10848349Scael.Fn va_end 10941892Sbosticmacro handles a normal return from the function whose variable argument 11041892Sbosticlist was initialized by 11148349Scael.Fn va_start . 11248349Scael.Pp 11341892SbosticThe 11448349Scael.Fn va_end 11541892Sbosticmacro returns no value. 11648349Scael.Sh EXAMPLES 11741892SbosticThe function 11848349Scael.Em foo 11941892Sbostictakes a string of format characters and prints out the argument 12041892Sbosticassociated with each format character based on the type. 12148349Scael.Bd -literal -offset indent 12248725Sdonnvoid foo(char *fmt, ...) 12341892Sbostic{ 12441892Sbostic va_list ap; 12541892Sbostic int d; 126*69054Sbostic char c, *s; 12720385Smckusick 12841892Sbostic va_start(ap, fmt); 12941892Sbostic while (*fmt) 130*69054Sbostic switch (*fmt++) { 13141892Sbostic case 's': /* string */ 13241892Sbostic s = va_arg(ap, char *); 13341892Sbostic printf("string %s\en", s); 13441892Sbostic break; 13541892Sbostic case 'd': /* int */ 13641892Sbostic d = va_arg(ap, int); 13741892Sbostic printf("int %d\en", d); 13841892Sbostic break; 13941892Sbostic case 'c': /* char */ 14041892Sbostic c = va_arg(ap, char); 14141892Sbostic printf("char %c\en", c); 14241892Sbostic break; 14341892Sbostic } 14441892Sbostic va_end(ap); 14541892Sbostic} 14648349Scael.Ed 14748349Scael.Sh STANDARDS 14848349ScaelThe 14948349Scael.Fn va_start , 15048349Scael.Fn va_arg , 15148349Scaeland 15248349Scael.Fn va_end 15348349Scaelmacros conform to 15448349Scael.St -ansiC . 15548349Scael.Sh COMPATIBILITY 15648349ScaelThese macros are 15748349Scael.Em not 15848349Scaelcompatible with the historic macros they replace. 15948349ScaelA backward compatible version can be found in the include 16048349Scaelfile 16148349Scael.Aq Pa varargs.h . 16248725Sdonn.Sh BUGS 16348725SdonnUnlike the 16448725Sdonn.Em varargs 16548725Sdonnmacros, the 16648725Sdonn.Nm stdarg 16748725Sdonnmacros do not permit programmers to 16848725Sdonncode a function with no fixed arguments. 16948725SdonnThis problem generates work mainly when converting 17048725Sdonn.Em varargs 17148725Sdonncode to 17248725Sdonn.Nm stdarg 17348725Sdonncode, 17448725Sdonnbut it also creates difficulties for variadic functions that 17548725Sdonnwish to pass all of their arguments on to a function 17648725Sdonnthat takes a 17748725Sdonn.Em va_list 17848725Sdonnargument, such as 17948725Sdonn.Xr vfprintf 3 . 180