Implicit function declarations in C

后端 未结 6 1525
你的背包
你的背包 2020-11-22 07:04

What is meant by the term \"implicit declaration of a function\"? A call to a standard library function without including the appropriate header file produces a warning as i

6条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-11-22 07:58

    C is a very low-level language, so it permits you to create almost any legal object (.o) file that you can conceive of. You should think of C as basically dressed-up assembly language.

    In particular, C does not require functions to be declared before they are used. If you call a function without declaring it, the use of the function becomes it's (implicit) declaration. In a simple test I just ran, this is only a warning in the case of built-in library functions like printf (at least in GCC), but for random functions, it will compile just fine.

    Of course, when you try to link, and it can't find foo, then you will get an error.

    In the case of library functions like printf, some compilers contain built-in declarations for them so they can do some basic type checking, so when the implicit declaration (from the use) doesn't match the built-in declaration, you'll get a warning.

提交回复
热议问题