Array of parameters

Functions

#include <stdarg.h>

//the list of arguments does not know its length:
//the count comes first
double getAvg(int count, ...) {
    if (count == 0) {
        return 0;
    }

    va_list args;
    va_start(args, count);
    double sum = 0;
    for (int i = 0; i < count; i++) {
        sum += va_arg(args, double);
    }
    va_end(args);
    return sum / count;
}

//the types are not checked: 1.0, not 1
double avg = getAvg(4, 1.0, 2.0, 3.0, 4.0);
//avg is 2.5

printf("avg is %g\n", avg);