C library macro - va_arg()



Description

The C library macro type va_arg(va_list ap, type) retrieves the next argument in the parameter list of the function with type. This does not determine whether the retrieved argument is the last argument passed to the function.

Declaration

Following is the declaration for va_arg() macro.

type va_arg(va_list ap, type)

Parameters

  • ap − This is the object of type va_list with information about the additional arguments and their retrieval state. This object should be initialized by an initial call to va_start before the first call to va_arg.

  • type − This is a type name. This type name is used as the type of the expression, this macro expands to.

Return Value

This macro returns the next additional argument as an expression of type type.

Example

The following example shows the usage of va_arg() macro.

#include <stdarg.h>
#include <stdio.h>

int sum(int, ...);

int main () {
   printf("Sum of 15 and 56 = %d\n",  sum(2, 15, 56) );
   return 0;
}

int sum(int num_args, ...) {
   int val = 0;
   va_list ap;
   int i;

   va_start(ap, num_args);
   for(i = 0; i < num_args; i++) {
      val += va_arg(ap, int);
   }
   va_end(ap);
 
   return val;
}

Let us compile and run the above program to produce the following result −

Sum of 15 and 56 = 71
stdarg_h.htm
Advertisements