首页 > 在程序开头没有写#include <stdio.h>,但是main()中的printf("hello\n");还是通过了编译而且运行正确,为什么?

在程序开头没有写#include <stdio.h>,但是main()中的printf("hello\n");还是通过了编译而且运行正确,为什么?

在程序开头没有写 #include stdio.h,但是 main() 中的 printf("hello\n"); 还是通过了编译而且运行正确。为什么会这样?举出例子说明什么情况下不#include相应的头文件程序仍然能通过编译但是不能正确运行。


以下的一些说明,使用如下的测试用例:

int main(int argc, char **argv)
{
    printf("Just for test\n");

    return 0;
}

printf属于gcc的内建函数,在不添加头文件的情况下也可以正常使用,编译器会帮你处理一些事情,只不过在编译的时候会发出警告:

warning: incompatible implicit declaration of built-in function ‘printf’ [enabled by default]

编译的时候不使用启动库和标准库:

$ gcc noheader.c -nostdlib -t
noheader.c: In function ‘main’:
noheader.c:3:2: warning: incompatible implicit declaration of built-in function ‘printf’ [enabled by default]
/usr/bin/ld: mode elf_i386
/tmp/ccJduOq0.o
/usr/bin/ld: warning: cannot find entry symbol _start; defaulting to 00000000080480d8
/tmp/ccJduOq0.o: In function `main':
noheader.c:(.text+0x11): undefined reference to `puts'
/usr/bin/ld: link errors found, deleting executable `a.out'
collect2: error: ld returned 1 exit status

正常情况下的编译:

$ gcc noheader.c -t
noheader.c: In function ‘main’:
noheader.c:3:2: warning: incompatible implicit declaration of built-in function ‘printf’ [enabled by default]
/usr/bin/ld: mode elf_i386
/usr/lib/gcc/i686-linux-gnu/4.7/../../../i386-linux-gnu/crt1.o
/usr/lib/gcc/i686-linux-gnu/4.7/../../../i386-linux-gnu/crti.o
/usr/lib/gcc/i686-linux-gnu/4.7/crtbegin.o
/tmp/ccbxOiWb.o
-lgcc_s (/usr/lib/gcc/i686-linux-gnu/4.7/libgcc_s.so)
/lib/i386-linux-gnu/libc.so.6
(/usr/lib/i386-linux-gnu/libc_nonshared.a)elf-init.oS
/lib/i386-linux-gnu/ld-linux.so.2
-lgcc_s (/usr/lib/gcc/i686-linux-gnu/4.7/libgcc_s.so)
/usr/lib/gcc/i686-linux-gnu/4.7/crtend.o
/usr/lib/gcc/i686-linux-gnu/4.7/../../../i386-linux-gnu/crtn.o

将测试中printf那句mark掉后编译:

$ gcc noheader.c -nostdlib -t
/usr/bin/ld: mode elf_i386
/tmp/ccmF4d1L.o
/usr/bin/ld: warning: cannot find entry symbol _start; defaulting to 00000000080480d8

建议:

个人猜测是编译器在检测到有和内建函数同名的函数时作了一些处理。期待你的正确答案!


C99之前,C语言不要求函数在使用前先声明,所有未声明函数都相当于int f(...),所以编译时不会出错,只会抱一个警告,又由于C标准库是会被缺省链接进去的,所以链接也不会出错,而且由于你的用法也没错,所以程序可以正常运行。
printf作为GCC的内置函数,被GCC特殊处理了,所以会显示一个不一样的信息,但即使你用其它C库里的函数比如puts,也只是警告信息的内容会有点不一样,其它也一切正常。
不过这绝对不是一个好习惯。


估计编译器自动给你加了

【热门文章】
【热门文章】