#include<stdio.h>
#include<ctype.h>
#include<math.h>
#include<malloc.h>
#include<stdarg.h>

/* functions with variable number of arguments should have at least one fixed argument. 
Otherwise, there is no way to access other arguments. The last argument is "...". */


/* this function will take the number arguments followed by the actual arguments */
double maximum(int num_arg, ...)
{   
    
    va_list arg_values; /* list of variable arguments */                    
    int i;
    double max, temp;

    /* This initializes the variable arguments. It takes the list of variable arguments and the last formal function argument as arguments. 
    You can not access the variable arguments before this initilization. */
    
    va_start(arg_values, num_arg);  
    
    /* The first variable argument (after formal argument num_arg) is accessed and set to max. Double is the data type of argument expected. */  
    max = va_arg(arg_values, double);        
    /* finding the maximum: it is assumed that the function caller will specify the number of arguments */
    
    for (i = 1; i < num_arg; i++)        
    {
        temp = va_arg(arg_values, double); /* the next argument */
        if(max < temp)
            max = temp; 
    }
    va_end(arg_values); // cleaning the list

    return (max);                      
}

int main()
{
    /* finds the maximum of 10.0 and 20.0 (2 specifies that the number of arguments is 2) */
    printf("%f\n", maximum(2, 10.0, 20.0));
    /* finds the maximum of 10.0, 5.0, 50.0 (3 specified that the number of arguments is 3) */
    printf("%f\n", maximum(3, 10.0, 5.0, 50.0));

return(0);
}
