---
title: "linux C语言系列--第五讲--函数"
author: "8366"
date: "2011-06-22T08:09:25Z"
tags: []
categories: ["编程语言"]
slug: "linux-c"
draft: false
lastmod: "2011-06-22T22:33:23Z"
id: 1100409
url: "https://8366.iteye.com/blog/1100409"
---

**可变参数列表：**

 

  例子：写一个函数，求给定所有参数的平均值，函数的参数的个数是不定的

 

```
[root@xhu-vm test]# more test6.c 
#include <stdio.h>
#include <stdarg.h>

float average(int n_values,...)
{
        va_list var_arg;
        int count;
        float sum=0;

        va_start(var_arg,n_values);

        for(count=0;count<n_values;count++)
        {
                sum+=va_arg(var_arg,int);
        }

        va_end(var_arg);
        return sum/n_values;
}

int main()
{
        printf("%f\n",average(4,1,2,3,4));
}

[root@xhu-vm test]# 
```

 编译运行:

 

```
[root@xhu-vm test]# gcc -g test6.c -o test6
[root@xhu-vm test]# ./test6
2.500000
[root@xhu-vm test]# 
```