C语言 '#include <stdio.h>' 在 C 程序中到底做了什么
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19088284/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What does '#include <stdio.h>' really do in a C program
提问by yaylitzis
I am new to c programming and I was coding some simple programs "Hello world" style.
我是 C 编程的新手,我正在编写一些“Hello world”风格的简单程序。
In all of these programs I put #include<stdio.h>in the top but I am not sure what this means exactly. I googled it and I found that stdio.h is a file that has commands for the preprocessor, but what is a preprocessor? I thought when I write code, I compile it and my code transforms to a form that a "computer" can read and then I can run it. Can somebody explain to me what the usage of this command is?
在所有这些程序中,我都排#include<stdio.h>在最前面,但我不确定这到底意味着什么。我用谷歌搜索,我发现 stdio.h 是一个包含预处理器命令的文件,但什么是预处理器?我想当我写代码时,我编译它,我的代码转换成“计算机”可以读取的形式,然后我可以运行它。有人可以向我解释这个命令的用法吗?
回答by
It looks for the stdio.hfile and effectively copy-pastes it in the place of this #includestatements. This file contains so-called function prototypes of functions such as printf(), scanf(), ... so that compiler knows what are their parameters and return values.
它查找stdio.h文件并有效地将其复制粘贴到此#include语句的位置。该文件包含所谓的函数原型,例如printf(), scanf(), ... 以便编译器知道它们的参数和返回值是什么。
回答by fkl
The simplest explanation perhaps should be that your program callsor uses many functions whose code is not part of your program itself. For e.g. if you write "printf" in your code to print something, the compiler does not know what to do with that call.
最简单的解释也许应该是您的程序调用或使用了许多函数,这些函数的代码不属于您的程序本身。例如,如果您在代码中编写“printf”来打印某些内容,则编译器不知道如何处理该调用。
stdio.h is the place where information for that printf resides.
stdio.h 是该 printf 的信息所在的位置。
Update:
更新:
Rather the prototypeof printf function (name, return type and parameters) reside in stdio.h. That is all required in the compilation phase. The actual code of printf is included in the linking phase, which comes after compilation.
而printf 函数的原型(名称、返回类型和参数)位于 stdio.h 中。这在编译阶段都是必需的。printf 的实际代码包含在编译后的链接阶段。
The include statement basically inserts all function prototypes BEFORE the actual compilation. Hence the name preprocessor.
include 语句基本上在实际编译之前插入所有函数原型。因此得名预处理器。
Update 2:
更新 2:
Since the question focused on include statement (and the OP also asked about writing definition of functions himself, another important aspect is if it is written like (note the angular brackets)
由于问题集中在 include 语句上(并且 OP 还询问了自己编写函数定义的问题,另一个重要方面是它是否写成(注意尖括号)
#include <stdio.h>
The preprocessor assumes, it is a standard library header and looks in the system folders first where the compiler has been installed.
预处理器假定它是一个标准库头文件,并首先在安装了编译器的系统文件夹中查找。
If instead a programmer defines a function by himself and place the .h file in the current working directory, he would use (note the double quotes)
如果程序员自己定义一个函数并将 .h 文件放在当前工作目录中,他会使用(注意双引号)
#include "stdio.h"
Followingillustrates it and the behavior is portable across all platforms.
以下说明了它,并且该行为在所有平台上都是可移植的。
回答by wirm
It tells the compiler to use functions, structures, macros and etc from file sdtio.h, which represents a part of glibc(or whatever is the standart C library you got). Compiler also adds record to the output executable "to-link list", that it should be linked to standart C library.
它告诉编译器使用文件 sdtio.h 中的函数、结构、宏等,它代表 glibc 的一部分(或任何你得到的标准 C 库)。编译器还将记录添加到输出可执行文件“链接列表”中,即它应该链接到标准 C 库。
回答by coco97
Preprocessor directives in a source code are the statements which are processed before the compilation of a program, after this step the source code is converted into the expanded source code as it now contains the references to the functions which are already defined in the Standard C Library(or any other) like printf, scanf, putw, getchar etc. The stdio.h is a file with ".h" extension that contains the prototypes (not definition) of standard input-output functions used in c.
源代码中的预处理器指令是在程序编译之前处理的语句,在此步骤之后,源代码被转换为扩展的源代码,因为它现在包含对标准 C 库中已经定义的函数的引用(或任何其他),如 printf、scanf、putw、getchar 等。 stdio.h 是一个带有“.h”扩展名的文件,它包含 c 中使用的标准输入输出函数的原型(不是定义)。

