Functions with a variable argument list. - C Function

C examples for Function:Variable Length Argument

Description

Functions with a variable argument list.

Demo Code

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

float average(int num, ...);

int main( void )
{
    float x;// www  .  j  ava2 s. c  o m

    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, ...)
{
    va_list arg_ptr;
    int count, total = 0;

    va_start(arg_ptr, num);

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

    va_end(arg_ptr);

    return ((float)total/num);
}

Related Tutorials