Implicit function declarations in C

后端 未结 6 1376
你的背包
你的背包 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:55

    It should be considered an error. But C is an ancient language, so it's only a warning.
    Compiling with -Werror (gcc) fixes this problem.

    When C doesn't find a declaration, it assumes this implicit declaration: int f();, which means the function can receive whatever you give it, and returns an integer. If this happens to be close enough (and in case of printf, it is), then things can work. In some cases (e.g. the function actually returns a pointer, and pointers are larger than ints), it may cause real trouble.

    Note that this was fixed in newer C standards (C99, C11). In these standards, this is an error. However, gcc doesn't implement these standards by default, so you still get the warning.

提交回复
热议问题