How do you override a defined macro?
You can use the #undef preprocessor directive to undefine (override) a previously defined macro. Many
programmers like to ensure that their applications are using their own terms when defining symbols such
as TRUE and FALSE. Your program can check to see whether these symbols have been defined already, and if
they have, you can override them with your own definitions of TRUE and FALSE. The following portion of
code shows how this task can be accomplished:
...
#ifdef TRUE /* Check to see if TRUE has been defined yet */
#undef TRUE /* If so, undefine it */
#endif
#define TRUE 1 /* Define TRUE the way we want it defined */
#ifdef FALSE /* Check to see if FALSE has been defined yet */
#undef FALSE /* If so, undefine it */
#endif
#define FALSE !TRUE /* Define FALSE the way we want it defined */
...
In the preceding example, the symbols TRUE and FALSE are checked to see whether they have been defined
programmers like to ensure that their applications are using their own terms when defining symbols such
as TRUE and FALSE. Your program can check to see whether these symbols have been defined already, and if
they have, you can override them with your own definitions of TRUE and FALSE. The following portion of
code shows how this task can be accomplished:
...
#ifdef TRUE /* Check to see if TRUE has been defined yet */
#undef TRUE /* If so, undefine it */
#endif
#define TRUE 1 /* Define TRUE the way we want it defined */
#ifdef FALSE /* Check to see if FALSE has been defined yet */
#undef FALSE /* If so, undefine it */
#endif
#define FALSE !TRUE /* Define FALSE the way we want it defined */
...
In the preceding example, the symbols TRUE and FALSE are checked to see whether they have been defined
yet. If so, they are undefined, or overridden, using the #undef preprocessor directive, and they are redefined
in the desired manner. If you were to eliminate the #undef statements in the preceding example, the compiler
would warn you that you have multiple definitions of the same symbol. By using this technique, you can
avoid this warning and ensure that your programs are using valid symbol definitions.
avoid this warning and ensure that your programs are using valid symbol definitions.
Cross Reference:
V.1: What is a macro, and how do you use it?
V.10: Is it better to use a macro or a function?
V.16: What is the concatenation operator?
V.17: How can type-insensitive macros be created?
V.18: What are the standard predefined macros?
V.31: How do you override a defined macro?
No comments:
Post a Comment