C Programming/stdarg.h

  is a header that allows functions to accept an indefinite number of arguments. It provides facilities for stepping through a list of function arguments of unknown number and type.

The contents of  are typically used in variadic functions, though they may be used in other functions (for example,  ) called by variadic functions.

Declaring variadic functions
Variadic functions are functions which may take a variable number of arguments and are declared with an ellipsis in place of the last parameter. An example of such a function is. A typical declaration is

Variadic functions must have at least one named parameter, so, for instance,

is not allowed in C. (In C++, such a declaration is permitted, but not very useful.) In C, a comma must precede the ellipsis; in C++, it is optional.

Defining variadic functions
The same syntax is used in a definition:

An ellipsis may also appear in old-style function definitions:

Accessing the arguments
To access the unnamed arguments, one must declare a variable of type  in the variadic function. The macro  is then called with two arguments: the first is the variable declared of the type , the second is the name of the last named parameter of the function. After this, each invocation of the  macro yields the next argument. The first argument to  is the   and the second is the type of the next argument passed to the function. Finally, the  macro must be called on the   before the function returns. (It is not required to read in all the arguments.)

C99 provides an additional macro,, which can duplicate the state of a. The macro invocation  copies   into.

There is no mechanism defined for determining the number or types of the unnamed arguments passed to the function. The function is simply required to know or determine this somehow, the means of which vary. Common conventions include:
 * Use of a  or  -like format string with embedded specifiers that indicate argument types.
 * A sentinel value at the end of the variadic arguments.
 * A count argument indicating the number of variadic arguments.

Type safety
Some C implementations provide C extensions that allow the compiler to check for the proper use of format strings and sentinels. Barring these extensions, the compiler usually cannot check whether the unnamed arguments passed are of the type the function expects, or convert them to the required type. Therefore, care should be taken to ensure correctness in this regard, since undefined behavior results if the types do not match.

For example, if calling  then a null pointer for that parameter must be passed in the call as  ; using   is incorrect as pointers to different data types can be different sizes. could be used instead, but gains nothing and probably leads the unwary reader into thinking the cast isn't required.

Another consideration is the default argument promotions applied to the unnamed arguments. A  will automatically be promoted to a. Likewise, arguments of types narrower than an  will be promoted to   or. The function receiving the unnamed arguments must expect the promoted type.

GCC has an extension that checks the passed arguments:

"   The format attribute specifies that a function takes,,   or   style arguments which should be type-checked against a format string. For example, the declaration:

causes the compiler to check the arguments in calls to  for consistency with the   style format string argument  ."

Example
This program yields the output:

5 2 14 84 97 15 24 48 84 51

1

To call other var args functions from within your function (such as sprintf) you need to use the var arg version of the function (vsprintf in this example):

POSIX defines the legacy header, which dates from before the standardization of C and provides functionality similar to. This header is not part of ISO C. The file, as defined in the second version of the Single UNIX Specification, simply contains all of the functionality of C89, with the exceptions that: it cannot be used in standard C new-style definitions; you may choose not to have a given argument (standard C requires at least one argument); and the way it works is different—in standard C, one would write:

or with old-style function definitions:

and call with

With, the function would be:

and is called the same way.

requires old-style function definitions because of the way the implementation works.