#ifdef #else #endif宏问题

发布于 2024-07-27 01:43:40 字数 495 浏览 3 评论 0原文

我是 C 新手,我正在维护某人的代码。 我在头文件中发现了这个。 我可以理解,如果源代码是在Windows上编译的,它将进入if语句,否则如果代码是在Linux上编译的,它将进入else语句。 如果我错了请纠正我。

但是,问题是为什么所有包含头前面都使用#(哈希)?

非常感谢您的任何建议,

#ifdef WIN32
#   include <conio.h>
#   include <process.h>
#   include <stdlib.h>
#   include <string.h>
#else
#   include <unistd.h>
#   include <termio.h>
#   include <sys/types.h>
#   include <sys/stat.h>
#   include <fcntl.h>
#endif

I am new to C, and I am maintaining someones code. I came across this in the header file. I can understand that if the source is compiled on the windows it will enter the if statement else if the code is compiled on a linux it will enter the else statement. Correct me if I am wrong.

However, the question is why is # (hash) used in front of all the include headers?

Many thanks for any suggestions,

#ifdef WIN32
#   include <conio.h>
#   include <process.h>
#   include <stdlib.h>
#   include <string.h>
#else
#   include <unistd.h>
#   include <termio.h>
#   include <sys/types.h>
#   include <sys/stat.h>
#   include <fcntl.h>
#endif

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

标点 2024-08-03 01:43:40

散列 (#) 表示预处理器指令。 预处理器在编译之前运行代码,并根据所有以“#”开头的行执行操作。 “#include filename.h”指令实质上复制了 filename.h 的所有内容并将其粘贴到“#include filename.h”行所在的位置。

The hash (#) indicates a preprocessor directive. The preprocessor runs over the code before compilation and does things depending on all the lines beginning with "#". The "#include filename.h" directive essentially copies all the contents of filename.h and pastes it where the "#include filename.h" line was.

星星的軌跡 2024-08-03 01:43:40

#include 是在 C 中包含文件的方式。

您可能会对 # 和 include 之间的空格感到困惑。

但它们并不重要。 这些行仍然是#include。

#include is the way you include files in C.

You might be confused by the spaces between the # and the include.

But they don't matter. These lines are still #include.

最美不过初阳 2024-08-03 01:43:40

因为“#include”是告诉预处理器包含标头的语法。 井号后面的空格只是用于格式化,并不是绝对必要的。

Because "#include" is the syntax for tell the preprocessor to include a header. The spaces after the pound are just there for formatting and are not strictly necessary.

于我来说 2024-08-03 01:43:40

# 行实际上不是由 C 编译器本身处理,而是由作为编译管道早期阶段运行的预处理器处理。 “#”是它如何知道它负责哪一行的方式。

相同的预处理器也可以在其他上下文中使用。

预处理器不仅可以对表达式求值(如 #if#ifdef 子句中所示),还可以打开其他文件并使用 #include 插入它们 甚至使用 #define 子句进行文本替换。

更多信息请参阅关于 C 预处理器的维基百科条目

#include 不同于 VB.Net Imports 语句或 C# using 语句。 这些引用其他类,但#include实际上将包含文件的文本插入到源文件中的该位置。 而且它可以递归地运行,因此包含的文件本身可以是#include,也可以是其他文件。

The # lines are actually handled not by the C compiler itself, but by a preprocessor that runs as an early stage in the compilation pipeline. The "#" is how it knows which lines it is responsible for.

That same preprocessor can be used in other contexts as well.

The preprocessor can not only do evaluation of expression, as in the #if and #ifdef clauses, but it can also open other files and insert them using #include and even do text substitution using #define clauses.

More information can be found in the Wikipedia entry on the C preprocessor.

#include is different from, say, the VB.Net Imports statement or the C# using statement. Those make references to other classes, but #include actually inserts the text of the included file at that location in the source file. And it can act recursively, so that included files may themselves #include still others.

可遇━不可求 2024-08-03 01:43:40

#include 指令告诉预处理器处理指定文件的内容,就好像这些内容已出现在该指令出现的源程序中一样。

http://msdn.microsoft.com/en-us /library/36k2cdd4(VS.80).aspx

The #include directive tells the preprocessor to treat the contents of a specified file as if those contents had appeared in the source program at the point where the directive appears.

http://msdn.microsoft.com/en-us/library/36k2cdd4(VS.80).aspx

黎夕旧梦 2024-08-03 01:43:40

includeifdef 等都是预处理器指令,因此它们必须前面有井号(或哈希)字符。 编写此代码的程序员只是将所有这些 # 字符排列在左侧,以使代码看起来更干净(在他看来)。

cplusplus.com 对预处理器指令有很好的概述

include, ifdef, etc. Are all preprocessor directives, so they must have the pound (or hash) character in front of them. The coder who wrote this code simply lined up all of those # characters on the left side to make to code look cleaner (in his opinion).

cplusplus.com has a good overview of preprocessor directives.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文