C supports variable numbers of arguments. But there is no language provided way for finding out total number of arguments passed. User has to handle this in one of the following ways:
1) By passing first argument as count of arguments.
2) By passing last argument as NULL (or 0).
3) Using some printf (or scanf) like mechanism where first argument has placeholders for rest of the arguments.
Following is an example that uses first argument arg_count to hold count of other arguments.
#include <stdarg.h>
#include <stdio.h>
int min( int arg_count, ...)
{
int i;
int min, a;
va_list ap;
va_start (ap, arg_count);
min = va_arg (ap, int );
for (i = 2; i <= arg_count; i++) {
if ((a = va_arg (ap, int )) < min)
min = a;
}
va_end (ap);
return min;
}
int main()
{
int count = 5;
printf ( "Minimum value is %d" , min(count, 12, 67, 6, 7, 100));
getchar ();
return 0;
}
|
Output:
Minimum value is 6
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!
Last Updated :
28 May, 2017
Like Article
Save Article