bob
bob

Reputation: 381

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()?

Upvotes: 17

Views: 54096

Answers (11)

Chankey Pathak
Chankey Pathak

Reputation: 21676

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

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

Upvotes: 1

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;
}

Upvotes: 1

Vijay
Vijay

Reputation: 67299

Since the compiler executes one line after another,by the time it sees the function call,it has to have the information about that function which the main function is calling.so my friend u need to tell the compiler about the function before you can ever use it.

Upvotes: 0

C Learner
C Learner

Reputation: 2337

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

Upvotes: 0

Prasoon Saurav
Prasoon Saurav

Reputation: 92922

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
}

Upvotes: 20

rui
rui

Reputation: 11284

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;
}

Upvotes: 7

miku
miku

Reputation: 188184

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.

Upvotes: 2

YOU
YOU

Reputation: 123917

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.

Upvotes: 3

sergiom
sergiom

Reputation: 4887

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;
}

Upvotes: 2

Mark Tolonen
Mark Tolonen

Reputation: 178334

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().

Upvotes: 1

sharptooth
sharptooth

Reputation: 170559

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
}

Upvotes: 6

Related Questions