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?
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.
In the function call, because
x
has no visible prototype (yet),s
will be promoted toint
andf
will be promoted todouble
. These are default argument promotions. This then causes a mismatch when the function is defined with a prototype that takes ashort
and afloat
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.