⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 vary.c

📁 c21Examples.rar
💻 C
字号:
/* Functions with a variable argument list. */

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

float average(int num, ...);

int main( void )
{
    float x;

    x = average(10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
    printf("\nThe first average is %f.", x);
    x = average(5, 121, 206, 76, 31, 5);
    printf("\nThe second average is %f.\n", x);
    return 0;
}

float average(int num, ...)
{
    /* Declare a variable of type va_list. */

    va_list arg_ptr;
    int count, total = 0;

    /* Initialize the argument pointer. */

    va_start(arg_ptr, num);

    /* Retrieve each argument in the variable list. */

    for (count = 0; count < num; count++)
        total += va_arg( arg_ptr, int );

    /* Perform clean up. */

    va_end(arg_ptr);

    /* Divide the total by the number of values to get the */
    /* average. Cast the total to type float so the value */
    /* returned is type float. */

    return ((float)total/num);
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -