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 #define directive. It is a part of the preprocessor directive since it is called by the compiler automatically before actual compilation starts. Before a C program is compiled by the compiler source code is processed thus this process is called preprocessing. All the commands used for the preprocessor are known as preprocessor directives and all preprocessor directives are defined using #. When a constant is defined using #define one can limit its scope using #undef directive in a large program.
Syntax
Preprocessors are a feature provided in C to process the source code written by the programmer before its actual compilation is done. Before the program is passed through a preprocessor compiler passes the code through the preprocessor where specific instructions such as directives are looked for in the C program known as preprocessor directives that can be easily understood by the preprocessor. These preprocessor directives are must begin with (#) sign.
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 below syntax
#define macro_name
Eg: #define PI 3.14
Thus when the above line is passed to the preprocessor, it assigns 3.14 value to PI variable that can be used further anywhere in the program. Further, in case, 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.
- Here # specifies that it is a preprocessor directive and is compiled using the preprocessor before the actual code is sent for the compilation to the compiler.
How does #undef work in C?
Preprocessors refer to the programs that are processed in our source code even before code enters the compiler for compilation. # under is such a command for the preprocessor.
- The source code written by the user is first sent for preprocessing to the preprocessors which generates an expanded source file with the same name as that of the program. This expanded file is further sent for the compilation to the compiler to generate an object code of the library functions and once this object code is linked to the various library functions being used , an executable ( .exe) file is generated.
- When #undef macro_name command is found by the preprocessor recognized using # symbol preprocessor checks for the macro with the same name. When it finds such a name it removes the macro from the memory so that it can be used again. If a macro being used is already assigned, a compile-time error is thrown.
- One can also use #ifdef ..#endIf directive that helps to check if a particular macro name exist 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
There are various preprocessor directives that can be defined which can be categorized into below 4 main categories:
- 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 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 have 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 we can implement define and undef directive for declaring a macro name and constant in ones program. We will use #ifdef directive to check if a particular macro exist or not and handle the situations such as failing due to 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 #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
While working with preprocessor directives in a large C program one defines constant and macros using #define preprocessor directives to define various constants that can be used but if one needs to reuse a maco_name by changing its definition we can remove them using #undef directives and limit their scopes in a program.
Recommended Articles
This is a guide to #undef in C. Here we discuss an introduction to #undef in C, syntax, how does it work with examples. You can also go through our other related articles to learn more –
3 Online Courses | 5 Hands-on Projects | 34+ Hours | Verifiable Certificate of Completion
4.5
View Course
Related Courses