implicit function declaration _c language in C language

Source: Internet
Author: User
Tags abs function prototype

1 What is a C-language implicit function declaration

In the C language, a function does not have to be declared before it is invoked. If there is no declaration, the compiler automatically generates assembly code for the C code of the calling function according to an implicitly-declared rule. Here is an example:

int main (int argc, char** argv)
{
  Double x = any_name_function ();
  return 0;
}

Simply compile the source code above, and there is no error, just in the link phase because the name is not found Any_name_function function body and error.

[smstong@centos192 test]$ gcc-c main.c
[smstong@centos192 test]$ gcc main.o
main.o:in function ' main ':
MAIN.C: (. text+0x15): Undefined reference to ' any_name_function '
collect2:ld return 1

Compilation does not complain because the C language stipulates that for functions that are not declared, an implicit declaration is automatically used. The equivalent becomes the following code:

int any_name_function ();
int main (int argc, char** argv)
{
  Double x = any_name_function ();
  return 0;
}

2 problems brought by

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 cause much impact, as it is easy to spot problems in the link phase. However, the following example will cause inexplicable run-time errors.

#include <stdio.h>
int main (int argc, char** argv)
{
  Double x = sqrt (1);
  printf ("%lf", x);
  return 0;
}

GCC Compile link

[smstong@centos192 test]$ gcc-c main.c
main.c: In function ' main ':
main.c:6: Warning: Implicit declaration incompatible with built-in function ' sqrt '
[ smstong@centos192 test]$ gcc MAIN.O

Run results

1.000000

A warning is given at compile time that an implicit declaration is incompatible with the built-in function ' sqrt '. The GCC compiler, at compile time, automatically looks for functions with the same name as an implicit declaration in common Library header files (built-in functions), and if the two are found to be different, the calling code is generated according to the declaration prototype of the built-in function. This is often the idea that programmers expect.
In the example above, the implicitly-declared function prototype is:

int sqrt (int);

The corresponding built-in function prototype for the same name is:

Double sqrt (double);

The final compiler compiles according to the built-in function prototype, and achieves the expected result. However, this behavior of the GCC compiler is not a specification of the C language, and not all compiler implementations have such a function. The same source code in the vc++2015 of the results of the compilation run is:

VC + + Compilation

Warning C4013: "sqrt" undefined; suppose the outer return int

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 between the return type and the parameter type, the wrong function invocation is caused by a strange run-time error.

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

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

The test code is as follows:

#include <stdio.h>

int main (int argc, char** argv)
{
  int x = ABS ( -1);
  printf ("%d", x);
  return 0;
}

At this point, because the implicitly-declared function prototype is identical to 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" undefined; suppose the outer return int.

In any case, the implicitly-declared function prototype is exactly the same as the library function, so the link runs without problems.

Next, change the code a little bit:

#include <stdio.h>

int main (int argc, char** argv)
{
  int x = ABS ( -1,2,3,4);
  printf ("%d", x);
  return 0;
}

The compile link under GCC does not have any errors.

GCC Compile link

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

As you can see, the built-in function mechanism of GCC does not care about the parameters of the function, just the return value of the function.

VC + + Compile link

Warning C4013: "ABS" not defined; Suppose the outer return int

Although the results of this example are correct, the correctness is "coincidence" because the additional function parameters do not affect the result. This accidental correctness is to be avoided in the procedure.

3 Considerations in programming

The implicit function declaration of C language brings all kinds of puzzles to the programmer, which brings a very bad effect to the stability of the program. I do not know how the original C language designers to consider this problem?

* In order to avoid this effect, programmers are strongly advised to pay attention to the compiler's warnings about implicit declarations and to eliminate such warnings by including the necessary header files in a timely manner. *

For GCC, the special example of ABS ( -1,2,3,4) that was given earlier, the compiler would have no warning at all, and it would rely on the programmer to familiarize itself with every library function it calls.

To avoid this problem, in the C99 version of the C language, a warning is given anyway. If GCC uses C99 to compile the above code.

GCC-STD=C99 compilation

[smstong@centos192 test]$ gcc-c main.c-std=c99
main.c: In function ' main ':
main.c:5: Warning: implicit declaration function ' Abs

C + + is more stringent, directly abandoned the implicit function declaration, for undeclared function calls, will not be directly through the compilation.

g++ compilation

[smstong@centos192 test]$ g++ main.c
main.c:in function ' int main (int, char**) ':
main.c:5: Error: ' ABS ' not yet declared in this scope

VC + + compilation (as C + +)

Error C3861: "ABS": identifier not found

C + + is indeed stricter and more rigorous than c in the sense that the function is strongly typed.

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.