[英]difference between “ifndef” and “if !defined” in C?
I have seen #ifndef ABC
and #if !defined (ABC)
in the same C source file. 我在同一个C源文件中看到
#ifndef ABC
和#if !defined (ABC)
。
Is there subtle difference between them? 他们之间有细微的差别吗? (If it is a matter of style, why would someone use them in the same file)
(如果是风格问题,为什么有人会在同一个文件中使用它们)
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. 当初始
#if
或后续#elif
条件之一需要更复杂的测试时,后一种形式(使用defined()
)非常有用。 #ifdef
will still work, but it might be clearer using #if defined()
in that case. #ifdef
仍然有效,但在这种情况下使用#if defined()
可能会更清楚。 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. 方差(在文件中使用两者)可能取决于使用中的特定细微差别,如上所述,或者仅仅是不良实践,因为不一致。
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. 但是,#if表单允许您计算表达式,这可能很有用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.