148349Scael.\" Copyright (c) 1990, 1991 The Regents of the University of California. 241892Sbostic.\" All rights reserved. 320385Smckusick.\" 441892Sbostic.\" %sccs.include.redist.man% 541892Sbostic.\" 6*48616Sbostic.\" @(#)stdarg.3 6.6 (Berkeley) 04/24/91 741892Sbostic.\" 848349Scael.Dd 9*48616Sbostic.Dt STDARG 3 1048349Scael.Os ATT 7th 1148349Scael.Sh NAME 12*48616Sbostic.Nm stdarg 1348349Scael.Nd variable argument lists 1448349Scael.Sh SYNOPSIS 1548349Scael.Fd #include <stdarg.h> 1648349Scael.Ft void 1748349Scael.Fn va_start "va_list ap" last 1848349Scael.Ft type 1948349Scael.Fn va_arg "va_list ap" type 2048349Scael.Ft void 2148349Scael.Fn va_end "va_list ap" 2248349Scael.Sh DESCRIPTION 2341892SbosticA function may be called with a varying number of arguments of varying 2441892Sbostictypes. 2541892SbosticThe include file 2648349Scael.Aq Pa stdarg.h 2748349Scaeldeclares a type 2848349Scael.Pq Em va_list 2948349Scaeland defines three macros for stepping 3041892Sbosticthrough a list of arguments whose number and types are not known to 3141892Sbosticthe called function. 3248349Scael.Pp 3341892SbosticThe called function must declare an object of type 3448349Scael.Em va_list 3541892Sbosticwhich is used by the macros 3648349Scael.Fn va_start , 3748349Scael.Fn va_arg , 3841892Sbosticand 3948349Scael.Fn va_end . 4048349Scael.Pp 4141892SbosticThe 4248349Scael.Fn va_start 4341892Sbosticmacro initializes 4448349Scael.Fa ap 4541892Sbosticfor subsequent use by 4648349Scael.Fn va_arg 4741892Sbosticand 4848349Scael.Fn va_end , 4941892Sbosticand must be called first. 5048349Scael.Pp 5141892SbosticThe parameter 5248349Scael.Fa last 5341892Sbosticis the name of the last parameter before the variable argument list, 5441892Sbostici.e. the last parameter of which the calling function knows the type. 5548349Scael.Pp 5641892SbosticBecause the address of this parameter is used in the 5748349Scael.Fn va_start 5841892Sbosticmacro, it should not be declared as a register variable, or as a 5948349Scaelfunction or an array type. 6048349Scael.Pp 6141892SbosticThe 6248349Scael.Fn va_start 6341892Sbosticmacro returns no value. 6448349Scael.Pp 6541892SbosticThe 6648349Scael.Fn va_arg 6741892Sbosticmacro expands to an expression that has the type and value of the next 6841892Sbosticargument in the call. 6941892SbosticThe parameter 7048349Scael.Fa ap 7141892Sbosticis the 7248349Scael.Em va_list Fa ap 7341892Sbosticinitialized by 7448349Scael.Fn va_start . 7541892SbosticEach call to 7648349Scael.Fn va_arg 7741892Sbosticmodifies 7848349Scael.Fa ap 7941892Sbosticso that the next call returns the next argument. 8041892SbosticThe parameter 8148349Scael.Fa type 8241892Sbosticis a type name specified so that the type of a pointer to an 8341892Sbosticobject that has the specified type can be obtained simply by 8448349Scaeladding a * 8541892Sbosticto 8648349Scael.Fa type . 8748349Scael.Pp 8841892SbosticIf there is no next argument, or if 8948349Scael.Fa type 9041892Sbosticis not compatible with the type of the actual next argument 9141892Sbostic(as promoted according to the default argument promotions), 9241892Sbosticrandom errors will occur. 9348349Scael.Pp 9441892SbosticThe first use of the 9548349Scael.Fn va_arg 9641892Sbosticmacro after that of the 9748349Scael.Fn va_start 9841892Sbosticmacro returns the argument after 9948349Scael.Fa last . 10041892SbosticSuccessive invocations return the values of the remaining 10141892Sbosticarguments. 10248349Scael.Pp 10341892SbosticThe 10448349Scael.Fn va_end 10541892Sbosticmacro handles a normal return from the function whose variable argument 10641892Sbosticlist was initialized by 10748349Scael.Fn va_start . 10848349Scael.Pp 10941892SbosticThe 11048349Scael.Fn va_end 11141892Sbosticmacro returns no value. 11248349Scael.Sh EXAMPLES 11341892SbosticThe function 11448349Scael.Em foo 11541892Sbostictakes a string of format characters and prints out the argument 11641892Sbosticassociated with each format character based on the type. 11748349Scael.Bd -literal -offset indent 11848349Scaelfoo(char *fmt) 11941892Sbostic{ 12041892Sbostic va_list ap; 12141892Sbostic int d; 12241892Sbostic char c, *p, *s; 12320385Smckusick 12441892Sbostic va_start(ap, fmt); 12541892Sbostic while (*fmt) 12641892Sbostic switch(*fmt++) { 12741892Sbostic case 's': /* string */ 12841892Sbostic s = va_arg(ap, char *); 12941892Sbostic printf("string %s\en", s); 13041892Sbostic break; 13141892Sbostic case 'd': /* int */ 13241892Sbostic d = va_arg(ap, int); 13341892Sbostic printf("int %d\en", d); 13441892Sbostic break; 13541892Sbostic case 'c': /* char */ 13641892Sbostic c = va_arg(ap, char); 13741892Sbostic printf("char %c\en", c); 13841892Sbostic break; 13941892Sbostic } 14041892Sbostic va_end(ap); 14141892Sbostic} 14248349Scael.Ed 14348349Scael.Sh STANDARDS 14448349ScaelThe 14548349Scael.Fn va_start , 14648349Scael.Fn va_arg , 14748349Scaeland 14848349Scael.Fn va_end 14948349Scaelmacros conform to 15048349Scael.St -ansiC . 15148349Scael.Sh COMPATIBILITY 15248349ScaelThese macros are 15348349Scael.Em not 15448349Scaelcompatible with the historic macros they replace. 15548349ScaelA backward compatible version can be found in the include 15648349Scaelfile 15748349Scael.Aq Pa varargs.h . 158