I'm facing a warning while compiling a C project:
myFunct was used with no prototype before its definition.
I do not understand this warning since the prototype is indeed before the call of the function.
Here is my code (simplified):
void myFunct();
int main(void)
{
myFunct();
}
void myFunct()
{
// Whatever
}
It is not required, but it is bad practice not to use prototypes. With prototypes, the compiler can verify you are calling the function correctly (using the right number and type of parameters).
A function prototype is a function declaration that specifies the number and types of parameters. T foo(); // non-prototype declaration T foo(int, char *); // prototype declaration T foo(int a, char *b); // prototype declaration.
There are two ways to resolve protype error. 1:put your entire user defined function block before main function. Despite the number of user defined function put all of them before main function. 2:Define a protype of function before maim after headers of c.
The function prototypes are used to tell the compiler about the number of arguments and about the required datatypes of a function parameter, it also tells about the return type of the function. By this information, the compiler cross-checks the function signatures before calling it.
In C, void myFunct();
is a function declaration, that does not include a prototype. It is an obsolescent feature dated back to K&R (pre-standard) C, where function calls and declarations were not checked.
Replace the declaration with:
void myFunct(void);
Your code provides a forward declaration for function myFunct()
, but that declaration is not a prototype because it does not declare the types of the (zero) function parameters (see below). The declaration you provide declares the function to return nothing and to accept a fixed but unspecified number of parameters,* which you will appreciate is insufficient for the compiler to fully verify the function call. An actual prototype for that function would be this:
void myFunct(void);
Your code is nevertheless completely valid. K&R-style function declarations are still allowed, and they satisfy C's declaration-before requirements. They're just not good style.
Details:
C2011 6.2.1/2 says,
A function prototype is a declaration of a function that declares the types of its parameters.
C2011 6.7.6/1 distinguishes between a "parameter type list", used in the ANSI declaration style and in particular for function prototypes, and an "identifier list", used in K&R-style declarations. Neither can be empty, but (only) the latter can be omitted altogether, so a function declaration with empty parentheses is a K&R-style declaration, which does not provide a prototype.
*C++ differs from C on this.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With