In C Language, the "#undef" directive is used to "undefine" a previously defined macro.
This allows us to remove a macro definition within the scope of our program.
It's useful when we need to change the behaviour of macros in different parts of our code.
#undef identifier
#undef syntax is used to declared `undef` macro in code, where "identifier" is the name of the macro we want to remove.
#include <stdio.h> #define PI 3.14159 int main() { printf("Value of PI: %f\n", PI); #undef PI #ifdef PI printf("Value of PI after undefining: %f\n", PI); #else printf("PI is not defined anymore.\n"); #endif return 0; }
Value of PI: 3.141590 PI is not defined anymore.
we first define a macro "PI" with the value 3.14159. Then, we print its value. After that, we "#undef" the macro PI.
Finally, we check if "PI" is still defined. Since we have undefined it, the "#ifdef" block won't execute, and it will print "PI is not defined anymore."
"#undef" can be useful in situations where we want to remove or redefine a macro within a specific scope of your program.
However, use it with caution, as removing a macro may have unintended consequences on the behaviour of your code.
#include <stdio.h> #define PI 3.14159 int main() { printf("Value of PI: %f\n", PI); #undef PI #define PI 3.14 printf("Value of PI after redefining: %f\n", PI); return 0; }
Value of PI: 3.141590 Value of PI after redefining: 3.140000
we first define the macro PI with the value 3.14159.
Then, we "#undefine" PI and "redefine" it with a different value "3.14".
This demonstrates how we can redefine a macro after removing its previous definition.