#undef in C

Amansingh Javatpoint
2 min readJul 23, 2021

In the C programming language, the #undef directive is used to undefine the macro or any constant, which will be defined by the preprocessor directive #define. The #undef preprocessor directive will inform the compiler to remove the definitions which have been specified for the macro. The macro can be redefined once it has been removed using the #undef directive. Once the macro has been undefined using the #undef directive, then the directive #ifdef will be evaluated to false on that macro.

In other words, the #undef directive can be defined as a directive which removes all the declarations for that particular macro name or any constant which is defined using the #define directive. Before the compiler compiles the C program, the main code will be processed; thus, the process is called preprocessing.

All the commands used for this preprocessor directive are defined using the “#” symbol. Once the constant is declared using the directive #define, it will be limited to its cope using #undef directive in an extensive program.

Any program or application consisting of many macros becomes hard to understand. It becomes even more complicated when the set of macros that are defined are not stable. Hence, during such times, the #undef directive will decrease the readability of the macros.

Therefore, well defined use of #undef directive improves readability and is more economical. This rule raises an issue when a #undef undefines a macro that was defined in another file. It will also raise an issue for an #undef directive that tries to undefine a non-existing macro.

--

--