Passing number of Variable Arguments in function
#include <stdio.h>
#include <stdarg.h>
int sum(int num, ...); /* Function which takes n number of arguments */
int main(void){
int s1;
s1 = sum(4, 100,200,300,400);/* Lets put some numbers to add up */
s2 = sum(6,6,6,6,6); /* which can be changed on each function */
/* call, but calling the same function */
printf("The sum S1 is: %d\n",s1);
printf("The sum S2 is: %d\n",s2);
return 0;
}
int sum(int num,...){
int S = 0, i;
va_list L; /* 'va_list' - structure defined in 'stdarg.h' header file */
va_start(L,num); /* 'va_start' -function taking object 'L'(of Structure
va_list) and 'num' -total number of arguments
passed when 'sum' function is called */
/* Object 'L' will hold all the values when sum is
called */
for(i=0;i<num;i++){
S += va_arg(L, int); /* va_arg will convert current value is L into
'int' (function argument) and return an 'int' */
}
va_end(L); /* destructing Object 'L' */
return S; /* returning 'S' to command line */
}
#include <stdio.h>
#include <stdarg.h>
int sum(int num, ...); /* Function which takes n number of arguments */
int main(void){
int s1;
s1 = sum(4, 100,200,300,400);/* Lets put some numbers to add up */
s2 = sum(6,6,6,6,6); /* which can be changed on each function */
/* call, but calling the same function */
printf("The sum S1 is: %d\n",s1);
printf("The sum S2 is: %d\n",s2);
return 0;
}
int sum(int num,...){
int S = 0, i;
va_list L; /* 'va_list' - structure defined in 'stdarg.h' header file */
va_start(L,num); /* 'va_start' -function taking object 'L'(of Structure
va_list) and 'num' -total number of arguments
passed when 'sum' function is called */
/* Object 'L' will hold all the values when sum is
called */
for(i=0;i<num;i++){
S += va_arg(L, int); /* va_arg will convert current value is L into
'int' (function argument) and return an 'int' */
}
va_end(L); /* destructing Object 'L' */
return S; /* returning 'S' to command line */
}