Updated June 30, 2023
Introduction to #undef in C
Undef is a directive in C programming language that helps to remove all the definitions for the given macro name or any constant defined using the #define directive. The #undef directive is a crucial component of the preprocessor directive in C programming. The compiler automatically invokes it before the actual compilation commences. The preprocessing phase, which occurs before compilation, involves processing the source code. The execution of preprocessor directives characterizes this preprocessing stage.
Syntax
A preprocessor compiler is responsible for processing the code by executing preprocessor directives. The preprocessor is that part of the compiler which executes essential operations in the given code before the compiler actually compiles it. The transformations performed by the preprocessors are lexical, which tells that the output of the preprocessor is in text form.
To define a macro, we use the below syntax
#define macro_name
Eg: #define PI 3.14
When the given line is processed by the preprocessor, it assigns the value 3.14 to the variable named PI. Further, if we need to limit the scope for this macro_name within the program, we can use the #undef directive to remove the declared macro_name for further assignments.
#undef macro_name
- Here macro_name refers to the name of the variable that we defined earlier and needs to be removed.
- The ‘#’ symbol in a C preprocessor directive indicates that it is a directive to be processed by the preprocessor.
How does #undef work in C?
- The source code written by the user is first sent for preprocessing to the preprocessor, which generates an expanded source file with the same name as the programs. The compiler analyzes the expanded file and generates object code for the library functions used in the program.
- One can also use the #ifdef ..#endIf directive that helps to check if a particular macro name exists or not; otherwise, if we access a macro name on which #undef has already been run compile-time error is thrown, as we can see,, in below, example -2.
Types of Preprocessor
- Macros
- File Inclusion
- Conditional Compilation
- Other directives
Examples to Implement #undef in C
Below are the examples mentioned:
Example #1
Let’s see what happens when we declare a num variable with value 7 and then undefine it using the undef directive. Then again, define it with value 10 and see how the value of variable square and square2 varies.
Code:
#include <stdio.h>
#define num 7
int square1=num*num;
#undef num
#define num 10
int square2=num*num;
int main() {
printf("Value of square with first value of num variable is = %d",square1);
printf("\n");
printf("Value of square with second value of num variable is = %d",square2);
return 0;
}
Output:
Example #2
In this example, we will see what happens when one tries to access a constant or macro defined using #define but has been removed using #undef directive.
Code:
#include <stdio.h>
#define num 7
int square1=num1*num1;
#undef num1
int main() {
printf("Value of constant num that has been removed using #undef directive = %d",num1);
return 0;
}
Output:
Example #3
In this example, we will see how to implement define and undef directives for declaring a macro name and constant in one’s program. We will use the #ifdef directive to check if a particular macro exists or not and handle the situations such as failing due to a call to non-existing macros.
Code:
#include <stdio.h>
#define StudentId 12
#undef StudentId
int main()
{
#ifdef StudentId
printf("Student with roll_no %d exists \n", StudentId);
#endif
printf("Learning preprocessor directives is fun\n");
return 0;
}
Output:
Example #4
This is another cause for the above example where the #ifdef directive returns true, and the statement gets executed.
Code:
#include <stdio.h>
#define StudentId 12
//#undef StudentId
int main()
{
#ifdef StudentId
printf("Student with roll_no %d exists \n", StudentId);
#endif
printf("Learning preprocessor directives is fun\n");
return 0;
}
Output:
Conclusion
When working with preprocessor directives in a large C program, #define directives are used to define constants and macros. These directives allow us to define various constants that can be used throughout the program. Programmers use the #undef directive to redefine a macro_name by modifying its definition or removing it completely.
Recommended Articles
This is a guide to #undef in C. Here we discuss an introduction to #undef in C, syntax, and how it works with examples. You can also go through our other related articles to learn more –