Chapter 8. Variable Arguments <stdarg.h>

The header <stdarg.h> declares a type and defines three macros, for advancing through a list of arguments whose number and types and not known to the called function when it is compiled.

A function may be called with a variable number of arguments of varying types. As described in the ANSI C Standard Section 6.7.1, its parameter list contains one or more parameters. The rightmost parameter plays a special role in the access mechanism, and will be designated paramN in this description.

The type declared is:

va_list

which is a type suitable for holding information needed by the macros va_start, va_arg, and va_end. If access to the varying arguments is desired, the called function shall declare an object (referred to as ap in this section) having type va_list. 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 shall be passed to the va_end macro prior to any further reference to ap.

8.1. Variable Argument List Access Macros

8.1.1. The va_start macro

Synopsis
#include <stdarg.h>

void va_start (va_list ap , paramN );

Description

The va_start macro shall be invoked before any access to the unnamed arguments.

The va_start macro initializes ap for subsequent use by va_arg and va_end.

The parameter paramN is the identifier of the rightmost parameter in the variable parameter list in the function definition (the one just before the , ...). If the parameter paramN is declared with the register storage class, with a function or array type, or with a type that is not compatible with the type that results after application of the default argument promotions, the behavior is undefined.

Returns

The va_start macro returns no value.

See Also

Section 8.1.2

Section 8.1.3

8.1.2. The va_arg macro

Synopsis
#include <stdarg.h>

type va_arg (va_list ap , type );

Description

The va_arg macro expands to an expression that has the type and value of the next argument in the call. The parameter ap shall be the same as the va_list ap initialized by va_start. Each invocation of va_arg modifies ap so that the values of successive arguments are returned in turn. The parameter type is a type name specified such that the type of a pointer to an object that has the specified type can be obtained by simply postfixing a * to type. If there is no actual next argument (as promoted according to the default argument promotions), the behavior is undefined.

Returns

The first invocation of the va_arg macro after that of the va_start macro returns the value of the argument after that specified by paramN. Successive invocations return values of the remaining arguments in succession.

See Also

Section 8.1.1

Section 8.1.3

8.1.3. The va_end macro

Synopsis
#include <stdarg.h>

void va_end (va_list ap );

Description

The va_end macro facilitates a normal return from the function whose variable argument list was referred to by the expansion of va_start that initialized the va_list ap. The va_end macro may modify ap so that it is no longer usable (without intervening invocation of va_start). If there is no corresponding invocation of the va_start macro, or if the va_end macro is not invoked bore the return, the behavior is undefined.

Returns

The va_end macro returns no value.

See Also

Section 8.1.1

Section 8.1.2