我一直在寻找解决方案,但没有找到任何有帮助的东西。 我收到以下错误:
Implicit declaration of function 'sum' is invalid in C99
Implicit declaration of function 'average' is invalid in C99
Conflicting types for 'average'
有人以前经历过这种情况吗?我正在尝试在 Xcode 中编译它。
#import <Foundation/Foundation.h>
int main(int argc, const char * argv[])
{
@autoreleasepool
{
int wholeNumbers[5] = {2,3,5,7,9};
int theSum = sum (wholeNumbers, 5);
printf ("The sum is: %i ", theSum);
float fractionalNumbers[3] = {16.9, 7.86, 3.4};
float theAverage = average (fractionalNumbers, 3);
printf ("and the average is: %f \n", theAverage);
}
return 0;
}
int sum (int values[], int count)
{
int i;
int total = 0;
for ( i = 0; i < count; i++ ) {
// add each value in the array to the total.
total = total + values[i];
}
return total;
}
float average (float values[], int count )
{
int i;
float total = 0.0;
for ( i = 0; i < count; i++ ) {
// add each value in the array to the total.
total = total + values[i];
}
// calculate the average.
float average = (total / count);
return average;
}
最佳答案
您需要为这两个函数添加声明,或者将这两个函数定义移到 main 之前。
关于c - 函数 'sum' 的隐式声明在 C99 中无效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58399069/