Variadic functions are one of the powerful but very rarely used features in C language. Variadic functions add a lot of flexibility in programming the application structure.
Variadic Function in C
A function that can take a variable number of arguments is called a variadic function. One fixed argument is required to define a variadic function.
The most frequently used library functions in C, i.e., printf() and scanf() are in fact the best-known examples of variadic functions, as we can put a variable number of arguments after the format specifier string.
printf("format specifier",arg1, arg2, arg3,...);
Syntax of Variadic Functions
In C, a variadic function is defined to have at least one fixed argument, and then followed by an ellipsis symbol (…) that enables the compiler to parse the variable number of arguments.
return_type function_name(type arg1,...);
You need to include the stdarg.h header file at the top of the code to handle the variable arguments. This header file provides the following macros to work with the arguments received by the ellipsis symbol.
Methods | Description |
---|---|
va_start(va_list ap, arg) | Arguments after the last fixed argument are stored in the va_list. |
va_arg(va_list ap, type) | Each time, the next argument in the variable list va_list and coverts it to the given type, till it reaches the end of list. |
va_copy(va_list dest, va_list src) | This creates a copy of the arguments in va_list |
va_end(va_list ap) | This ends the traversal of the variadic function arguments. As the end of va_list is reached, the list object is cleaned up. |
Examples of Variadic Functions in C
Example: Sum of Numbers Using a Variadic Function
The following code uses the concept of a variadic function to return the sum of a variable number of numeric arguments passed to such a function. The first argument to the addition() function is the count of the remaining arguments.
Inside the addition() function, we initialize the va_list variable as follows −
va_list args;va_start(args, n);
Since there are “n” number of arguments that follow, fetch the next argument with “va_arg()” macro for “n” number of times and perform cumulative addition −
for(i =0; i < n; i++){
sum +=va_arg(args,int);}
Finally, clean up the va_list variable −
va_end(args);
The function ends by returning the sum of all the arguments.
The complete code is given below −
#include <stdio.h>#include <stdarg.h>// Variadic function to add numbersintaddition(int n,...){
va_list args;int i, sum =0;va_start(args, n);for(i =0; i < n; i++){
sum +=va_arg(args,int);}va_end(args);return sum;}intmain(){printf("Sum = %d ",addition(5,1,2,3,4,5));return0;}
Output
Run the code and check its output −
Sum = 15
You can try providing a different set of numbers as the variadic arguments to the addition() function.
Example: Finding the Largest Number Using a Variadic Function
We can extend the concept of variadic function to find the largest number in a given list of variable number of values.
#include <stdio.h>#include <stdarg.h>// Variadic function to add numbersintlargest(int n,...){
va_list args;int i, max =0;va_start(args, n);for(i =0; i < n; i++){int x =va_arg(args,int);if(x >= max)
max = x;}va_end(args);return max;}intmain(){printf("Largest number in the list = %d ",largest(5,12,34,21,45,32));return0;}
Output
When you run this code, it will produce the following output −
Largest number in the list = 45
Example: Concatenation of Multiple Strings Using Variadic Function
In the following code, we pass multiple strings to a variadic function that returns a concatenated string.
#include <stdio.h>#include <string.h>#include <stdarg.h>char*concat(int n,...){
va_list args;int i;staticchar string[100],*word;va_start(args, n);strcpy(string,"");for(i =0; i < n; i++){
word=va_arg(args,char*);strcat(string, word);strcat(string," ");}va_end(args);return string;}intmain(){char* string1 =concat(2,"Hello","World");printf("%s\n", string1);char* string2 =concat(3,"How","are","you?");printf("%s\n", string2);return0;}
Output
Run the code and check its output −
Hello World
How are you?
Leave a Reply