C++ Standard Library C++ STL Library

C++ <cstdarg> - va_end



The C++ <cstdarg> va_end macro performs cleanup for an instance of va_list type initialized by a call to va_start or va_copy.

If there is no corresponding call to va_start or va_copy, or if va_end is not called before a function that calls va_start or va_copy returns, the behavior is undefined.

Syntax

void va_end( va_list ap );               

Parameters

ap Specify an instance of the va_list type to clean up.

Return Value

None.

Example:

The example below shows the usage of va_end macro function.

#include <iostream>
#include <cstdarg>

using namespace std;

int add_nums(int count, ...) {
  int result = 0;
  va_list args;

  va_start(args, count);
  for (int i = 0; i < count; ++i) {
    result += va_arg(args, int);
  }
  va_end(args);
  return result;
}

int main () {
  cout<<"Sum is: "<<add_nums (2, 10, 20)<<"\n";
  cout<<"Sum is: "<<add_nums (3, 10, 20, 30)<<"\n";
  cout<<"Sum is: "<<add_nums (4, 10, 20, 30, 40)<<"\n";
  return 0;
}

The output of the above code will be:

Sum is: 30
Sum is: 60
Sum is: 100

❮ C++ <cstdarg> Library