Programming in C: Unit I (d): Preprocessor Directives

#undef

with Example C Programs | Preprocessor Directives

As the name suggests, the #undef directive undefines or removes a macro name previously created with #define. Undefining a macro means to cancel its definition.

#UNDEF

As the name suggests, the #undef directive undefines or removes a macro name previously created with #define. Undefining a macro means to cancel its definition. This is done by writing #undef followed by the macro name that has to be undefined.

Like definition, undefinition also occurs at a specific point in the source file, and it applies starting from that point. Once a macro name is undefined, the name of the macro ceases to exist (from the point of undefinition) and the preprocessor directive behaves as if it had never been a macro name.

Therefore, the #undef directive removes the current definition of macro and all subsequent occurrences of macro name are ignored by the preprocessor.

Note

If you had earlier defined a macro with parameters, then when undefining that macro you do not have to give the parameter list. Simply specify the name of the macro.

You can also apply the #undef directive to a macro name that has not been previously defined. This can be done to ensure that the macro name is undefined.

The #undef directive when paired with a #define directive creates a region in a source program in which the macro has a special meaning. For example, a specific function of the source program can use certain constants to define environment-specific values that do not affect the rest of the program.

The #undef directive can also be paired with the #if directive to control conditional compilation of the source program. We will read about the #if directive later in this section.

Consider the following example in which the #undef directive removes definitions of a symbolic constant and a macro.

#define MAX 10

#define MIN (X, Y) (((X) < (Y)) ? ((X): (Y))

.

.

.

#undef MAX

#undef MIN

Programming in C: Unit I (d): Preprocessor Directives : Tag: : with Example C Programs | Preprocessor Directives - #undef