0

I found here that function prototype is necessary before function call if the function is below function calling.

I checked this case in gcc compiler and it compile code without function prototype.

Example:

#include <stdio.h>

//int sum (int, int); -- it runs with this commented line

int main (void)
{
   int total;       
   total = sum (2, 3);
   printf ("Total is %d\n", total);        
   return 0;
}

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

Could explain somebody this issue?

BenMorel
  • 34,448
  • 50
  • 182
  • 322
Marcin
  • 422
  • 6
  • 17
  • 2
    Sounds like a C99 update – LeatherFace Mar 16 '14 at 18:58
  • I personally think the best way to avoid questions like this is to compile code by using "-Wpedantic -ansi" options. This way, your code would not even compile, as "//" is not recognized. :) – Markon Mar 16 '14 at 19:10

3 Answers3

3

It says on this page that: " You don't have to declare the function first, but if you don't, the C compiler will assume the function returns an int (even if the real function, defined later, doesn't)."

It is confirmed here.

As for arguments: "and nothing is assumed about its arguments."

Community
  • 1
  • 1
darxsys
  • 1,560
  • 4
  • 20
  • 34
  • 2
    It also takes any number of arguments. Correct me if I'm wrong. – aisbaa Mar 16 '14 at 19:02
  • Indeed, if i change ints to doubles in above code copiler says: main.c:22:5: error: conflicting types for ‘sum’ main.c:15:17: note: previous implicit declaration of ‘sum’ was here If I write double sum(double, double) or double sum() all is alright. – Marcin Mar 16 '14 at 19:08
  • The arguments in the function definition must have the same number that the function is called with, and each argument's type must match after the default argument promotions are performed, otherwise the behaviour is undefined. – M.M Mar 16 '14 at 20:45
3

When you don't provide a function prototype before it is called, the compiler assumes that the function is defined somewhere which will be linked against its definition during the linking phase. The default return type is assumed to be int and nothing is assumed about the function parameter. This is called implicit declaration. This means that the assumed function signature here is

int sum();

Here, the empty parameter list means the function sum takes a fixed but unknown number of arguments all of which are of unknown types. It is different from the function declaration

int sum(void);

However, in C++, the above two declarations are exactly the same. If your function has a return type other than int, then the assumed function signature won't match and this will result in compile error. In fact, it's an error in C99 and C11. You should always provide function prototype before it is called.

ajay
  • 9,402
  • 8
  • 44
  • 71
0

I changed your code to #include

/* int sum (int, int); -- it runs with this commented line */

int
main (void)
{
    int total;

    total = sum (2, 3);
    printf ("Total is %d\n", total);

    return 0;
}

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

Then compile with:

gcc --std=c90 -ansi -pedantic node.c

Also i try with some standard but may be it's related to gcc version and C standard.

PersianGulf
  • 2,845
  • 6
  • 47
  • 67