Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Non-prototyped function declarations

Tags:

c

How is the function declaration different from the function prototype?

I'm posting this question in reference to this answer on a question.

like image 299
Bazooka Avatar asked Dec 28 '22 07:12

Bazooka


2 Answers

A Function declaration may/may not not include the function arguments.
While a Function Prototype must include the function arguments.

From Wikipedia:
Consider the following function prototype:

int fac(int n);

This prototype specifies that in this program, there is a function named fac which takes a single integer argument n and returns an integer. Elsewhere in the program a function definition must be provided if one wishes to use this function.

It's important to be aware that a declaration of a function does not need to include a prototype. The following is a prototype-less function declaration, which just declares the function name and its return type, but doesn't tell what parameter types the definition expects.

int fac();
like image 79
Alok Save Avatar answered Jan 08 '23 17:01

Alok Save


A prototype is a declaration, but a declaration not always is a prototype. If you don't specify the parameters, then that's only a declaration and not a prototype. That means the compiler won't reject a call to that function complaining it wasn't declared, but won't be able to check if the parameters passed are correct (as it would if you had a prototype).

like image 42
Fabio Ceconello Avatar answered Jan 08 '23 17:01

Fabio Ceconello