C语言 为什么我的讲师编写的所有 C 文件的第一行都以单个 # 开头?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/45629176/
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
Why do all the C files written by my lecturer start with a single # on the first line?
提问by The Main Man
I'm going through some C course notes, and everyC program source file begins with a single #on the first line of the program.
我正在阅读一些 C 课程笔记,每个C 程序源文件都以程序#第一行的一个单一开头。
Then there are blank lines, and following that other stuff followed by the mainfunction.
然后是空行,然后是其他内容,然后是main函数。
What is the reason for the #?
的原因是#什么?
(It's out of term now and I can't really ask the chap.)
(现在已经过时了,我真的不能问这个人。)
Here's an example:
下面是一个例子:
#
#include <stdio.h>
int main() {
printf("Hello, World!");
return 0;
}
回答by Bathsheba
Wow, this requirement goes way backto the 1970s.
哇,这个要求可以追溯到1970 年代。
In the very earlydays of pre-standardised C, if you wanted to invoke the preprocessor, then you had to write a #as the first thing in the first line of a source file. Writing onlya #at the top of the file affords flexibility in the placement of the other preprocessor directives.
在预标准化 C 的早期,如果您想调用preprocessor,那么您必须#在源文件的第一行中编写 a作为第一件事。只#在文件顶部写入a可以灵活地放置其他预处理器指令。
From an original C draft by the great Dennis Ritchie himself:
来自伟大的丹尼斯·里奇 (Dennis Ritchie) 本人的原始 C 草稿:
12. Compiler control lines
[...] In order to cause [the] preprocessor to be invoked, it is necessary that the very first line of the program begin with #. Since null lines are ignored by the preprocessor, this line need contain no other information.
12. 编译器控制线
[...] 为了使[该]预处理器被调用,程序的第一行必须以#开头。由于预处理器会忽略空行,因此该行不需要包含其他信息。
That document makes for great reading (and allowed me to jump on this question like a mad cat).
该文件非常适合阅读(并让我像疯猫一样跳到这个问题上)。
I suspect it's the lecturer simply being sentimental - it hasn't been required certainly since ANSI C.
我怀疑这是讲师只是多愁善感 - 自 ANSI C 以来就没有必要这样做了。
回答by Minhas Kamal
Does Nothing
什么也没做
As of the ISO standard of C/C++:
从 C/C++ 的 ISO 标准开始:
A preprocessing directive of the form
# new-linehas no effect.
形式的预处理指令
# new-line没有效果。
So in today's compilers, that empty hash does not do anything(like- new-line ;has no functionality).
所以在今天的编译器中,那个空散列不做任何事情(就像 -new-line ;没有功能)。
PS: In pre-standardised C, # new-linehad an important role, it was used to invoke the C Pre-Processor (as pointed out by @Bathsheba). So, the code here was either written within that time period, or came from habit.
PS:在预先标准化的 C 中,# new-line具有重要作用,它用于调用 C 预处理器(如@Bathsheba 所指出的)。所以,这里的代码要么是在那个时间段内编写的,要么是出于习惯。
回答by GOBI
You need to know about the Compilation process of C. Because that is "must know" how the Source code converting into Executable binary code (file).
您需要了解 C 的编译过程。因为这是“必须知道”源代码如何转换为可执行二进制代码(文件)的过程。
From the Compilation Process, the C source code has to Cross the pre-processor Section. But how to tell the Compiler to pre-process the code?... That the time # Symbol was introduced to the indicator of Preprocess to the compiler.
从编译过程开始,C 源代码必须跨越预处理器部分。但是如何告诉 Compiler 对代码进行预处理?... 那个时候#Symbol 被引入到 Preprocess 的指标给编译器。
For Example #define PI 3.141is in the Source code. Then it will be change after the Preprocessing session. Means, all the PI will be changed into 3.141.
例如#define PI 3.141在源代码中。然后它将在预处理会话后更改。意味着,所有的 PI 都会变成 3.141。
This like #include <stdio.h>, the standard I/O Functions will be added into your Source code.
就像这样#include <stdio.h>,标准的 I/O 函数将被添加到您的源代码中。
If you have a Linux machine, compile like gcc -save-temps source_code.c. And see the compiler outputs.
如果您有一台 Linux 机器,请像gcc -save-temps source_code.c. 并查看编译器输出。

