简体   繁体   中英

What does “implicit declaration of function” mean?

#include <stdio.h>

int main()
{
    int a = 4;
    int b = 3;
    addNumbers(a, b);
}

int addNumbers(int a, int b)
{
    return a + b;
}

Why does this not compile, I get a message saying implicit declaration of function addNumbers() ?

Either define the function before main() or provide its prototype before main() .

So either do this:

#include <stdio.h>

int addNumbers(int a, int b)
{ //definition
}

int main()
{ //Code in main
  addNumbers(a, b);
}

or this:

#include <stdio.h>

int addNumbers(int, int);
int main()
{ //Code in main
  addNumbers(a, b);
}

int addNumbers(int a, int b)
{ // definition
}

You need to declare the function before you call it in main(). Either move it before main or at least declare it there. Also, you should prob add return 0 at the end of the main function since it's supposed to return int .

#include <stdio.h>

int addNumbers(int a, int b)
{
    return a + b;
}

int main()
{
    int a = 4;
    int b = 3;
    addNumbers(a, b);
    return 0;
}

You have to either move the entire addNumber() function above main or provide a prototype. The latter is done the following way:

int addNumbers(int a, int b);

int main()
{
//    code of main() here
}

int addNumbers(int a, int b)
{
//code of addNumbers() here
}

Put addNumbers before main

int addNumbers(int a, int b)
{
    return a + b;
}

int main()
{
    int a = 4;
    int b = 3;

    addNumbers(a, b);
}

UPDATE :

To print it, printf("%i",addNumbers(a, b)); will display 7 in above case.

You can move the whole function above the point where it is called, or use a function prototype, like this:

#include <stdio.h>

int addNumbers(int a, int b); // function prototype

int main()
{
    int a = 4;
    int b = 3;
    addNumbers(a, b);
}

int addNumbers(int a, int b)
{
    return a + b;
}

You'll need a forward declaration of the addNumbers function or its definition moved up before the first usage:

// 2161304
#include <stdio.h>

// forward declaration
int addNumbers(int a, int b);

int main()
{
    int a = 4;
    int b = 3;
    addNumbers(a, b);
}

// alternatively move this up before main ...
int addNumbers(int a, int b)
{
    return a + b;
}

Regarding main and return :

Programs will compile without. The signatures of main defined by the standard are:

int main(void)
int main(int argc, char **argv)

There are three portable return values:

0, EXIT_SUCCESS, EXIT_FAILURE

which are defined in stdlib.h .

Declare the function before using it by either adding a prototype before main():

int addNumbers(int a, int b);

or move the whole addNumbers function before main().

I agree with declaration and definition thing but i am not getting any compilation errors with the above code.My gcc version is "4.4.1-4ubuntu9".Any ideas.

I have done a little modification to test the code.

 #include <stdio.h>

int main()
{
    int a = 4;
    int b = 3;
    printf("%d", addNumbers(a, b));   // Printf added.
}

int addNumbers(int a, int b)
{
    return a + b;
}

You must declare the function before using. Remember these 4 basic parts while dealing with functions.

  1. Declaration
  2. Call
  3. Definition
  4. Return

if your compiler is C99 standard it throws and error "implicit declaration", since since default promotion is obsolete in C99 standard. if you try to compile with C89 standard this would be allowable.

In C99 standard function prototype is necessary

由于编译器一个接一个地执行一行,当它看到函数调用时,它必须具有关于该函数的信息,主函数正在调用。所以我的朋友你需要先告诉编译器有关函数的信息。用它。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM