0

I'm a Java programmer,I learnt a little C++ and now I'm studying a little C for my job. I can't understand C behaviour about function declaration/definition and related function calls. From K&R I know that in C (very different from C++) I can call a function that has not been previously declared,and the compiler assumes an implicit declaration of the type:

int main() 
{
  function(10);   // implicit function declaration ( int function() )
}

and I know that such a declaration implies a function that accepts a fixed but indefinite number of arguments of any type (as long as each call is consistent with the others). And I know this is K&R C, before C89, but I want to know how it works as well. Now, I have this test code, I can't understand:

#include <stdio.h>

function();

int main()
{
  printf("hello %d",function(1,2,3));
  implicit(11,12,32);     // implicit function declaration ( implicit() )
}


int implicit(int b)
{

}

function(int a)
{

}

in the case of function the declaration (return type is assumed to be int,no assumption about the arguments) does match the definition (the compiler issues a warning) but if I call the function with the wrong arguments,it compiles! Same for the function implicit. I can't understand..

Luca
  • 1,658
  • 4
  • 20
  • 41
  • These type of issues is exactly why C++ requires functions to be declared in advanced. And that's a practice I would stick to in C. – Jonathan Wood Nov 13 '15 at 20:57
  • 6
    This is old-style, deprecated, bad code. Do you as a C beginner really need to know how bad code works? – n. m. could be an AI Nov 13 '15 at 20:58
  • Yeah, C needs the .h files where you put your function definitions (prototypes). It will assume they exist somewhere during compile time and then fail at run time. Take a look at the stdio.h file for that. – jmbmage Nov 13 '15 at 20:59
  • @n.m. yes,because I stumbled upon it because,for example, a function declaration without parameters is a function that doesn't take arguments for me, understanding deprecated (but supported by compiler) code helps me understand what coding style to avoid – Luca Nov 13 '15 at 21:02
  • 2
    If you're studying C for your job, you should study something useful, like how to properly declare a function with a well-formed function prototype. And you should use a modern compiler that will issue a warning when you get it wrong. And most importantly, fix everything that the compiler warns you about. Never ignore warnings. – user3386109 Nov 13 '15 at 21:04
  • @user3386109,yes, but if I think that `int f()` is a well formed function declaration that takes no args,and then I discover that it's not, I need to understand why,to avoid pitfalls (for example,to use `int f(void)`) – Luca Nov 13 '15 at 21:05

2 Answers2

3

The thing you have to remember is that the compile is pretty much sequential when it comes to declarations and definition. When the compiler processes the call for function all it has is the declaration that, as you say, doesn't have any assumptions about the arguments, which means you can call the function with any argument you like. When the compiler sees the definition, it doesn't go back to issue an error about the call, it might issue a warning though.

As for the implicit function, when the compiler first sees it it will assume that the arguments are what you pass in the call when it deduces the declaration. Again it will not know anything else until it later sees the declaration, and may issue a warning then.

Calling a function with to many, or to few, arguments leads to undefined behavior, which is why implicitly declared functions are so dangerous, as well as using an empty argument list when declaring functions.

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
  • so, while in C++ every function name is declared with a prototype,and the prototype must match the definition (in the same or another compilation unit), I could use the same syntax in ANSI C, but if I use old style (which I won't) C,and use implicit or no-arg function declaration,the compiler doesn't enforce declaration/definition matching,and everything could happen? – Luca Nov 13 '15 at 21:13
  • 3
    @Luca Short answer to a long question is yes. – Some programmer dude Nov 13 '15 at 21:27
0

There is really nothing to understand. This is a legacy C behaviour, which was an extremely lax language. As long as compiler could generate assembly instruction, it would gladly compile your code, and leave it to you to clean up the mess.

This is why it compiles in your case. Compiler can generate instruction to call the function - so it does as asked.

SergeyA
  • 61,605
  • 5
  • 78
  • 137
  • yes,but from a C++ perspective, it's hard to understand. It's the compiler behaviour that baffles me, allowing a function call that "matches" a previous declaration (implicit,or allowing any arguments) but with a subsequent different definition. – Luca Nov 13 '15 at 21:21