Variable Length Agruments
Variable Length Agruments
It should be noted that the function func() has its last argument as ellipses, i.e. three
dotes (...) and the one just before the ellipses is always an int which will represent
the total number variable arguments passed. To use such functionality, you need to
make use of stdarg.h header file which provides the functions and macros to
implement the functionality of variable arguments and follow the given steps −
Define a function with its last parameter as ellipses and the one just before the ellipses is
always an int which will represent the number of arguments.
Create a va_list type variable in the function definition. This type is defined in stdarg.h
header file.
Use int parameter and va_start macro to initialize the va_list variable to an argument
list. The macro va_start is defined in stdarg.h header file.
Use va_arg macro and va_list variable to access each item in argument list.
Use a macro va_end to clean up the memory assigned to va_list variable.
Now let us follow the above steps and write down a simple function which can take
the variable number of parameters and return their average −
Example
#include <stdio.h>
#include <stdarg.h>
va_list valist;
int i;
}
return sum/num;
int main() {
return min;
3| Institute of Professional Computer Programming & Designing, Contact 95027 83007
Indian Bank Road, Veerabhdra Swamy temple Backside, Bhimavaram - 1, W. G Dist., A.P.
Vikram Computer Institute Storage Classes in C
LEARN HOW TO THINK & THINK HOW TO DEVELOP
// Driver code
int main()
{
int count = 5;
printf("Minimum value is %d", min(count, 12, 67, 6, 7, 100));
return 0;
}