Implicit function declarations in C

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

    An implicitly declared function is one that has neither a prototype nor a definition, but is called somewhere in the code. Because of that, the compiler cannot verify that this is the intended usage of the function (whether the count and the type of the arguments match). Resolving the references to it is done after compilation, at link-time (as with all other global symbols), so technically it is not a problem to skip the prototype.

    It is assumed that the programmer knows what he is doing and this is the premise under which the formal contract of providing a prototype is omitted.

    Nasty bugs can happen if calling the function with arguments of a wrong type or count. The most likely manifestation of this is a corruption of the stack.

    Nowadays this feature might seem as an obscure oddity, but in the old days it was a way to reduce the number of header files included, hence faster compilation.

    0 讨论(0)
  • 2020-11-22 07:41

    Because of historical reasons going back to the very first version of C, functions are assumed to have an implicit definition of int function(int arg1, int arg2, int arg3, etc).

    Edit: no, I was wrong about int for the arguments. Instead it passes whatever type the argument is. So it could be an int or a double or a char*. Without a prototype the compiler will pass whatever size the argument is and the function being called had better use the correct argument type to receive it.

    For more details look up K&R C.

    0 讨论(0)
  • 2020-11-22 07:51

    To complete the picture, since -Werror might considered too "invasive",
    for gcc (and llvm) a more precise solution is to transform just this warning in an error, using the option:

    -Werror=implicit-function-declaration
    

    See Make one gcc warning an error?

    Regarding general use of -Werror: Of course, having warningless code is recommendable, but in some stage of development it might slow down the prototyping.

    0 讨论(0)
  • 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.

    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2020-11-22 08:03

    Implicit declarations are not valid in C.

    C99 removed this feature (present in C89).

    gcc chooses to only issue a warning by default with -std=c99 but a compiler has the right to refuse to translate such a program.

    0 讨论(0)
提交回复
热议问题