#define vs #undef in C language
In this article, we will discuss the difference between #define and #undef pre-processor in C language.
- Pre-processor is a program that performs before the compilation.
- It only notices the # started statement.
- # is called preprocessor directive.
- Each preprocessing directive must be on its own line.
- The word after # is called the preprocessor command.
The #define directive defines an identifier and a character sequence (a set of characters) that will be substituted for the identifier each time it is encountered in the source file.
#define macro-name char-sequence.
The identifier is referred to as a macro name and replacement process as a macro replacement.
#define PI 3.14
Here. PI is the macro-name and 3.14 is the char-sequence.
Below is the C program illustrating the use of #define:
area of circle is 50.240002
- In this example, PI is the macro-name and the char-sequence is 3.14.
- When the program runs the compiler will check the #define command first and assign the PI as 3.14.
- Now in the entire program wherever the compiler sees the PI word it will replace it with 3.14.
Below is the C program printing product of two numbers using #define:
product of a and b is 12
- In this example, a macro-name as the product is defined and passes two arguments as a and b and gives the char-sequence as the product of these two arguments.
- When the compiler sees the macro-name in the print statement, it replaces the macro-name with the product of a and b and gives the answer as their product.
The #undef preprocessor directive is used to undefined macros.
Below is the C program to illustrate the use of #undef in a program:
Explanation: In this example, when #undef is used, then it will delete the #define command and the macro will get undefined and the compiler will show the error.