C library macro - va_end()



Description

The C library macro void va_end(va_list ap) 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.

Declaration

Following is the declaration for va_end() macro.

void va_end(va_list ap)

Parameters

  • ap − This is the va_list object previously initialized by va_start in the same function.

Return Value

This macro does not return any value.

Example

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

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

int mul(int, ...);

int main () {
   printf("15 * 12 = %d\n",  mul(2, 15, 12) );
   
   return 0;
}

int mul(int num_args, ...) {
   int val = 1;
   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 −

15 * 12 =  180
stdarg_h.htm
Advertisements