A brief analysis of C language header and library some problems

  • 2020-04-02 01:14:58
  • OfStack

The compiler header file that USES GCC does not contain stdlib.h, USES the atoi function (the atoi function is declared in stdlib.h), and compiles without error

If the -wall option is added when compiling, there will be a warning. Why?
This is because of a very silly C stipulation: a function that does not declare a function prototype returns an int (called implicit declaration). Since atoi happens to actually return an int, you don't report an error even if you don't include its header file. As for this warning, it is in case you make a mistake by forgetting to declare the function prototype.

The compiler simply returns an int for an undefined function prototype, and it does not need to know that it has been defined
That is, when you call atoi with an argument list, the compiler already knows what the argument list of the function is, it just doesn't know the return value, so what else does the compiler need? Of course it can pass.
Note: the compiler doesn't care which header file a function is defined in, it just needs to know what prototype the function is

#include<stdio.h>
//#include<stdlib.h>
int main()
{
int i;
double f;
char b[5] = "23";
char c[5] = "2.3";
i = atoi(b);
f = atof(c);
printf("i=%d f=%lfn", i, f);
return 0;
}

GCC test.c-o test has no warning
Gcc-wall test. C-o test
Warning: w9.c: In function 'main':
Warning: implicit declaration of function 'atoi'
W9. C :10: warning: implicit declaration of function 'atof'
Output results:
I = 23 f = 1717986918.000000
Add # include < Stdlib. H > The result is normal
It seems that both atoi and atof are in the C standard library glibc, but I wonder why the C standard library functions SQRT and pow are not in glibc

Related articles: