Reputation: 953
My question is a continuation of this one: extern function during linkage?
I now tried in file2.c:
extern int foo(void);
and I called
foo(1,2,3);
Now, I got a compilation error that there are too many arguments in foo(1,2,3);
Why does that happen ? We just said that extern functions are looked for during linkage and that in that stage there's no consideration regarding the parameters...
Upvotes: 0
Views: 124
Reputation: 134346
by saying extern int foo(void);
, you're telling the compiler to look for the function definition at linking time. In this process, you're already supplying the function prototype [declaration] int foo(void);
, where the number of parameter is 0.
But, while using , you're calling foo(1,2,3);
, so the comilation error is happenning.
Note: If i'm not mistaken, function declarations are by default extern
.
Upvotes: 0
Reputation: 726709
extern
functions are looked for during linkage and that in that stage there's no consideration regarding the parameters.
That's exactly right. However, you get an error at the compiling stage, not at linkage stage. You promised the compiler that there is a function foo
that takes no parameters, and then you call foo
with three parameters. Compiler does not take that, and reports the error.
The problem with the linker disregarding parameters would be if you separately compiled foo
with zero parameters and a call to foo
with a non-matching prototype that takes three parameters. This is undefined behavior.
impl.c
void foo() {}
main.c
void foo(int,int,int);
int main(int argc, char *argv[]) {
foo(1, 2, 3);
return 0;
}
If you compile the above, it would link, because you tricked the compiler by giving it a wrong prototype, and the linker does not know any better.
Upvotes: 2