使用printf函数没有实际进口stdio.h中和它的工作?为什么会这样? [英] Using printf function without actually importing stdio.h and it worked?! Why is that so?
问题描述
可能重复:结果
<一href=\"http://stackoverflow.com/questions/336814/why-include-stdio-h-is-not-required-to-use-printf\">Why #包括LT&;&stdio.h中GT;是的不的使用需要的printf()?
块引用>//#包括LT&;&stdio.h中GT;
//#包括LT&;&CONIO.H GT;主要(){的printf(你好);
残培();}当我在编程,这令我震惊,它的工作没有实际导入任何C库如包含printf函数STDIO。为什么会这样? (用于开发-C ++ 4.9.9.2,保存为.c的,不是的.cpp)
解决方案c可让您调用函数不首先确定了原型。 (C ++不这样做。)为
的printf
隐含的原型将被定义如下:INT的printf();
巧合的是,这隐含原型的调用约定相匹配的
的printf
实际调用约定您的平台。在一般情况下,你不能指望这方面的工作,并有大量的情况下,它不会工作的。我建议让编译器警告检测隐原型声明,以便您可以修复它们(通过包括正确的标头)。
脚注:
的#include
不导入库,它只是粘贴到文件在编译时你的源$ C $ C。在&LT;&stdio.h中GT;
头包含(直接或间接)某些原型,但图书馆在分别被链接。由于的printf
通常是链接到默认的程序库中,你通常没有做任何事情来使用的printf
。Possible Duplicate:
Why #include <stdio.h> is not required to use printf()?
//#include <stdio.h> //#include <conio.h> main(){ printf("Hi"); getch(); }
As I was programming this, it shocked me that it worked without actually importing any c libraries such as stdio that contains the printf function. Why is that so? (Used Dev-C++ 4.9.9.2, saved as .c, not .cpp)
解决方案C allows you to call functions without first defining the prototypes. (C++ does not do this.) An implicit prototype for
printf
will be defined as follows:int printf();
Coincidentally, the calling conventions for this implicit prototype matched the actual calling conventions for
printf
on your platform.In general, you cannot count on this working, and there are a large number of cases where it won't work. I recommend enabling compiler warnings to detect implicit prototype declarations so you can fix them (by including the correct header).
Footnote:
#include
does not import libraries, it merely pastes files into your source code at compile time. The<stdio.h>
header contains (directly or indirectly) certain prototypes, but the library has to be linked in separately. Sinceprintf
is usually in a library that is linked to programs by default, you usually don't have to do anything to useprintf
.这篇关于使用printf函数没有实际进口stdio.h中和它的工作?为什么会这样?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!