Monday, 7 November 2011

How can you check to see whether a symbol is defined? in C programming

How can you check to see whether a symbol is defined?

You can use the #ifdef and #ifndef preprocessor directives to check whether a symbol has been defined
(#ifdef) or whether it has not been defined (#ifndef). Many programmers like to ensure that their own
version of NULL is defined, not someone else’s. This task can be accomplished as shown here:

#ifdef NULL
#undef NULL
#endif
#define NULL (void*) 0
The first line, #ifdef NULL, checks to see whether the NULL symbol has been defined. If so, it is undefined
using #undef NULL (see FAQ V.31), and the new definition of NULL is defined.

To check whether a symbol has not been defined yet, you would use the #ifndef preprocessor directive. See
FAQ V.3 for an example of how you can use #ifndef to determine whether you have already included a
particular header file in your program.

Cross Reference:

V.3: How can you avoid including a header more than once?
V.8: How are portions of a program disabled in demo versions?

No comments:

Post a Comment