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

// The sum() function can accept variable number of arguements.
int sum(int num_of_arguments, ... )
{
  /* A list to store the aruements. In example, this may contain
     1,2,3 and 4 elements on successive calls from main()
  */
  va_list args;   
  int sum= 0, i;

  /* Directing the va_list args initialized above to start storing
     all parameters folloowing the first parameter 'num_of_arguments'.
  */
  va_start (args, num_of_arguments );
  
  /* Loop until all parameters are seen. */
  for (i= 0; i< num_of_arguments; i++ ) 
    /* Extract the next value in argument list and add it to sum. */
    sum += va_arg (args,int); 

  /* Signal that we are done with our usage of the list */
  va_end (args);

  return sum;  /* Returns the calculated sum.*/
}
int main()
{
    int result;
    
    result=sum (1,1);
    printf("result of calling sum() with 2 arguement: %d \n", result);
    
    result=sum (2,1,2);
    printf("result of calling sum() with 3 arguement: %d \n", result);
    
    result=sum (3,1,2,3);
    printf("result of calling sum() with 4 arguement: %d \n", result);
    
    result=sum (4,1,2,3,4);
    printf("result of calling sum() with 5 arguement: %d \n", result);
    
    return 0;
}