Variadic Function in C Programming

Function with variable number of arguments

Looi Kian Seong
The Startup
4 min readJul 29, 2020

--

Figure: C functions for summation.

If you ever coded in C programming language, you might be wondering how the standard functions like printf() and scanf() can accept variable number of arguments in the function call. These functions which can accept variable number of arguments is known as variadic / varargs function.

You may come up with a need to code for a variadic function from time to time, so the standard library <stdarg.h> is here to help you in writing your own variadic function.

For example, as shown in the Figure above, if you need a function that can perform summation with n numbers, where n can be any number more than 1, you would not want to write multiple summation functions, instead you need a variadic function in this case.

Understanding the Macro Functions Defined in <stdarg.h>

There are three function-like macros that we need to understand before we can write our own variadic function:

  1. va_start(va_list pargs, last)
    This macro accepts two arguments.
    - The first argument is a variable declared as va_list type, which is an argument pointer variable.
    - The second argument is the last fixed argument accepted by the variadic function.

    This macro initialises the argument pointer variable pargs to point to the first optional argument accepted by the variadic function.
  2. va_arg(va_list pargs, type)
    This macro accepts two arguments.
    - This first argument is same as the first argument in va_start() macro.
    - The second argument specified the expected data type pointed by pargs.

    This function returns the value of the argument pointed by pargs, and also update the pargs to point to the next argument in the list.
  3. va_end(va_list pargs)
    This macro ends the use of pargs. According to this manual page, after calling va_end(pargs), further va_arg calls with pargs may not work. However, in GNU C library, va_end does nothing, so you may not necessary to use it except for portability reason.

Declaration of a Variadic Function

Variadic function prototype:
return_type func_name(type arg1, …)

The three periods after the last fixed argument is known as ellipsis. It indicates that a variable number of arguments might be accepted after the last fixed argument.

Examples:
void print(char *str, …);
int sum(unsigned int n, …);
double average(unsigned int n, …);
int formatStr(char *dest, const char *formatted_str, …)

Note: The examples below may not be the best practical application of variadic function as those are just for showing of the concept of variadic function only.

Example 1 — Averaging of a variable number of double-typed values

Output:

Example 2 — Create formatted string data

Output:

Conclusion

In summary, a variadic function is a function which can accept variable number of arguments like printf() and scanf() in C standard library. We can also define our own variadic function as we need by including the standard library <stdarg.h>. The library provides function-like macros as the routines to help us in writing a variadic function. va_list is a built-in variable type for declaring pointer to the optional argument. va_start macro is used in initialisation of the va_list variable to point to the first optional argument. va_arg macro is used to access the value of the optional argument pointed by the va_list pointer and update it to point to the next optional argument. By referring to both of the example codes above, you will get a clearer picture on the concept of variadic function.

Thank you for reading and hope that you gained knowledge from this post.

Feel free to comment if you find any part is confusing and I will try my best to help.

References

  1. https://www.gnu.org/software/libc/manual/html_node/Argument-Macros.html
  2. Horton, I. (2007). Chapter 9 More on Functions. In Beginning C: From Novice to Professional (pp. 345–347). Berkeley, CA: Apress.

--

--