Table of Contents
Variadic Functions In C
Variadic functions in C are like flexible functions because they can accept different numbers of arguments. They always have at least one fixed argument, and then you can pass any number of additional arguments. To indicate this flexibility, variadic functions have an ellipsis (…) as their last parameter.
Syntax
int function_name(data_type variable_name, ...);
Values of the passed arguments can be accessed through the header file named as:
#include <stdarg.h>
<stdarg.h> includes the following methods:
Methods | Description |
va_start(va_list ap, argN) | This method will grant access to variadic function arguments. Whereas, *va_list* will be the pointer to the last fixed argument in the variadic function* and, it is the last fixed argument in the variadic function. Hence, From the above variadic function (function_name (data_type variable_name, …);) provided, We can conclude that variable_name is the last fixed argument making it the argN. *va_list ap* will be a pointer to argN (variable_name) |
va_arg(va_list ap, type) | In this method, it will give access to the next variadic function argument. Also, *va_list ap* is the same as above i.e. a pointer to argN*type*, and it refers to the data type the *va_list ap* should expect (double, float, int, etc.) |
va_copy(va_list dest, va_list src) | This method can create a copy of the variadic function arguments. |
va_end(va_list ap) | This indicates the passing of the variadic function arguments. |
Note
va_list holds the information needed by va_start, va_arg, va_end, and va_copy
Program 1: C program to illustrate the working of variadic function
// C program for the above approach
#include <stdarg.h>
#include <stdio.h>
// Variadic function to add numbers
int AddNumbers(int n, ...)
{
int Sum = 0;
// Declaring pointer to the
// argument list
va_list ptr;
// Initializing argument to the
// list pointer
va_start(ptr, n);
for (int i = 0; i < n; i++)
// Accessing current variable
// and pointing to next one
Sum += va_arg(ptr, int);
// Ending argument list traversal
va_end(ptr);
return Sum;
}
// Driver Code
int main()
{
printf("\n\n Variadic functions: \n");
// Variable number of arguments
printf("\n 1 + 2 = %d ",
AddNumbers(2, 1, 2));
printf("\n 3 + 4 + 5 = %d ",
AddNumbers(3, 3, 4, 5));
printf("\n 6 + 7 + 8 + 9 = %d ",
AddNumbers(4, 6, 7, 8, 9));
printf("\n");
return 0;
}
Output
Variadic functions:
1 + 2 = 3
3 + 4 + 5 = 12
6 + 7 + 8 + 9 = 30
Program 2: C program consisting of the variadic function LargestNumber()
// C program for the above approach
#include <stdarg.h>
#include <stdio.h>
// Variadic function to find the largest number
int LargestNumber(int n, ...)
{
// Declaring pointer to the
// argument list
va_list ptr;
// Initializing argument to the
// list pointer
va_start(ptr, n);
int max = va_arg(ptr, int);
for (int i = 0; i < n-1; i++) {
// Accessing current variable
// and pointing to next
int temp = va_arg(ptr, int);
max = temp > max ? temp : max;
}
// End of argument list traversal
va_end(ptr);
return max;
}
// Driver Code
int main()
{
printf("\n\n Variadic functions: \n");
// Variable number of arguments
printf("\n %d ",
LargestNumber(2, 1, 2));
printf("\n %d ",
LargestNumber(3, 3, 4, 5));
printf("\n %d ",
LargestNumber(4, 6, 7, 8, 9));
printf("\n");
return 0;
}
Output
Variadic functions:
2
5
9
FAQ- Variadic Functions In C
Q1. What is an example of a variadic function?
Ans. A variadic function is a function that can accept a variable number of arguments. A well-known example is the printf()
function from the <stdio.h>
header in C. When you declare a variadic function, you use ellipses (…) as the last parameter to indicate that it can accept different numbers of arguments.
Q2. How to access variadic arguments in C?
Ans. To work with variadic functions in C, you use a set of special macros provided by the <stdarg.h>
header file. These macros include va_list
, va_start
, va_arg
, and va_end
. They help programmers access and work with the variable arguments that are passed to the function.
Q3. What is the structure of a Va_list?
Ans. The va_list
type in C is used to handle variable arguments in a function, but its internal structure is compiler-dependent and not necessarily an array. It’s best to use the provided macros va_arg
for safe and portable access to variable arguments.
Hello, I’m Hridhya Manoj. I’m passionate about technology and its ever-evolving landscape. With a deep love for writing and a curious mind, I enjoy translating complex concepts into understandable, engaging content. Let’s explore the world of tech together