Variadic Functions in C


The implementation of variadic functions in the C programming language

Variadic Functions in C Variadic Functions,C Programming
Photo by Shahadat Rahman on Unsplash

Lately, I have been using variadic functions in the C programming language. Even though I use them constantly, I find myself forgetting how to use them. That is why I have created a reference article for myself so that I don’t have to search the details of variadic functions on the web constantly.

Variadic Functions

We typically use the variadic function when we don’t know the total number of arguments that will be used for a function. Basically, one single function could potentially have n number of arguments.[1]

For example, in the printf function when you want to print one number, we do something like this.

printf(" the one number = %d", nOneNumber);

When you want to print two numbers, we still use the same printf function as shown below:

printf(" the first number = %d, the second number =%d ", nOneNumber, nSecondNumber);

In order to use variadic functions we need to understand these macros, All these macros can be found in stdarg.h library.

va_list -> access optional parameters
va_start -> va_start will connect our argument list
va_end -> stop using arguments or reset our list position
va_copy -> situations for which we need to save our current location, like book marker [2]

Structure of Variadic Functions

A variadic function has two parts:

  1. mandatory
  2. optional

At least one mandatory argument is required. The order is important in this case. So, you will have mandatory arguments first and then you will have optional arguments.

Common practice is to have some number that will tell us how many arguments there are or we look for a stopping sign in our optional list. [3]

Variadic Function Example

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

int process(int param, va_list var_args)
{
    printf("\nThe First Param : %d", param);
    
    char* name = va_arg(var_args, char*);
    if(NULL != name)
        printf(" \nThe String Param I Expect : %s", name);
    else
        printf("\nYou have not sent the expected second param.s ...");
        
    return 0;
}

int my_variadic_function(int first_param, ...)
{
    int ret;
    va_list variadic_arguments;
    
    va_start(variadic_arguments, first_param);

    ret = process(first_param, variadic_arguments);

    va_end(variadic_arguments);
    
    return ret;
}



int main()
{
    my_variadic_function(22, "Mustafa");
    
    // Since the implementation expect 2 arguments, 
    // it fails if less than expected is provided
    
    // Uncomment the line below to get error.
    // my_variadic_function(22);               // Segmentation fault (core dumped)
    
    return 0;
}

References

[1] https://www.thegeekstuff.com/2017/05/c-variadic-functions/

[2] https://stackoverflow.com/questions/15659859/how-can-i-get-my-va-list-arguments-to-repeat-itself

[3] https://medium.com/swlh/variadic-functions-3419c287a0d2


Leave a Reply

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