fork download
  1. #include <stdio.h>
  2. #include <stdarg.h>
  3.  
  4. int calculateTotal(int n, ...)
  5. {
  6.  
  7. //declartion of a datatype that would hold all arguments
  8. va_list arguments;
  9.  
  10. //starts iteration of arguments
  11. va_start (arguments, n);
  12.  
  13. //declarion of initialization for 'for loop'
  14. //declation of accumulator
  15. int i = 0;
  16. int localTotal = 0;
  17.  
  18. for(i; i < n; i++)
  19. {
  20. //va_arg allows access to an individual argument
  21. int currentArgument = va_arg(arguments, int);
  22. localTotal += currentArgument;
  23. }
  24.  
  25. //freeing the declaration of the datatype that holds the information
  26. va_end(arguments);
  27.  
  28. return localTotal;
  29. }
  30.  
  31. int main()
  32. {
  33. int total = calculateTotal(2,7,8);
  34. printf("Total > %d\n",total);
  35.  
  36. return 0;
  37. }
Success #stdin #stdout 0.02s 1676KB
stdin
Standard input is empty
stdout
Total > 15