C va_end()

The va_end() is a macro defined in the stdarg.h header file. This macro helps the function to use with variable arguments which is used to return from the va_start() macro. The result became undefined if the va_end is not called before returning from the function.


void va_end(va_list ap); #where ap is the object of va_list

 

va_end() Parameters: 

The va_end() function takes a single parameter. 

Parameter Description Required / Optional
ap the object of va_list initialized by va_start macro Required

va_end() Return Value

The macro va_end() does not return any value. This macro is used to terminate the use of the variable-length argument list pointed by 'ap'.


 

Examples of va_end()

Example 1: Working of va_end() function in C?


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

int getmul(int, ...);

int main () {
   printf("13 * 14 = %d\n",  getmul(2, 13, 14) );
   
   return 0;
}

int getmul(int num_args, ...) {
   int value = 1;
   va_list ap;
   int k;

   va_start(ap, num_args);
   for(k = 0; k < num_args; k++) {
      value *= va_arg(ap, int);
   }
   va_end(ap);
 
   return value;
}

Output:


13 * 14 =  182

Example 2: How va_end() works in C?


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

int getmul(int, ...);

int main () {
   printf("10 * 12 * 8 = %d\n",  getmul(3, 10, 12, 8) );
   
   return 0;
}

int getmul(int num_args, ...) {
   int value = 1;
   va_list ap;
   int k;

   va_start(ap, num_args);
   for(k = 0; k < num_args; k++) {
      value *= va_arg(ap, int);
   }
   va_end(ap);
 
   return value;
}

Output:


10 * 12 * 8 =  960