This will compile just fine. But all you're doing is declaring the function. This is the same as adding a (non-prototype) declaration at the top level.
int hey( );
// ^ empty parens means it's not a prototype
You can call a function in a declaration if it's part of the initializer.
#include <stdio.h>
int main()
{
int reply=hey();
// ^ here the function is called, even though this is a declaration,
// because the value is needed.
return 0;
}
int hey(){
return printf("Hello");
// a function returning `int` ought to `return ` an int!
};
But normally to call a function you just place the call in a (non-declaration) expression statement.
#include <stdio.h>
int main()
{
int reply; // declaring a variable
int hey(); // declaring a function
(void) hey(); // function call, casting return value to (void)
return 0;
}
int hey(){
return printf("Hello");
};
There is a restriction in some earlier compilers that only the last declaration can contain a function call. C99 (and most "modern" compilers) have relaxed this restriction and function calls can now be used in initializers with impunity.
IIRC splint
the syntax-checker has this same restriction on function calls in initializers.
It may be considered bad style, but it's not necessarily incorrect to call a function without a prototype. To be sure, it removes the compiler's ability to check that the call make sense from a type point-of-view. But all you really have to do is don't screw it up.
Non-prototyped functions will default to the standard calling conventions, which means all integer args (char, short, int) promote to int
and all float args promote to double
. These promotions also apply to variadic functions using #include <stdarg.h>
(and our beloved printf
), so I consider it very useful to know how a non-prototyped function will be called.
I've got some "don't screw it up" code here that calls non-prototyped functions through a function-pointer. It all works and conforms to the standard (near as I can figure), but I have no idea how to prototype that function-pointer which may point to one of many stereotypical patterns. It wouldn't be correct to use a variadic notation (...)
, because it's not the same thing. There's just no appropriate way to prototype it, so the pointer is just declared void (*fp)();
.