HackerRank C- Variadic functions in C
In this problem, you will implement three variadic functions named sum(), min() and max() to calculate sums, minima, maxima of a variable number of arguments. The first argument passed to the variadic function is the count of the number of arguments, which is followed by the arguments themselves.
- int sum (int count,...) {
- int sum=0;
- va_list values;
- va_start(values,count);
- for(int i=0;i<count;i++){
- sum+=va_arg(values,int);
- }
- va_end(values);
- return sum;
- }
-
- int min(int count,...) {
- int min=MAX_ELEMENT,test;
- va_list values;
- va_start(values,count);
- for(int i=0;i<count;i++){
- test=va_arg(values,int);
- if(min>test){
- min=test;
- }
- }
- va_end(values);
- return min;
- }
-
- int max(int count,...) {
- int max=MIN_ELEMENT,test;
- va_list values;
- va_start(values,count);
- for(int i=0;i<count;i++){
- test=va_arg(values,int);
- if(max<test){
- max=test;
- }
- }
- va_end(values);
- return max;
- }