klaufir
klaufir

Reputation: 31

C: defining function below main - why does this compile?

struct ack {
    char a, b, c;
};

main()
{
   struct ack bad;

   foo(bad);
}

foo(c)
   struct ack c;
{

}

This is a test case in gcc. When I try to compile it using gcc4.8, it compiles without problem. However, I have learned that you have to declare your functions before main. Why does this even compile?

Upvotes: 0

Views: 151

Answers (2)

JackCColeman
JackCColeman

Reputation: 3807

Currently, in general, when the compiler finds the call to foo(bad) in main() and foo is NOT defined, then the compiler assumes it exists and will return int.

This punts the problem to the linker who will complain if foo() is NOT defined somewhere in the source file.

Upvotes: 0

ouah
ouah

Reputation: 145829

Try to compile with

-std=c11 -pedantic-errors

and you'll get the required diagnostics.

By default gcc compiles with -std=gnu89 which is c89 + GNU extensions. The c89 rule for implicit declarations has been removed in c99.

Upvotes: 6

Related Questions