“ifndef”和“ifndef”之间的区别和“如果!已定义”在C语言中?
我在同一个 C 源文件中看到了 #ifndef ABC
和 #if !define (ABC)
。
它们之间有细微的差别吗? (如果是风格问题,为什么有人会在同一个文件中使用它们)
I have seen #ifndef ABC
and #if !defined (ABC)
in the same C source file.
Is there subtle difference between them?
(If it is a matter of style, why would someone use them in the same file)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
不,这样使用时两者没有区别。当初始
#if
或后续#elif
条件之一需要更复杂的测试时,后一种形式(使用define()
)非常有用。#ifdef
仍然有效,但在这种情况下使用#if Defined()
可能会更清晰。例如,是否需要测试是否定义了多个宏,或者是否等于某个特定值。差异(在文件中使用两者)可能取决于使用中的具体细微差别,如上所述,或者只是由于不一致而导致的不良实践。
No, there's no difference between the two when used that way. The latter form (using
defined()
) is useful when the initial#if
or one of the subsequent#elif
conditions needs a more complex test.#ifdef
will still work, but it might be clearer using#if defined()
in that case. For example, if it needs to test if more than one macro is defined, or if it equals a specific value.The variance (using both in a file) could depend on specific subtleties in usage, as mentioned above, or just poor practice, by being inconsistent.
在您给出的上下文中,它们是相同的:您只是检查一个宏标识符是否存在。
但是,#if 形式允许您计算表达式,这可能很有用。
In the context you gave, they are the same: you are just checking for the existence of one macro identifier.
However, the #if form allows you to evaluate expressions, which can be useful.