I've just come across someone's C code that I'm confused as to why it is compiling. There are two points I don't understand.
First, the function prototype has no parameters compared to the actual function definition. Second, the parameter in the function definition does not have a type.
#include <stdio.h>
int func();
int func(param)
{
return param;
}
int main()
{
int bla = func(10);
printf("%d", bla);
}
Why does this work? I have tested it in a couple of compilers, and it works fine.
If the function declaration has no parameters i.e. empty then it is taking unspecified number of arguments. If you want to make it take no arguments then change it to:
It's K&R style function declaration and definition. From C99 Standard (ISO/IEC 9899:TC3)
Section 6.7.5.3 Function Declarators (including prototypes)
Section 6.11.6 Function declarators
Section 6.11.7 Function definitions
Which the old style means K&R style
Example:
Declaration:
int old_style();
Definition:
int
.I would consider any build that passes this to be lacking in configured warning/error level though, there's no point in being this allowing for actual code.
C assumes
int
if no type is given on function return type and parameter list. Only for this rule following weird things are possible.A function definition looks like this.
If its a prototype you write
In prototype you can only specify the type of parameters. Parameters' name is not mandatory. So
Also if you dont specify parameter type but name
int
is assumed as type.If you go farther, following works too.
Compiler assumes
int func()
when you writefunc()
. But dont putfunc()
inside a function body. That'll be a function call