I'm learning C after a little coding in Java and C++, I've just asked this question about ANSI and old-style C function definitions/declarations here .
I understand that if I don't declare a function,it is deduced from context,when I call it,to be int function()
with a arbitrary number of arguments, and that if I provide a definition for it in the same file,as long as it declares a int return type it can take any arguments (nothing is assumed at the declaration).
Now, I understand that standard C avoids this syntax,it being error-prone and generally a receipt for trouble. So I use a proper function prototype as int function(void)
(assuming I want a function that takes no arguments), and I can call it only as function()
and the compiler enforces a subsequent definition to be int function(){....}
and nothing else.
But... if I define the function in another compilation unit,it compiles (and links) even if the parameter list doesn't match or even if the return type doesn't! I assume this lead to undefined behaviour?
can someone explain me this behaviour? why doesn't the compiler enforces something to avoid all this (as in C++)?