我正在尝试用 C 语言制作一个简单的加法计算器,以学习如何使用不定参数。以下编译正确,但输出总是不正确,我不知道如何调试它。任何指针都会很棒。
#include <stdio.h>
#include <stdarg.h>
int calculateTotal(int n, ...)
{
//declartion of a datatype that would hold all arguments
va_list arguments;
//starts iteration of arguments
va_start (arguments, n);
//declarion of initialization for 'for loop'
//declation of accumulator
int i = 0;
int localTotal = 0;
for(i; i < n; i++)
{
//va_arg allows access to an individual argument
int currentArgument = va_arg(arguments, int);
localTotal += currentArgument;
}
//freeing the declaration of the datatype that holds the information
va_end(arguments);
return localTotal;
}
int main()
{
int total = calculateTotal(56,7,8);
printf("Total > %d\n",total);
return 0;
}