The source of all evils: implicit function declaration in C language

Source: Internet
Author: User

1 What is the implicit function declaration of C language

In the C language, functions are not necessarily declared before they are called. If there is no declaration, the compiler automatically generates assembly code for the C code that invokes the function, in accordance with an implicitly-declared rule. Here is an example:

int main(intchar** argv){    double x = any_name_function();    return0;}

Simply compile the above source code, and there is no error, but in the link stage because the name is not found any_name_function function body and error.

`main‘`any_name_function‘collect2: ld 返回 1

Compilation does not cause errors because the C language specifies that implicit declarations are automatically used for functions that are not declared. The equivalent becomes the following code:

int any_name_function();int main(intchar** argv){    double x = any_name_function();    return0;}
2 Problem 2.1 implicitly declares that the function name exists exactly in the link library, but returns a non-int type

The examples given above do not have much impact, as it is easy to spot problems in the link phase. The following example, however, creates an inexplicable run-time error.

#include <stdio.h>int main(intchar** argv){    doublesqrt(1);    printf("%lf", x);    return0;}

GCC Compile link

[smstong@centos192 test]$ gcc -c main.cmain.c: 在函数‘main’中:main.c:6: 警告:隐式声明与内建函数‘sqrt’不兼容[smstong@centos192 test]$ gcc main.o

Run results

1.000000

A warning is given at compile time that the implicit declaration is incompatible with the built-in function ' sqrt '. The GCC compiler can automatically find functions with the same name as implicitly declared in the Common library header file (built-in functions) at compile time, and if it finds that they are not the same, the calling code is generated according to the declaration prototype of the built-in function. This is often the idea that programmers expect.
The function prototype implicitly declared in the example above is:

intsqrt(int);

The corresponding built-in function prototypes for the same name are:

doublesqrt(double);

The final compiler compiles the built-in function prototypes to achieve the desired results. However, this behavior of the GCC compiler is not a C-language specification, and not all compiler implementations have this capability. The same source code in the vc++2015 under the results of the compilation run:

VC + + Compilation

warning C4013: “sqrtint

Run results

2884223.000000

Obviously, VC + + compiler does not have the so-called "built-in function", but simply follow the implicit declaration of the prototype, generate calls to the SQRT function code. Because of the difference in the return type and the parameter type, the wrong function is called, which produces a strange and strange run-time error.

In this case, because of the different return types, both compilers can give warning messages, at least to the attention of the programmer. The following situation is more subtle.

2.2 Implicitly declares that the function name exists exactly in the link library and returns an int type

The test code is as follows:

#include <stdio.h>int main(intchar** argv){    intabs(-1);    printf("%d", x);    return0;}

At this point, because the implicitly declared function prototype is exactly the same as the built-in function prototype of GCC, GCC does not give any warning and the result is correct.
VC + + will still give a warning: warning C4013: "abs" is undefined; suppose an external return int.

In any case, the implicitly declared function prototype is exactly the same as the library function, so there is no problem with the link running.

Below, change the code a little bit:

#include <stdio.h>int main(intchar** argv){    intabs(-1,2,3,4);    printf("%d", x);    return0;}

There is no error in compiling the link under GCC.

GCC Compile link

[smstong@centos192 test]$ gcc -c main.c[smstong@centos192 test]$ gcc main.o

As can be seen, the internal function mechanism of GCC does not care about the parameters of the function, only the return value of the function.

VC + + Compile link

warning C4013: “absint

Although the result of this example is correct, it is "happened" because the extra function parameter does not affect the result. This accidental correctness is to be avoided in the program.

3 Considerations in programming

The implicit function declaration of C language brings a variety of puzzles to programmers, which has a very bad effect on the stability of the program. I do not know how the C language designers think of this problem?

* To avoid this effect, it is strongly recommended that programmers pay attention to the compiler's warnings about implicit declarations, and to eliminate such warnings in a timely manner by including the necessary header files. *

For GCC, the special example of ABS ( -1,2,3,4) given above, the compiler does not produce any warning at all, but only by the programmer's familiarity with each library function that he calls.

In order to avoid this problem, in the C99 version of the C language, a warning will be given anyway. such as GCC uses C99 to compile the above code.

Gcc-std=c99 compiling

[[email protected] test]$ gcc -c main.c -std=c99main.c: 在函数‘main’中:main.c:5: 警告:隐式声明函数‘abs’

C + + is more stringent, directly discarding the implicit function declaration, for undeclared function calls, will not be compiled directly.

g++ compiling

[smstong@centos192function ‘int main(intchar**)’:main.c:5: 错误:‘abs’在此作用域中尚未声明

VC + + compilation (as C + +)

error C3861: “abs”: 找不到标识符

C + + is indeed stricter and more rigorous than C in a strongly typed function.

The source of all evils: implicit function declaration in C language

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.