Xcode - Warning: Implicit declaration of function is invalid in C99

CXcode

C Problem Overview


Getting a warning : Implicit declaration of function 'Fibonacci' is invalid in C99. What's wrong?

#include <stdio.h>

int main(int argc, const char * argv[])
{
    int input;
    printf("Please give me a number : ");
    scanf("%d", &input);
    getchar();
    printf("The fibonacci number of %d is : %d", input, Fibonacci(input)); //!!!
    
}/* main */

int Fibonacci(int number)
{
    if(number<=1){
        return number;
    }else{
        int F = 0;
        int VV = 0;
        int V = 1;
        for (int I=2; I<=getal; I++) {
            F = VV+V;
            VV = V;
            V = F;
        }
        return F;
    }
}/*Fibonacci*/

C Solutions


Solution 1 - C

The function has to be declared before it's getting called. This could be done in various ways:

  • Write down the prototype in a header
    Use this if the function shall be callable from several source files. Just write your prototype
    int Fibonacci(int number);
    down in a .h file (e.g. myfunctions.h) and then #include "myfunctions.h" in the C code.

  • Move the function before it's getting called the first time
    This means, write down the function
    int Fibonacci(int number){..}
    before your main() function

  • Explicitly declare the function before it's getting called the first time
    This is the combination of the above flavors: type the prototype of the function in the C file before your main() function

As an additional note: if the function int Fibonacci(int number) shall only be used in the file where it's implemented, it shall be declared static, so that it's only visible in that translation unit.

Solution 2 - C

The compiler wants to know the function before it can use it

just declare the function before you call it

#include <stdio.h>

int Fibonacci(int number); //now the compiler knows, what the signature looks like. this is all it needs for now

int main(int argc, const char * argv[])
{
    int input;
    printf("Please give me a number : ");
    scanf("%d", &input);
    getchar();
    printf("The fibonacci number of %d is : %d", input, Fibonacci(input)); //!!!

}/* main */

int Fibonacci(int number)
{
//…

Solution 3 - C

in c The function has to be declared before it's getting called

include head file

Solution 4 - C

I have the same warning (it's make my app cannot build). When I add C function in Objective-C's .m file, But forgot to declared it at .h file.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionCodingUpStreamView Question on Stackoverflow
Solution 1 - CeckesView Answer on Stackoverflow
Solution 2 - CvikingosegundoView Answer on Stackoverflow
Solution 3 - C王传义View Answer on Stackoverflow
Solution 4 - CleeView Answer on Stackoverflow