# Functions With Variable number of Arguements?

Here is the example code for giving us the idea of function with variable no. of arg, Plz sought out my following Questions...

Q: Why va_list, va_start, va_arg are used?...what is its function?
Q: ->When we pass 7 arguments in the findmax() it doesn't give result in according to 7 arg. but only 6 arg.
->When we pass less than 6 arg in the same functions then, it results an uexpected output.
Why so?
#include
#include
#include

main()
{
int max;
int findmax(int,...);

max = findmax(5,23,15,1,92,50);
printf("
Maximum is: %d",max);
}

int findmax(int tn,...)
{
int max, count, num;
va_list ptr;
va_start(ptr,tn);
max = va_arg(ptr,int);

for(count =1; count < tn; count++)
{
num = va_arg(ptr,int);
if(num > max)
max = num;
}

return(max);
}

• Note that the first arg passed is the number of args following. You have set this to 5, so the function expects exactly 5 args.

You also need to finish the function with

va_end(ptr);
• : Here is the example code for giving us the idea of function with
: variable no. of arg, Plz sought out my following Questions...
:
: Q: Why va_list, va_start, va_arg are used?...what is its function?
: Q: ->When we pass 7 arguments in the findmax() it doesn't give
: result in according to 7 arg. but only 6 arg.
: ->When we pass less than 6 arg in the same functions then, it
: results an uexpected output.
: Why so?
: #include
: #include
: #include
:
: main()
: {
: int max;
: int findmax(int,...);
:
: max = findmax(5,23,15,1,92,50);
: printf("
Maximum is: %d",max);
: }
:
: int findmax(int tn,...)
: {
: int max, count, num;
: va_list ptr;
: va_start(ptr,tn);
: max = va_arg(ptr,int);
:
: for(count =1; count < tn; count++)
: {
: num = va_arg(ptr,int);
: if(num > max)
: max = num;
: }
:
: return(max);
: }
: Those function are used because the amount of input argument is unknown.
: Here max = findmax(5,23,15,1,92,50); the first argument,5,tells the function :findmax that you want to compare the following 5 intergers and get the : largest,namely, for 23,15,1,92,50; you'll get 92.