C Variable Arguments Function Tutorial

In this section, we will learn what variable argument is and how it works in C.

What is Variable Arguments Function in C?

Variable-argument function is a type of function that can take a random number of arguments.

For example, at one call we can put 3 arguments in it and the next call we can decide to put another number of arguments like 10 arguments for this type of function.

Basically, there’s no limit on how many arguments we can set for such a type of function (hence variable-argument function).

C Variable Arguments Function Syntax:

data-type functionName(arg, ...){/*...*/}

In order to create this type of function, the rightmost parameter of the function should be declared as ellipses (three dots `…`) and another parameter just before ellipses in order to declare the number of arguments that we want to set for the function.

Notes:

  • C-compilers are not smart to count the number of new arguments to a function. So we need to declare how many more arguments we’re about to put in the function.
  • Other than these two parameters, we’re allowed to set other parameters as well, but those are optional and they should be declared before these two parameters.

Example: creating variable arguments function in C

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

void variableArg(char * st, int n, ...);
int main() {

    variableArg("Test",3, 1,2,3);
    return 0;
}
void variableArg(char* st, int n , ...){
    
    printf("This is the value of st: %s\n",st);
}

Output:

This is the value of st: Test

In this example, we’ve created a variable-argument function named `variableArg`. The first parameter of this function is a pointer to `char`, the second parameter which happened to be the one before the ellipses, is of type integer and for its value we should set the number of new arguments we’re going to put in the function when calling it.

Then we have the last parameter which, as mentioned before, should be ellipses.

Next within the body of the `main()` function we called the `variableArg` and as its first argument we set “Test” and then for the second argument we set the number of new arguments we want to put to this function which for this example the value is 3 meaning there’s going to be 3 arguments from here onward.

Now we can put in 3 more arguments.

For now, we didn’t use the new arguments of this function, but in the section below, you’ll see how to use the new arguments within the body of a variable-argument function:

C va_list Macro

We need to create a variable of type `va_list`. This variable holds the list of new arguments of the function.

C va_start Macro

There’s a function-like macro named `va_start` which takes two arguments: 1- the name of the variable of type `va_list` that we just created, and 2- the number of new arguments (the value set for the parameter before ellipses).

Note: when we call this macro, it will fill the variable of type `va_list` with the arguments we set to the function (those that replaced the ellipses).

C va_arg Macro

Now that we have the new arguments of the function in the variable of type `va_list`, we can access them by call to another function-like macro named `va_arg`. This function also takes two arguments: 1- the name of the variable of type `va_list` that we created in the function. 2- the type of the new arguments (Because in replacement of ellipses we can put any type of value, like `int`, `double`, `float` etc. we need to declare the type of arguments we put for the function).

Note: the first time we call this macro, we will get the first argument of the function (of those arguments that were put in replacement of ellipses), and the next call to the macro will give us the next argument and so on.

C va_end Macro

Finally, after we’ve read all the new arguments of the function, we need to clean and release the memory allocated to the variable of type `va_list`. This is done via the call to another function-like macro called `va_end`. This macro only takes one argument and that is the variable of type `va_list` that we’ve created in the function.

Note: All these macros are in `stdarg.h` header file and so we need to include the file in order to use these macros.

Example: variable arguments function in C

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

void variableArg(char * st, int n, ...);
int main() {

    variableArg("Test",3, 1,2,3);
    return 0;
}
void variableArg(char* st, int n , ...){

    va_list list;

    va_start(list, n);

    for (int i = 0 ; i<n; i++){
        printf("%d, ",va_arg(list, int));
    }
    printf("\nThis is the value of st: %s\n",st);

    va_end(list);
}

Output:

1, 2, 3,

This is the value of st: Test
Facebook
Twitter
Pinterest
LinkedIn

Top Technologies