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

Source: Internet
Author: User
Tags function prototype

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, then the compiler will take the initiative to generate assembly code for the C code that invokes the function, in accordance with an implicitly-declared rule. Here's a sample:

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

Simply compile the above source code. No matter what the error, just in the link stage due to not find the function body named any_name_function and error.

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

The reason why the compilation will not error, is due to the C language provisions, for non-declared functions, self-actively use implicit declaration.

The equivalent becomes such as 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 example given above. does not cause too much impact. Because in the link phase very easy to discover the problems that exist.

The following example, however, can cause inexplicable execution-time errors.

#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

Execution results

1.000000

A warning is given at compile time that the implicit declaration is incompatible with the built-in function ' sqrt '. At compile time, the GCC compiler can proactively look for functions that have the same name as implicitly declared in the library header file (built-in function), if they do not find the same. The calling code is generated according to the declaration prototype of the built-in function. This is often the idea of procedural ape expectations.
The function prototype implicitly declared in the above example is:

intsqrt(int);

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

doublesqrt(double);

Finally, the compiler compiles according to the built-in function prototype. achieved the desired effect.

However, the GCC compiler's behavior is not a C-language specification, and not all compiler implementations have such functionality.

The result of the same source code being compiled and executed under VC++2015 is:

VC + + Compilation

warning C4013: “sqrtint

Execution results

2884223.000000

Obviously. VC + + compiler does not have the so-called "built-in function", simply according to the implicit declaration of the prototype, generate calls to the SQRT function code. Because of the difference between the return type and the parameter type. The way the function was called that caused the error. Produces strange and bizarre execution-time errors.

In this case, due to the different return types, both compilers can give warning messages, at least to the attention of the program ape. The following situations are more subtle.

2.2 Implicitly declares that the function name exists exactly in the link library. and returns the INT type

Test code such as the following:

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

At this time The implicit declaration of a function prototype is exactly the same as the built-in function prototype of GCC. So GCC will not give no warning, and the result is correct.
VC + + will still give a warning: warning C4013: "ABS" is not defined. If int is returned externally.

In any case, the implicit declaration of a function prototype is exactly the same as a library function, so there is no problem with link execution.

Below, slightly modify the code:

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

GCC compiles the link without any error.

GCC Compile link

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

Visible. The built-in function mechanism of GCC does not care about function parameters. The only concern is the return value of the function.

VC + + Compile link

warning C4013: “absint

Although the result of this example is correct, the correctness of this is "happen", because the additional function parameters do not affect the result. Such accidental correctness is something to avoid in the program.

3 Considerations in programming

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

* In order to avoid this effect, it is strongly recommended that the program ape attach importance to the compiler's warning about implicit declarations, and promptly eliminate such warnings by including the necessary header files. *

For GCC. A special example of ABS ( -1,2,3,4) given above. The compiler does not produce any warnings at all, only the program apes are familiar with each library function that they call.

In order to avoid this problem, in the C language C99 version number, regardless will give the warning. 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”: 找不到标识符

At the point where the function is strongly typed. C + + is really stricter and more rigorous than C.

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

Related Article

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.