7.4.10 <stdarg.h>

Enables referencing of variable arguments for functions with such arguments.
The following macros are implementation-defined.

Type

Definition Name

Description

Type
(macro)

va_list

Indicates the types of variables used in common by the va_start, va_arg, and va_end macros in order to reference variable arguments.

Function
(macro)

va_start

Executes initialization processing for performing variable argument referencing.

va_arg

Enables referencing of the argument following the argument currently being referenced for a function with variable arguments.

va_end

Terminates referencing of the arguments of a function with variable arguments.

va_copy <-lang=c99>

Copies variable arguments.

An example of a program using the macros defined by this standard include file is shown below.

[Format]

  1   #include <stdio.h>
  2   #include <stdarg.h>
  3
  4   extern void prlist(int count, ...);
  5
  6   void main( )
  7  {
  8      prlist(1, 1);
  9      prlist(3, 4, 5, 6);
 10      prlist(5, 1, 2, 3, 4, 5);
 11   }
 12
 13   void prlist(int count, ...)
 14  {
 15      va_list ap;
 16      int i;
 17
 18      va_start(ap, count);
 19      for(i=0; i<count; i++)
 20           printf("%d", va_arg(ap, int));
 21      putchar('\n');
 22      va_end(ap);
 23  }

 

Explanation:

This example implements function prlist, in which the number of data items to be output is specified in the first argument and that number of subsequent arguments are output.

In line 18, the variable argument reference is initialized by va_start. Each time an argument is output, the next argument is referenced by the va_arg macro (line 20). In the va_arg macro, the type name of the argument (in this case, int type) is specified in the second argument.

When argument referencing ends, the va_end macro is called (line 22).

 

va_start

Executes initialization processing for referencing variable arguments.

[Format]

#include <stdarg.h>
void va_start (va_list ap, parmN)

[Parameters]

ap Variable for accessing variable arguments

parmN Identifier of rightmost argument

[Remarks]

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

The argument parmN is the identifier of the rightmost argument in the argument list in the external function definition (the one just before the , ...).

To reference variable unnamed arguments, the va_start macro call must be executed first of all.

 

va_arg

Allows a reference to the argument following the argument currently being referred to in the function with variable arguments.

[Format]

#include <stdarg.h>
type va_arg (va_list ap, type);

[Parameters]

ap Variable for accessing variable arguments

type Type of arguments to be accessed

[Return values]

Argument value

[Remarks]

Specify a variable of the va_list type initialized by the va_start macro as the first argument. The value of ap is updated each time va_arg is used, and, as a result, a sequence of variable arguments is returned by sequential calls of this macro.

Specify the type to refer to as the second argument type.

The ap argument must be the same as the ap initialized by va_start.

It will not be possible to refer to arguments correctly if argument type is set to a type of which size is changed by type conversion when it is used as a function argument, i.e., if char type, unsigned char type, short type, unsigned short type, or float type is specified as type. If such a type is specified, correct operation is not guaranteed.

 

va_end

Terminates referencing of the arguments of a function with variable arguments.

[Format]

#include <stdarg.h>
void va_end (va_list ap);

[Parameters]

ap Variable for referencing variable arguments

[Remarks]

The ap argument must be the same as the ap initialized by va_start. If the va_end macro is not called before the return from a function, the operation of that function is not guaranteed.

 

va_copy

Makes a copy of the argument currently being referenced for a function with variable arguments.

[Format]

#include <stdarg.h>
void va_copy (va_list dest, va_list src);

[Parameters]

dest Copy of variable for referencing variable arguments

src Variable for referencing variable arguments

[Remarks]

A copy is made of the second argument src which is one of the variable arguments that have been initialized by the va_start macro and used by the va_arg macro, and the copy is saved in the first argument dest.

The src argument must be the same as the src initialized by va_start.

The dest argument can be used as an argument that indicates the variable arguments in the subsequent va_arg macros.