Two function declarations with void and empty argument list

Viewed 2533

I'd like to know why the following code:

void foo(void);
void foo()
{
}

is valid in gcc. In C, there is no such thing as overloading and above declarations (in fact, one of them is a definition) declare two different functions (the first one doesn't take any arguments, the second one could take any number of arguments of any types).

However, if we provide a definition for the first function:

void foo(void)
{
}
void foo()
{
}

a compilation fails this time due to redefinition. But still, the first code is correct and might be confusing as in the following:

void foo(void);

int main(void)
{
    foo();      //OK
    //foo(5);   //Wrong, despite ->the definition<- allows it
}

void foo()
{
}

On the other hand, something like this is invalid straightaway:

void foo(int);
void foo() //error: number of arguments doesn't match prototype
{
}

I think the compiler's behavior is kinda weird in comparison to my first foregoing code. int isn't equal to (/*empty list*/) and neither is void.

Can anyone explain this ?

3 Answers
Related