#undef in C

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.

Syntax:

The syntax for the #undef directive is:

Where,

macro: is the name of the macro which the preprocessor will remove.

E.g.:

Output:

The #undef directive is generally used to declare the preprocessor constant or a macro until a limited scope so that the developer can declare the constant once more.

When the #define preprocessor directive is passed with the value of PI being 3.14, the value of that variable can be further used anywhere in the application or the program by the developer.

Further, if one needs to limit the macro scope within the program, they have to undefine that macro using the #undef directive to remove the macro for further purposes.

Again once we undefine the macro, using that name of the variable used earlier, such as we have undefined the macro P I, which has the value 3.14, it will be removed.

Output:

Output:

In this example, the YEARS_OLD macro is first defined with a value of 12 and then undefined using the #undef directive. Since the macro no longer exists, the statement #ifdef YEARS_OLD evaluates to false. This causes the subsequent printf function to be skipped.

  • The developers’ source code will be sent to the preprocessor for the preprocessing, which later gives an expanded code file with the same name as that of the program or an application. That file will be further sent to the compiler for future compilation and generated an object code of the library functions, and the executable file will be generated.
  • The #undef directive command found by the preprocessor recognizes the # symbol, and the preprocessor will check the macro with the same name. Once it matches, that following macro will be eliminated from the memory so that it cannot be used again. If the macro which is already being used is assigned, it throws an error.
  • The user can use the #ifdef…#endif directive to check if the macro they want to use exists or has been deleted.

Pin It on Pinterest

Share This