varargs(S) 6 January 1993 varargs(S) Name varargs, stdarg - variable argument list Syntax varargs.h Syntax #include <varargs.h> function (va_alist) va_dcl va_list pvar; va_start (pvar); f = va_arg (pvar, type); va_end (pvar); stdarg.h Syntax #include <stdarg.h> function (parmN, ...) va_list pvar; va_start (pvar, parmN); f = va_arg (pvar, type); va_end (pvar); Description This man page describes two header files that allow the manipulation of variable argument lists. varargs.h is provided for backward compatibil- ity while stdarg.h is provided for greater portability. varargs.h Description These macros provide a means of writing portable procedures that accept variable argument lists. Routines having variable argument lists (such as printf(S) that do not use either stdarg or varargs are inherently non- portable, since different machines use different argument passing conven- tions. vaalist is used in a function header to denote a variable argument list. vadcl is a declaration for vaalist. Note that there is no semicolon after vadcl. valist is a type which can be used for the variable pvar, which is used to traverse the list. One such variable must always be declared. vastart (pvar) is called to initialize pvar to the beginning of the list. vaarg (pvar, type) returns the next argument in the list pointed to by pvar. type is the type the argument is expected to be. Different types can be mixed but it is up to the routine to know what type of argument is expected since it cannot be determined at runtime. vaend (pvar) is used to finish up. Multiple traversals, each bracketed by vastart ... vaend, are possible. stdarg.h Description These macros provide a means of writing portable procedures that accept variable argument lists. Routines having variable argument lists (such as printf(S) that do not use either stdarg or varargs are inherently non- portable, since different machines use different argument passing conven- tions. valist is a type which can be used for the variable pvar, which is used to traverse the list. One such variable must always be declared. vastart (pvar, parmN) is called to initialize pvar to the beginning of the list. parmN is the identifier of the rightmost parameter in the variable parameter list in the function definition. vaarg (pvar, type) returns the next argument in the list pointed to by pvar. type is the type the argument is expected to be. Different types can be mixed but it is up to the routine to know what type of argument is expected since it cannot be determined at runtime. vaend (pvar) is used to finish up. Multiple traversals, each bracketed by vastart ... vaend, are possible. Examples The following examples illustrate the use of the macros specified in each header file. varargs.h example The following example illustrates the use of the macros specified in varargs.h. #include <stdio.h> #include <varargs.h> /* * the first argument is an int which tells how many pairs follow. * the pairs are doubles and character pointers * * remember that when variables are passed to functions * floats are promoted to doubles and chars to ints. */ void show(n, va_alist) int n; va_dcl { va_list ap; int i; double f; char *p; va_start(ap); for (i = 0; i < n; ++i) { f = va_arg(ap, double); p = va_arg(ap, char *); printf("%4.1f %s\n", f, p); } va_end(ap); } main() { show(2, 3.1, "but", 4.1, "end"); show(1, 5.9, "hello"); show(4, 6.2, "oops", 5.3, "blah", 5.1, "lovely", 2.3, "madrigal"); } stdarg.h example The following example illustrates the use of the macros specified in stdarg.h. #include <stdio.h> #include <stdarg.h> /* * the first argument is an int which tells how many pairs follow. * the pairs are doubles and character pointers * * remember that when variables are passed to functions * floats are promoted to doubles and chars to ints. */ void show(int n, ...) { va_list ap; int i; double f; char *p; va_start(ap, n); for (i = 0; i < n; ++i) { f = va_arg(ap, double); p = va_arg(ap, char *); printf("%4.1f %s\n", f, p); } va_end(ap); } main() { show(2, 3.1, "but", 4.1, "end"); show(1, 5.9, "hello"); show(4, 6.2, "oops", 5.3, "blah", 5.1, "lovely", 2.3, "madrigal"); } Notes It is up to the called routine to determine how many arguments there are, since it is not possible to determine this from the stack frame. For example, excel passes a 0 to signal the end of the list. printf can tell how many arguments are supposed to be there by the format of the list. Standards conformance stdarg, vaarg, vaend, valist, and vastart are conformant with: ANSI X3.159-1989 Programming Language -- C; X/Open Portability Guide, Issue 3, 1989; IEEE POSIX Std 1003.1-1990 System Application Program Interface (API) [C Language] (ISO/IEC 9945-1). varargs is conformant with: AT&T SVID Issue 2.