#include<stdio.h>
#include <stdarg.h>
 
/* The sum() function can accept variable number of arguments.
   In the function declaration ... means that the number and
   type of the arguments may vary. The marker ... can only
   appear at the end.
*/
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 );
  
  va_list args_copy; //initialize a copy of va_list to be used
  va_copy(args_copy, args); //copy args to args_copy
  
  for (i= 0; i< num_of_arguments; i++ )
     /* We add 5 to each element in variable argument list and
        print it.
     */
     printf("%d  ", va_arg (args_copy, int) + 5); 
     
  va_end(args_copy); //destruct args_copy
  
  printf("\n");
  
  /**** Next, we use args as in previous post ****/
    
  /* 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 (4,1,2,3,4);
  printf("result of sum() with 5 arguement: %d \n", result);
 
  return 0;
}