The Single UNIX ® Specification, Version 2
Copyright © 1997 The Open Group


stdarg.h - handle variable argument list


#include <stdarg.h>

void va_start(va_list ap, argN);
type va_arg(va_list ap, type);
void va_end(va_list ap);


The <stdarg.h> header contains a set of macros which allows portable functions that accept variable argument lists to be written. Functions that have variable argument lists (such as printf()) but do not use these macros are inherently non-portable, as different systems use different argument-passing conventions.

The type va_list is defined for variables used to traverse the list.

The va_start() macro is invoked to initialise ap to the beginning of the list before any calls to va_arg().

The object ap may be passed as an argument to another function; if that function invokes the va_arg() macro with parameter ap, the value of ap in the calling function is indeterminate and must be passed to the va_end() macro prior to any further reference to ap. The parameter argN is the identifier of the rightmost parameter in the variable parameter list in the function definition (the one just before the , ...). If the parameter argN is declared with the register storage class, with a function type or array type, or with a type that is not compatible with the type that results after application of the default argument promotions, the behaviour is undefined.

The va_arg() macro will return the next argument in the list pointed to by ap. Each invocation of va_arg() modifies ap so that the values of successive arguments are returned in turn. The type parameter is the type the argument is expected to be. This is the type name specified such that the type of a pointer to an object that has the specified type can be obtained simply by suffixing a * to type. Different types can be mixed, but it is up to the routine to know what type of argument is expected.

The va_end() macro is used to clean up; it invalidates ap for use (unless va_start() is invoked again).

Multiple traversals, each bracketed by va_start() va_end(), are possible.


This example is a possible implementation of execl().

#include <stdarg.h>

#define  MAXARGS     31

 * execl is called by
 * execl(file, arg1, arg2, ..., (char *)(0));
int execl (const char *file, const char *args, ...)
    va_list ap;
    char *array[MAXARGS];
    int argno = 0;
        va_start(ap, args);
    while (args != 0) {
        array[argno++] = args;
        args = va_arg(ap, const char *);
return execv(file, array);


It is up to the calling routine to communicate to the called routine how many arguments there are, since it is not always possible for the called routine to determine this in any other way. For example, execl() is passed a null pointer to signal the end of the list. The printf() function can tell how many arguments are there by the format argument.




exec, printf().

UNIX ® is a registered Trademark of The Open Group.
Copyright © 1997 The Open Group
[ Main Index | XSH | XCU | XBD | XCURSES | XNS ]