C Programming Tutorial

C Programming Tutorial

#undef in C

#undef in c is a preprocessor directive used to undefine or remove the definition of a previously defined macro in a C# program. This means that after the #undef directive is used, the macro is no longer available for use in the program.

Here’s an example of how to use #undef:

#define PI 3.14159 // define a macro PI
Console.WriteLine($"PI = {PI}"); // output: PI = 3.14159
#undef PI // undefine the macro PI
//Console.WriteLine($"PI = {PI}"); // this line would cause a compilation error

In this example, we first define a macro named PI and give it the value 3.14159. We then use the Console.WriteLine method to output the value of PI to the console. The output is “PI = 3.14159”.

After that, we use the #undef directive to undefine the macro PI. If we attempt to use the macro PI again in the program, such as in the commented-out line, it will cause a compilation error.

Output:

PI = 3.14159

Note that in this example, we have commented out the line that attempts to use the macro PI after it has been undefined. If we were to uncomment that line and try to compile the program, we would get a compilation error because the PI macro is no longer defined.

C program that demonstrates the use of the #undef preprocessor directive:

#include <stdio.h>#define MAX_VALUE 100 // define a macro MAX_VALUEint main() {
    printf("MAX_VALUE is %d\\n", MAX_VALUE); // output: MAX_VALUE is 100
    #undef MAX_VALUE // undefine the macro MAX_VALUE//printf("MAX_VALUE is %d\\n", MAX_VALUE); // this line would cause a compilation error
    return 0;
}

In this program, we first define a macro named MAX_VALUE and give it the value 100. We then use the printf function to output the value of MAX_VALUE to the console. The output is “MAX_VALUE is 100”.

After that, we use the #undef directive to undefine the macro MAX_VALUE. If we attempt to use the macro MAX_VALUE again in the program, such as in the commented-out line, it will cause a compilation error.

Output:

MAX_VALUE is 100

Note that in this example, we have commented out the line that attempts to use the macro MAX_VALUE after it has been undefined. If we were to uncomment that line and try to compile the program, we would get a compilation error because the MAX_VALUE macro is no longer defined.

Share This Article