Latest web development tutorials

C library macro - va_arg ()

C standard library - <stdarg.h> C standard library - <stdarg.h>

description

C library macrotype va_arg (va_list ap, type) next parameter retrieval function parameter list oftypetype.It can not determine whether the retrieved parameter is the last parameter to the function.

statement

Here is the va_arg () macro statement.

type va_arg(va_list ap, type)

parameter

  • ap - it is an object of type va_list,stores information about additional parameters and retrieve state. This object should be before the first call to va_arg initialized by calling va_start.
  • type - This is a type name.The type name as a type extension from the macro expression to use.

return value

This macro returns the next extra parameter type is a type of expression.

Examples

The following example demonstrates the va_arg () macro usage.

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

int sum(int, ...);

int main()
{
   printf("15 和 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's compile and run the above program, which will result in the following:

15 和 56 的和 = 71

C standard library - <stdarg.h> C standard library - <stdarg.h>