Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C prototype functions

As a beginner to C, I can understand the need for function prototypes in the file, but am unsure of a couple things.

First, does every function call outside of the main require a prototype declaration? Are there any conditions where that can change?

Second, do you need a separate function prototype for method overloads?

like image 596
Jason Avatar asked Feb 06 '11 16:02

Jason


3 Answers

No, functions are not required to have prototypes. However, they are useful as they allow functions to be called before they are declared. For example:

int main(int argc, char **argv)
{
    function_x(); // undefined function error
    return 0;
}

void function_x()
{

}

If you add a prototype above main (this is usually done in a header file) it would allow you to call function_x, even though it's defined after main. This is why when you are using an external library that needs to be linked, you include the header file with all the function prototypes in it.

C does not have function overloading, so this is irrelevant.

like image 25
Tim Cooper Avatar answered Oct 25 '22 07:10

Tim Cooper


Function calls in C don't require a prototype to be visible but it is highly recommended that a correct prototype is in scope.

The reason for this is that if the function definition doesn't match the types of the function arguments after the default function argument promotions are performed you are highly likely to get undefined behavior.

Having the correct prototype visible means that the compiler can check the arguments of a function call and warn the programmer if there is a mismatch.

C doesn't allow functions to be overloaded so you can only have a single prototype for any function name.

Default argument promotions can cause unexpected mismatches.

E.g.

int main(int argc, char **argv)
{
    short s = 5;
    float f = 2.3f;
    x(s, f);     // x implicitly declared; default argument promotions performed
    return 0;
}

int x(short t, float g)  // Error: called with an int and a double
{
    return (int)(t + g);
}

In the function call, because x has no visible prototype (yet), s will be promoted to int and f will be promoted to double. These are default argument promotions. This then causes a mismatch when the function is defined with a prototype that takes a short and a float even though these are the original types of the arguments that were passed in.

Functions that take a variable number of arguments (i.e. use , ... syntax) must always have a visible prototype at the point at which they are called.

like image 122
CB Bailey Avatar answered Oct 25 '22 05:10

CB Bailey


Others have already pointed out that C doesn't require prototypes for functions. I'll just add a couple minor points.

First of all, without a prototype, the arguments to a function always undergo "default promotions" before being passed as parameters, so (for example) all the smaller integer types get promoted to int, and float gets promoted to double. Therefore, without a prototype, it's impossible for a function to receive (for example) a char, short, or float. If the function is defined to receive one of those types, there will be a mismatch between what's passed and what the function expects, giving undefined behavior (any other mismatch gives undefined behavior as well).

Second, if a function is defined with a variable argument list, then you do need a prototype for it, or else calling it will result in undefined behavior.

like image 35
Jerry Coffin Avatar answered Oct 25 '22 07:10

Jerry Coffin