#undef in C
×


#undef in C

30

The #undef directive in C removes the definition of a macro that was previously defined.

This directive removes the definition of a macro, allowing you to use the macro name later in the code without conflict.

Syntax of #undef in C 

#undef macro_name

#undef: this is the preprocessor directive used to undefine a macro.

macro_name: This is the name of the macro that you want to undefine.

Example:

// Program for #undef in C
#include<stdio.h>

#define PI 3.14159 // Define PI macro

int main() {
    printf("Value of PI: %f\n", PI);

    #undef PI // Undefine PI macro

    #ifdef PI
        printf("PI is defined.\n");
    #else
        printf("PI is not defined.\n");
    #endif

    return 0;
}
		

Output:

Value of PI: 3.141590 
PI is not defined.

Uses of #define in C

Avoid Macro Redefinition: If you have defined a macro and later in the code you want to redefine it with a different value or behavior, you can use #undef to undefine the macro first.

Conditional Compilation: #undef can be used with conditional compilation directives like #ifdef, #ifndef, #if, #elif, and #endif to control the compilation of specific parts of the code based on whether a macro is defined or not.

Macro Management: it helps in managing macros effectively, preventing potential naming conflicts, and ensuring that are used correctly throughout the codebase.

Remember, using #undef removes the definition of a macro completely.

After #undef, the macro name can be redefined if needed without any issues.



Best WordPress Hosting


Share:


Discount Coupons

Get a .COM for just $6.98

Secure Domain for a Mini Price



Leave a Reply


Comments
    Waiting for your comments