0

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++)?

Community
  • 1
  • 1
Luca
  • 1,658
  • 4
  • 20
  • 41
  • If that's not a duplicate, then search for 'c function prototype mismatch' – KevinDTimm Nov 13 '15 at 22:17
  • "I understand that if I don't declare a function,it is deduced from context" - that was only true in C89. For the last 16 years it's been an error to use a function without declaration. – M.M Nov 13 '15 at 23:44

0 Answers0