CodeSteps

Python, C, C++, C#, PowerShell, Android, Visual C++, Java ...

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

C/C++ allows passing the variable number of arguments to its functions through a variable argument list (va_list). C/C++ provides a series of macros/functions va_start, va_arg, va_end to allow passing the 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 the 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 the 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 the 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 an ellipse (three dots); which means we can pass any number of arguments. But we should know how to use it within the function.

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

So, our next step must be to 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. The first call should be va_start macro to sets the first argument in the variable arguments list. We should pass the va_list type argument as the first argument and the second argument will be the first argument that 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 the va_arg macro. It will return the variable argument in the current position and the position points to the next argument in the variable argument list. The first argument to this function should be a va_list type argument which is initialized through its previous function called va_start. The second argument is the type of argument (int, char, double, etc.,).

Below is an 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 the 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 prints them on the console window. We are going to use the first argument to tell the 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

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

4 thoughts on “C Programming – How to pass variable number of arguments to C / C++ functions?

  1. Hello! Quick question that’s entirely off topic.
    Do you know how to make your site mobile friendly? My weblog looks weird when browsing from my iphone 4.
    I’m trying to find a template or plugin that might be able to fix this problem.
    If you have any recommendations, please share.
    Appreciate it!

Leave a Reply

Your email address will not be published. Required fields are marked *

Scroll to top
Exit mobile version