How to pass variable number of arguments to C/C++ functions?

C/C++ allows to pass variable number of arguments to its functions through variable argument list (va_list). C/C++ provides a series of macros/functions va_start, va_arg, va_end to allow to pass variable number of arguments to its functions.

In this article we will discuss how to implement this in C/C++.

Step 1. We will prepare a function to take variable number of arguments. So, the function signature should be like this:

function_name(variable_type variable_name, …);

function_name is the name of the function. The functions first argument should be declared. variable_type is type of the first argument and variable_name will be the name of the first argument.

The second argument is an ellipse (three dots); which tells the compiler, through this function we are passing variable number of arguments.

Below is one example:

void varargs_fn(int num, …);

Step 2. Now we have to access these variable number of arguments within the function. In our function declaration we have used ellipse (three dots); which means we can pass any number of arguments. But we should know how to use within in the function.

C/C++ provides va_start, va_end and va_arg macros to handle these arguments. These macros expect an variable of type va_list.

So, our next step must be declare an identifier of type va_list.

Step 3. Create a variable of type va_list.

va_list valst;

Step 4. Start accessing the variable arguments. First call should be va_start macro to sets the first argument in variable arguments list. We should pass va_list type argument as first argument and second argument will be the first argument what we pass to the variable argument function.

Below is an example:

va_start(valst, num);

Step 5. Next we have to access the variable argument using va_arg macro. It will return the variable argument in the current position and the position points to the next argument in variable argument list. The first argument to this function should be va_list type argument which is initialized through its previous function call va_start. The second argument is the type of the argument (int, char, double, etc.,).

Below is the example:

int n = va_arg(valst, int);

Step 6. Once access all the arguments resets the variable argument list pointer to NULL. We should use va_end macro to do this. It should be called for every va_start macro.

All together below is the example. This example takes “10” int type arguments and print them on the console window. We are going to use first argument to tell number of arguments we are going to pass.

#include <stdarg.h>

//
//
void varargs_fn(int num, ...)
{
   va_list valst;

   va_start(valst, num);

   int n = 0;

   for ( int i = 0 ; i < num ; i++ )
   {
      n = va_arg(valst, int);

      printf("%d\n", n);
   }

   va_end(valst);
}

//
//
int main()
{
   varargs_fn(5, 2, 4, 6, 8, 10);

   return 0;
}

From our example; we are passing “5” as the number of arguments. And next we are passing 5 arguments. Once you run this application, it will display “5” integer values “2, 4, 6, 8, 10”.

// Malin

1 comment for “How to pass variable number of arguments to C/C++ functions?

Leave a Reply