不包括;
下面给定的程序是否可以在不包含
的情况下工作?为什么这有效?
int main()
{
printf("integra");
return 0;
}
below given program is working without including <stdio.h>
? Why does this work?
int main()
{
printf("integra");
return 0;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
printf() 的定义位于 libc.so 中,即使您不包含头文件,动态链接器也会处理它。在编译时, printf() 将是一个未定义的符号,并且它假设稍后可以在 libc 中找到定义。头文件只会给出原型类型并抑制编译器(警告),表明原型的定义存在于 glibc 中。所以基本上,包含头文件只是为了确保定义在我们的库中可用,以帮助开发人员。
Definition of printf() is there in libc.so and the dynamic linker will take care of it even if you don't include the header file. During compile time, printf() will be an undefined symbol and it assumes that it may find the definition later on in libc. The header file will just give the proto-type and suppress the compiler(warnings) stating that the definition of the prototype is present in glibc. So basically, the header files are included just to make sure that the definitions are available in our libraries, to help the developer.
在旧标准中,未声明的函数采用
int
参数和返回值。您的 char* 与 int 具有相同的大小(32 位),因此一切正常。只是不要这样做。
in older standard, undeclared function assume
int
argument and return value. Yourchar*
have same size (32-bit) asint
, so everything work.Just don't do it.
printf() 仅在 libc.so 中定义
动态链接器将解析 libc 中的符号 printf(),因为您尚未包含它
libc 是每个程序的 gcc 中的默认值
printf() is only defined in libc.so
The dynamic linker will resolve the symbol printf() in libc since you have not included it
libc is default in gcc for every program
正如 Abi 指出的,您的代码在不包含 stdio.h 的情况下构建成功,因为链接器默认使用系统 std 库来处理未定义的符号 (
printf
)。 GCC 通常会警告您此类情况。让 test.c 为:
在 Mac OSX 上使用 GCC 4.2.1 构建 test.c:
您可以通过指定 GCC 链接器选项
-nostdlib
(或-nodefaultlibs) 以及
-lgcc
(按照 GCC 手册的建议):As Abi pointed out, your code builds successfully without including stdio.h because the linker is defaulting to the system std library for the undefined symbol (
printf
). GCC would normally warn you of such cases.Let test.c be:
Building test.c with GCC 4.2.1 on Mac OSX:
You can disable this default linking by specifying a GCC linker option
-nostdlib
(or-nodefaultlibs
) along with-lgcc
(as the GCC manual recommends):一些(旧的?)编译器在调用函数之前不需要原型。
Some (old?) compilers do not require prototypes before calling functions.
当您使用尚未声明的函数时,编译器将假定该函数返回 int 并采用未指定但固定数量的参数。
如果此假设与函数的定义匹配,并且您提供的参数也与函数期望接收的参数匹配(以默认参数提升为模),那么一切都很好。
如果假设不正确(例如 printf,它是一个可变参数函数),或者当参数不匹配时,结果是未定义的。未定义行为的令人讨厌的事情之一是它看起来可以按预期工作。
When you use a function that has not been declared, then the compiler will assume this function returns an
int
and takes an unspecified, but fixed, number of arguments.If this assumption matches with the definition of the function, and if the arguments you provided also match (modulo the default argument promotions) the parameters that the function expects to receive, then everything is well.
If the assumption is incorrect (like for
printf
, which is a variadic function), or when the arguments don't match, the results are undefined. One of the nasty things of undefined behaviour is that it can appear to work as expected.