tags:

views:

341

answers:

13
+9  Q: 

Basic c question

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

+7  A: 

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;
}
ruibm
@ruibm: _Also, you should prob add return 0 at the end of the main function since it's supposed to return int_ : **NO** thats optional
Prasoon Saurav
Yeah, I know it is, but it is a good programming practice and the compiler will prob issue warnings if you turn on pedantic mode.
ruibm
Further nitpicking, in C it's a "function", not a "method". Otherwise good.
Vatine
Lol, good point @vatine, updated reply.
ruibm
+5  A: 

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
}
sharptooth
+5  A: 

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.

S.Mark
+13  A: 

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
}
Prasoon Saurav
As a side note, aren't function declarations normally done in a .h file?
R. Bemrose
@R. Bemrose: when there's more than one source file, yes. When there's only one source file, as here, the function should be declared as a static function (since it does not need to be accessible from any other file, since there is only the one file to compile).
Jonathan Leffler
+2  A: 

See this similar question that was asked a few hours ago: http://stackoverflow.com/questions/2160250/type-redeclaration-while-running-in-c

jamesdlin
+1  A: 

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

Mark Tolonen
+2  A: 

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;
}
sergiom
The behavior is undefined. According to standards : You must declare the function before using.
Chankey Pathak
+2  A: 

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.

The MYYN
A: 

Sorry i m new to this community hence i am not allowed to comment, the addNumbers function is declared as returning int value but when the function call is made its made void.

whats up with that?? am i missing sumthing or did he(bob) miss that??

BK
Any undeclared functions get an implicit type of int func(int) (I think). So in your program it compiled down to addNumbers, but didn't know what the parameters are so it gave it int addNumbers(int) as a default so it could continue.
Robert
huh? i m sorry i dint get u????
BK
A: 

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

C Learner
A: 

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.

Vijay Sarathi
+1  A: 

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;
}
Srinivas Reddy Thatiparthy
A: 

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

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