1*48349Scael.\" Copyright (c) 1990, 1991 The Regents of the University of California. 241892Sbostic.\" All rights reserved. 320385Smckusick.\" 441892Sbostic.\" %sccs.include.redist.man% 541892Sbostic.\" 6*48349Scael.\" @(#)stdarg.3 6.5 (Berkeley) 04/19/91 741892Sbostic.\" 8*48349Scael.Dd 9*48349Scael.Dt VARARGS 3 10*48349Scael.Os ATT 7th 11*48349Scael.Sh NAME 12*48349Scael.Nm varargs 13*48349Scael.Nd variable argument lists 14*48349Scael.Sh SYNOPSIS 15*48349Scael.Fd #include <stdarg.h> 16*48349Scael.Ft void 17*48349Scael.Fn va_start "va_list ap" last 18*48349Scael.Ft type 19*48349Scael.Fn va_arg "va_list ap" type 20*48349Scael.Ft void 21*48349Scael.Fn va_end "va_list ap" 22*48349Scael.Sh DESCRIPTION 2341892SbosticA function may be called with a varying number of arguments of varying 2441892Sbostictypes. 2541892SbosticThe include file 26*48349Scael.Aq Pa stdarg.h 27*48349Scaeldeclares a type 28*48349Scael.Pq Em va_list 29*48349Scaeland defines three macros for stepping 3041892Sbosticthrough a list of arguments whose number and types are not known to 3141892Sbosticthe called function. 32*48349Scael.Pp 3341892SbosticThe called function must declare an object of type 34*48349Scael.Em va_list 3541892Sbosticwhich is used by the macros 36*48349Scael.Fn va_start , 37*48349Scael.Fn va_arg , 3841892Sbosticand 39*48349Scael.Fn va_end . 40*48349Scael.Pp 4141892SbosticThe 42*48349Scael.Fn va_start 4341892Sbosticmacro initializes 44*48349Scael.Fa ap 4541892Sbosticfor subsequent use by 46*48349Scael.Fn va_arg 4741892Sbosticand 48*48349Scael.Fn va_end , 4941892Sbosticand must be called first. 50*48349Scael.Pp 5141892SbosticThe parameter 52*48349Scael.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. 55*48349Scael.Pp 5641892SbosticBecause the address of this parameter is used in the 57*48349Scael.Fn va_start 5841892Sbosticmacro, it should not be declared as a register variable, or as a 59*48349Scaelfunction or an array type. 60*48349Scael.Pp 6141892SbosticThe 62*48349Scael.Fn va_start 6341892Sbosticmacro returns no value. 64*48349Scael.Pp 6541892SbosticThe 66*48349Scael.Fn va_arg 6741892Sbosticmacro expands to an expression that has the type and value of the next 6841892Sbosticargument in the call. 6941892SbosticThe parameter 70*48349Scael.Fa ap 7141892Sbosticis the 72*48349Scael.Em va_list Fa ap 7341892Sbosticinitialized by 74*48349Scael.Fn va_start . 7541892SbosticEach call to 76*48349Scael.Fn va_arg 7741892Sbosticmodifies 78*48349Scael.Fa ap 7941892Sbosticso that the next call returns the next argument. 8041892SbosticThe parameter 81*48349Scael.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 84*48349Scaeladding a * 8541892Sbosticto 86*48349Scael.Fa type . 87*48349Scael.Pp 8841892SbosticIf there is no next argument, or if 89*48349Scael.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. 93*48349Scael.Pp 9441892SbosticThe first use of the 95*48349Scael.Fn va_arg 9641892Sbosticmacro after that of the 97*48349Scael.Fn va_start 9841892Sbosticmacro returns the argument after 99*48349Scael.Fa last . 10041892SbosticSuccessive invocations return the values of the remaining 10141892Sbosticarguments. 102*48349Scael.Pp 10341892SbosticThe 104*48349Scael.Fn va_end 10541892Sbosticmacro handles a normal return from the function whose variable argument 10641892Sbosticlist was initialized by 107*48349Scael.Fn va_start . 108*48349Scael.Pp 10941892SbosticThe 110*48349Scael.Fn va_end 11141892Sbosticmacro returns no value. 112*48349Scael.Sh EXAMPLES 11341892SbosticThe function 114*48349Scael.Em foo 11541892Sbostictakes a string of format characters and prints out the argument 11641892Sbosticassociated with each format character based on the type. 117*48349Scael.Bd -literal -offset indent 118*48349Scaelfoo(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} 142*48349Scael.Ed 143*48349Scael.Sh STANDARDS 144*48349ScaelThe 145*48349Scael.Fn va_start , 146*48349Scael.Fn va_arg , 147*48349Scaeland 148*48349Scael.Fn va_end 149*48349Scaelmacros conform to 150*48349Scael.St -ansiC . 151*48349Scael.Sh COMPATIBILITY 152*48349ScaelThese macros are 153*48349Scael.Em not 154*48349Scaelcompatible with the historic macros they replace. 155*48349ScaelA backward compatible version can be found in the include 156*48349Scaelfile 157*48349Scael.Aq Pa varargs.h . 158