Consider the following code:
#include<stdio.h>
#include<stdarg.h>
int sum(int, ...);
int main()
{
int a;
a=sum(10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
printf("the sum is %d", a);
return 0;
}
int sum(int a, ...)
{
int s=0, i=1;
va_list ls;
va_start(ls, a);
while(i<=a)
{
s=s+va_arg(ls, int);
i++;
}
va_end(ls);
return s;
}
The above code computes the sum of first 10 natural numbers, by using the variable arguments, by sum function. a the first argument of sum, it is specified that the user is giving 10 natural numbers whose sum is needed but I want some way so that we can find the sum without giving the count of the numbers I'm going to add. I means
sum(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
will calculate the sum of the first 10 natural numbers which is 55. Please find a way and tell me