Variadic functions in C

Table of Contents
  • What are variadic functions?
  • Example of variadic functions in C
  • Implementing a variadic function
  • References

What are variadic functions?

Variadic functions are functions that take in a variable number of arguments. In C, variadic functions take a fixed argument and are denoted with an ellipsis (…) as the last parameter. Here’s the syntax for the function declaration:

int function_name(data_type variable_name, ...);

In order to access the passed arguments, we can use macros defined in the <stdarg.h> header file:

va_startAccess to variadic function arguments
va_argAccess the next variadic argument
va_copyMakes a copy of the variadic arguments
va_endEnds traversal of variadic arguments
va_listObject type that holds the information required by above macros

Example of variadic functions in C

Now, let’s take a look at one of the most commonly used variadic functions in C, printf. Printf is part of the C standard library <stdio.h> and is used to print formatted output to the standard output.

Here’s an example of how it can be used:

Output: The month is November and the year is 2023.

The printf function accepts a string with a variable number of format specifiers. The function iterates through the string to print to the stdout, and replaces format specifiers with the passed values of the month and year variables.

Implementing a variadic function

Now, let’s create our custom variadic function. First, we’ll declare a va_list variable to hold the arguments passed to the function.

va_list args;

We can use the va_start macro to initialize the argument list. va_start takes two arguments, the argument list and the last non-variadic argument.

va_start(args, last_arg);

Next, we can utilize va_arg to access the next argument. It takes two arguments; the argument list and variable type.

va_arg(args, int);

Finally, va_end can be used to clean up the va_list.

va_end(args);

Combining these macros, we can write a simple program in C that sums a variable number of integers.

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

int sum_ints(int count, ...) 
{
    va_list args;
    va_start(args, count);

    int sum = 0;
    for (int i = 0; i < count; i++) {
        sum += va_arg(args, int);
    }

    va_end(args);
    return sum;
}

int main(void) 
{
    int sum1 = sum_ints(4, 1, 2, 3, 4);
    printf("Sum: %d\n", sum1);

    int sum2 = sum_ints(3, 1, 2, 9);
    printf("Sum: %d\n", sum2);

    return 0;
}

Output:

Sum: 10
Sum: 12

In the above example, the first argument is the count of variadic arguments passed. This is indicated as the compiler doesn’t verify if a variadic function call receives an accurate number or type of arguments. Consequently, with custom variadic functions, passing invalid arguments would lead to undefined behavior.

Thank you for reading my summary of variadic functions. I hope it’ll be helpful for implementing your own variadic functions. Till next post!

References

One response to “Variadic functions in C”

  1. Veola Weaver Avatar

    It is perfect time to make some plans for the future and it’s time to be happy.

    I have read this post and if I could I wish to suggest you
    few interesting things or suggestions. Maybe you can write
    next articles referring to this article. I want to read more things about it!

Leave a Reply

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