简体   繁体   中英

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?

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM