I almost understood this function, but I wondered when I looked at the example. Why does the For statement loop until i is less than count?
int
add_em_up (int count,...)
{
va_list ap;
int i, sum;
va_start (ap, count); /* Initialize the argument list. */
sum = 0;
for (i = 0; i < count; i )
sum = va_arg (ap, int); /* Get the next argument value. */
va_end (ap); /* Clean up. */
return sum;
}
int
main (void)
{
/* This call prints 16. */
printf ("%d\n", add_em_up (3, 5, 5, 6));
/* This call prints 55. */
printf ("%d\n", add_em_up (10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10));
return 0;
}
I try to understand the concept by looking at various explanations, but it's not easy. I understand that the first parameter is the first argument of the list. It's the first parameter, so why do we have to loop around in here? But WHY? I want to know why! Help me plz!
CodePudding user response:
"Why does the For statement loop until i is less than count?"
It doesn't - it loops while i is less than count.
CodePudding user response:
Okay, this is such an easy question that experts like you must have been confused. I am implementing printf. For example, the following features are implemented:
int printf(const char *str, ...) //funtion
printf("my name : %s \n my age : %d \n", "baby coder", 1);
const char : "my name : %s \n my age : %d \n" Parameters : baby coder, 1
That's why spin the loop until less 'i' than the number of str...!