希望这是一个非常简单的问题。以下是我拥有的 C pgm (test.c)。
#include <stdio.h>
//#include <stdlib.h>
int main (int argc, char *argv[]) {
int intValue = atoi("1");
double doubleValue = atof("2");
fprintf(stdout,"The intValue is %d and the doubleValue is %g\n", intValue, doubleValue);
return 0;
}
请注意,我使用的是 stdlib.h 中的 atoi() 和 atof(),但我不包含该头文件。我编译了 pgm (gcc test.c) 并且没有编译器错误!
我运行 pgm (./a.out),这是错误的输出。
The intValue is 1 and the doubleValue is 0
现在我包含stdlib.h(通过删除#include 之前的注释)并重新编译并再次运行它。这次我得到了正确的输出:
The intValue is 1 and the doubleValue is 2
为什么编译器没有抱怨不包括 stdlib.h 并且仍然让我使用 atoi()、atof() 函数?
我的 gcc 信息:
$ gcc --version
gcc (GCC) 4.1.2 20070925 (Red Hat 4.1.2-27)
任何想法表示赞赏!