In Visual Studio 2012, when I run the following code, which just calculates the average of numbers, gives an erroneous answer. The for
loop - for some reason - starts on Number = 1
and goes one entry beyond Number = 9
.
#include <stdio.h>
#include <stdarg.h>
float avg(int Count, ...)
{
va_list Numbers;
va_start(Numbers, Count);
int Sum = 0;
for(int i = 0; i < Count; ++i)
{
int entry = va_arg(Numbers, int);
printf("%d %d\n", i, entry);
Sum += entry;
}
va_end(Numbers);
float av = Sum/Count;
return(av);
}
int main()
{
float Average = avg(10, 1, 2, 3, 4, 5, 6, 7, 8, 9);
printf("The average of the first 10 whole numbers: %f\n", Average);
}
I checked e.g. Passing variable number of arguments around but I can't figure out why va_arg
pulls the second entry from Numbers
first. The result of this code on my machine is:
0 1
1 2
2 3
3 4
4 5
5 6
6 7
7 8
8 9
9 790803
The average of the first 10 whole numbers: 79084.000000