Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C Warning : Function was used with no prototype before its definition [closed]

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
}
like image 827
M.Ferru Avatar asked Dec 21 '17 14:12

M.Ferru


People also ask

Is function prototype mandatory for every user defined function in C?

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

What is non prototype function in C?

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.

How do you fix prototype errors?

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.

Why does C need function prototype?

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.


2 Answers

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);
like image 118
Grzegorz Szpetkowski Avatar answered Oct 25 '22 21:10

Grzegorz Szpetkowski


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.

like image 22
John Bollinger Avatar answered Oct 25 '22 22:10

John Bollinger