Variadic functions in C

  • + 0 comments
    #include <stdarg.h>
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    
    #define MIN_ELEMENT 1
    #define MAX_ELEMENT 1000000
    int  sum (int count,...) {
         va_list args;
        va_start(args, count);
        int total = 0;
        for (int i = 0; i < count; i++) {
            total += va_arg(args, int);
        }
        va_end(args);
        return total;
    
    }
    
    int min(int count,...) {
        va_list args;
        va_start(args, count);
        int minimum = va_arg(args, int);
        for (int i = 1; i < count; i++) {
            int num = va_arg(args, int);
            if (num < minimum) {
                minimum = num;
            }
        }
        va_end(args);
        return minimum;
    }
    
    int max(int count,...) {
          va_list args;
        va_start(args, count);
        int maximum = va_arg(args, int);
        for (int i = 1; i < count; i++) {
            int num = va_arg(args, int);
            if (num > maximum) {
                maximum = num;
            }
        }
        va_end(args);
        return maximum;
    }