#if 0 ..... #endif 块到底有什么作用?
在 C/C++ 中,
放置在 #if 0
/#endif
块之间的代码会发生什么情况?
#if 0
//Code goes here
#endif
代码是否只是被跳过并因此不被执行?
In C/C++
What happens to code placed between an #if 0
/#endif
block?
#if 0
//Code goes here
#endif
Does the code simply get skipped and therefore does not get executed?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
它不仅没有被执行,甚至没有被编译。
#if
是一个预处理器命令,它在实际编译步骤之前进行评估。该块内的代码不会出现在编译的二进制文件中。它通常用于暂时删除代码段,以便稍后将其重新打开。
Not only does it not get executed, it doesn't even get compiled.
#if
is a preprocessor command, which gets evaluated before the actual compilation step. The code inside that block doesn't appear in the compiled binary.It's often used for temporarily removing segments of code with the intention of turning them back on later.
它与注释掉块相同,除了一个重要的区别:嵌套不是问题。考虑这段代码:
如果我想将其注释掉,我可能会尝试:
Bzzt。语法错误!为什么?因为块注释不嵌套,所以(正如您从 SO 的语法突出显示中看到的那样)单词“NULL”之后的
*/
终止注释,从而进行baz
调用没有注释掉,并且baz
后面的*/
是语法错误。另一方面:可以注释掉整个事情。并且
#if 0
将相互嵌套,如下所示:当然,如果注释不正确,这可能会有点令人困惑,并且会成为维护难题。
It's identical to commenting out the block, except with one important difference: Nesting is not a problem. Consider this code:
If I want to comment it out, I might try:
Bzzt. Syntax error! Why? Because block comments do not nest, and so (as you can see from SO's syntax highlighting) the
*/
after the word "NULL" terminates the comment, making thebaz
call not commented out, and the*/
afterbaz
a syntax error. On the other hand:Works to comment out the entire thing. And the
#if 0
s will nest with each other, like so:Although of course this can get a bit confusing and become a maintenance headache if not commented properly.
它永久注释掉该代码,因此编译器永远不会编译它。
如果编码员愿意,稍后可以更改 #ifdef 以使该代码在程序中编译。
就像代码不存在一样。
It permanently comments out that code so the compiler will never compile it.
The coder can later change the #ifdef to have that code compile in the program if he wants to.
It's exactly like the code doesn't exist.
我想为
#else
案例添加以下内容:I'd like to add on for the
#else
case:当预处理器看到 #if 时,它会检查下一个标记是否具有非零值。如果是这样,它会为编译器保留代码。如果没有,它就会删除该代码,这样编译器就永远看不到它。
如果有人说#if 0,他们实际上是在注释掉代码,因此它永远不会被编译。你可以认为这就像他们在它周围加上了 /* ... */ 一样。虽然不太一样,但是效果是一样的。
如果你想了解详细的情况,可以经常查看。许多编译器允许您在预处理器运行后查看文件。例如,在 Visual C++ 上,开关 /P 命令将执行预处理器并将结果放入 .i 文件中。
When the preprocessor sees #if it checks whether the next token has a non-zero value. If it does, it keeps the code around for the compiler. If it doesn't, it gets rid of that code so the compiler never sees it.
If someone says #if 0 they are effectively commenting out the code so it will never be compiled. You can think of this the same as if they had put /* ... */ around it. It's not quite the same, but it has the same effect.
If you want to understand what happened in detail, you can often look. Many compilers will allow you to see the files after the preprocessor has run. For example, on Visual C++ the switch /P command will execute the preprocessor and put the results in a .i file.
以
#
开头的行是预处理器指令。#if 0 [...] #endif
块不会进入编译器,并且不会生成任何机器代码。您可以使用源文件
ifdef.cxx
演示预处理器会发生什么情况:运行
gcc -E ifdef.cxx
将显示编译后的内容。您可以选择使用此机制来防止在开发周期期间编译代码块,但您可能不想将其签入源代码管理中,因为它只会增加代码的缺陷并降低可读性。如果它是一段已被注释掉的历史代码,那么应该将其删除:源代码管理包含历史记录,对吧?
另外,对于 C 和 C++ 来说,答案可能是相同的,但是没有一种语言称为 C/C++,引用这种语言也不是一个好习惯。
Lines beginning with a
#
are preprocessor directives.#if 0 [...] #endif
blocks do not make it to the compiler and will generate no machine code.You can demonstrate what happens with the preprocessor with a source file
ifdef.cxx
:Running
gcc -E ifdef.cxx
will show you what gets compiled.You may choose to use this mechanism to prevent a block of code being compiled during the development cycle, but you would probably not want to check it in to your source control as it just adds cruft to your code and reduces readability. If it's a historical piece of code that has been commented out, then it should be removed: source control contains the history, right?
Also, the answer may be the same for both C and C++ but there is no language called C/C++ and it's not a good habit to refer to such a language.
不完全
它显示“tc:4:19:警告:缺少终止'字符”
与海湾合作委员会4.2.4
Not quite
It shows "t.c:4:19: warning: missing terminating ' character"
with gcc 4.2.4
这是一种廉价的注释方式,但我怀疑它可能具有调试潜力。例如,假设您有一个将值输出到文件的构建。您可能不希望在最终版本中出现这种情况,因此您可以使用#if 0...#endif。
另外,我怀疑出于调试目的更好的方法是:
您可以做类似的事情,它可能更有意义,您所要做的就是定义 DEBUG 来查看结果。
It is a cheap way to comment out, but I suspect that it could have debugging potential. For example, let's suppose you have a build that output values to a file. You might not want that in a final version so you can use the #if 0... #endif.
Also, I suspect a better way of doing it for debug purpose would be to do:
You can do something like that and it might make more sense and all you have to do is define DEBUG to see the results.