Previous Section2.9 signal.h  | 
| Table of Contents | Index | | 
Next Section
![]() 2.11 stddef.h  | 
The stdarg header defines several macros used to get the arguments in a function when the number of arguments is not known.
Macros:
     va_start();
     va_arg();
     va_end();
Variables:
     typedef va_list
     The va_list type is a type suitable for use in accessing the arguments of a function with
the stdarg macros.
     A function of variable arguments is defined with the ellipsis (,...) at the end of the
parameter list.
Declaration:
Initializes ap for use with thevoid va_start(va_listap,last_arg);
va_arg and va_end macros.  last_arg is the last known fixed
argument being passed to the function (the argument before the ellipsis).
     Note that va_start must be called before using va_arg and va_end.
Declaration:
typeExpands to the next argument in the paramater list of the function with type type. Note that ap must be initialized withva_arg(va_listap,type);
va_start.  If there is no next argument, then the result is
undefined.
Declaration:
void va_end(va_listap);
     Allows a function with variable arguments which used the va_start macro to return.  If
va_end is not called before returning from the function, the result is undefined.  The variable
argument list ap may no longer be used after a call to va_end without a call to va_start.
Example:
#include<stdarg.h>
#include<stdio.h>
void sum(char *, int, ...);
int main(void)
{
  sum("The sum of 10+15+13 is %d.\n",3,10,15,13);
  return 0;
}
void sum(char *string, int num_args, ...)
{
  int sum=0;
  va_list ap;
  int loop;
  va_start(ap,num_args);
  for(loop=0;loop<num_args;loop++)
    sum+=va_arg(ap,int);
  printf(string,sum);
  va_end(ap);
}
 
Previous Section2.9 signal.h  | 
| Table of Contents | Index | | 
Next Section
![]() 2.11 stddef.h  |