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?

2
  • 2
    Sounds like a C99 update Commented Mar 16, 2014 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. :) Commented Mar 16, 2014 at 19:10

3 Answers 3

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.

Sign up to request clarification or add additional context in comments.

Comments

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."

3 Comments

It also takes any number of arguments. Correct me if I'm wrong.
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.
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.
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.

1 Comment

and? This doesn't seam like an answer. Or I am completely missing your point.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.