difference between "ifndef" and "if !defined" in C?
Asked Answered
P

2

53

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)

Purpose answered 23/12, 2011 at 15:27 Comment(0)
M
47

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.

Minion answered 23/12, 2011 at 15:30 Comment(0)
C
8

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.

Chansoo answered 23/12, 2011 at 15:32 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.