The #undef Directive

The #undef directive removes (undefines) a macro name previously created with #define.

Syntax

control-line : #undef identifier new-line

The #undef directive removes the current definition of identifier. Consequently, subsequent occurrences of identifier are ignored by the preprocessor. To remove a macro definition using #undef, give only the macro identifier; do not give a parameter list.

You can also apply the #undef directive to an identifier that has no previous definition. This ensures that the identifier is undefined. Macro replacement is not performed within #undef directives.

The #undef directive is typically paired with a #define directive to create a region in a source program in which an identifier has a special meaning. For example, a specific function of the source program can use manifest constants to define environment-specific values that do not affect the rest of the program. The #undef directive also works with the #if directive (see “The #if, #elif, #else, and #endif Directives”) to control conditional compilation of the source program.

#define WIDTH 80

#define ADD( X, Y ) (X) + (Y)

.

.

.

#undef WIDTH

#undef ADD

In this example, the #undef directive removes definitions of a manifest constant and a macro. Note that only the identifier of the macro is given.