Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Implicit declaration of function 'sum' is invalid in C99

Tags:

c

xcode

I've been looking for a solution to this but haven't found anything that will help. I'm getting the following errors:

Implicit declaration of function 'sum' is invalid in C99
Implicit declaration of function 'average' is invalid in C99
Conflicting types for 'average'

Has anyone experienced this before? I'm trying to compile it in 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;
    }
like image 205
uplearned.com Avatar asked Dec 13 '12 23:12

uplearned.com


People also ask

What does implicit declaration of function is invalid in c99 mean?

implicit declaration of function means you do not have Prototypes for your functions. You should have a Prototype before the function is used. "call the block" I assume your Blocks are functions. 9/30/2020.

How do you fix an implicit function declaration?

Function name typo: Often the function name of the declaration does not exactly match the function name that is being called. For example, startBenchmark() is declared while StartBenchmark() is being called. I recommend to fix this by copy-&-pasting the function name from the declaration to wherever you call it.

Why is implicit declaration error in C?

Such an 'implicit declaration' is really an oversight or error by the programmer, because the C compiler needs to know about the types of the parameters and return value to correctly allocate them on the stack.

What is implicit declaration of function?

If a name appears in a program and is not explicitly declared, it is implicitly declared. The scope of an implicit declaration is determined as if the name were declared in a DECLARE statement immediately following the PROCEDURE statement of the external procedure in which the name is used.


2 Answers

You need to add declaration for these two functions, or move the two function definitions before main.

like image 62
Lei Mou Avatar answered Oct 25 '22 19:10

Lei Mou


The problem is that by the time the compiler sees the code where you use sum it doesn`t know of any symbol with that name. You can forward declare it to fix the problem.

int sum (int values[], int count);

Put that before main(). This way, when the compiler sees the first use of sum it knows that it exists and must be implemented somewhere else. If it isn't then it will give a liner error.

like image 45
imreal Avatar answered Oct 25 '22 20:10

imreal