c 可变参数demo

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdarg.h>

int average(int num, ...);

int main() {
    int averageRes = average(3, 1, 3, 5);
    printf("average:%d\n", averageRes);

    return 0;
}


int average(int num, ...) {
    va_list list;
    int sum = 0;
    va_start(list, num);
    for (int i = 0; i < num; i++) {
        sum += va_arg(list, int);
    }
    return sum / num;
}

 

posted on 2020-03-03 00:25  luckygxf  阅读(110)  评论(0编辑  收藏  举报

导航