C Programming

Learning Preprocessor Command #define, #undef in C

There are several preprocessor commands that exist in the C language. # define and # undef are the most important preprocessor command present in the C language. Each of them has some unique features. In this topic, we will discuss in detail # define and # undef.

What is Preprocessor?

Before we discuss # define and # undef, we must know about the preprocessor.

A preprocessor is a program that performs before the compilation. In the source code preprocessor only notices the # symbol started statements. The rest of the lines remains unchanged by the processor. # is called the preprocessor directive. Each processor directive must be on its own line. The word after # is called preprocessor command. # define and # undef are the preprocessor directives.

Processor Command #define in C Language

The # define directive defines an identifier and a character sequence ( a set of characters ) that will be substituted for the identifier each encountered in the source file.

# define PI 3.14.

# is called preprocessor Directive.

Define is called a preprocessor command.

PI is called an identifier or macro.

3.14 are called a sequence of character.

# is called preprocessor directive. It indicates the preprocessor to process or execute the line.

After writing the preprocessor directive, we immediately write the preprocessor command. There are many types of commands present in the C language. Define is one of the processor commands written here. With these commands, the processor can understand what it will be done.

After that, we write a word which is called macro. Here we write a macro which is called PI. It can be written both in the capital or in small letters. Here we write in a small letter.

After using macro, we will immediately write a character sequence. Character sequence may be a number or string. They can be any type depending on the requirement of the programmer. Here we use a character sequence 3.14. In the whole program where we write PI, the preprocessor replaces this PI with 3.14. So we actually set a value of PI once, preprocessor replaces all the PI to 3.14. So when the compiler compiles the program, it can see all the PI values to 3.14.

General Syntax

# define macro-name char-sequence

The identifier is referred to as a macro name and the replacement process as a macro replacement.

Example

# define PI 3.14

# define MSG “Hello Preprocessor”

Programming Example 1

#include <stdio.h>

#define SUM(a,b) a+b

int main ()

{

printf ( “ The sum of 5 and 6 is %d ”, Sum(5,6)) ;

return 0 ;

}

Output

Explanation

Here we use a preprocessor command define. We write a line:

#define SUM(a,b) a+b

“#” is the preprocessor directive. After “#”, we immediately write the define preprocessor command. After that, we write an identifier or a macro named SUM(a,b). Then we write a character sequence a+b.

To see the directive “#,” the preprocessor can understand that it is the line that has to be resolved. By writing the define command, the preprocessor can understand what will be the action performed by the preprocessor. Then the macro SUM(a,b) and a+b preprocessor replace all the macro Sum ( a, b ) with the character sequence a+b. It means when the compiler compiles the program, it can see a+b in place of Sum (a, b ) everywhere it is used. So we write a macro only once, and it can be used multiple times in our program.

Programming Example 2

#include <stdio.h>

#define PRODUCT(a,b) a*b

int main ()

{

printf ( “ The Product of 5 and 6 is %d ”, Product (5+6 , 4-6)) ;

return 0 ;

}

Output

Explanation

Here we use another preprocessor command define. We write a line:

#define PRODUCT(a,b) a*b

“#” is the preprocessor directive. After “#”, we immediately write the define preprocessor command. After that, we write an identifier or a macro named PRODUCT(a,b). Then we write a character sequence a*b.

To see the directive # preprocessor can understand that it is the line that has to be resolved. By writing the define command, the preprocessor can understand what will be the action performed by the preprocessor. Then the macro PRODUCT(a,b) and a*b preprocessor replace all the macro PRODUCT (a,b) with the character sequence a*b. It means when the compiler compiles the program, it can see a*b in the place of PRODUCT (a,b) everywhere it is used. So we write a macro only once, and it can be used multiple times in our program.

The actual mechanism of this particular example is

Product (5+6,5-6)

a = 5 is wrong here.

The correct calculation is:

5+6*5-6

= 5+30-6

= 356

= 29.

So the result is 29.

Programming Example 3

#include <stdio.h>

#define SQUARE(a) a*a

int main ()

{

int s = SQUARE(5)

printf ( “ The Square of is %d ”, s ) ;

return 0 ;

}

Output

Explanation

Here we use another preprocessor command define. We write a line:

#define SQUARE(a) a*a

“#” is the preprocessor directive. After “#”, we immediately write the define preprocessor command. After that, we write an identifier or a macro named SQUARE(a). Then we write a character sequence a*a.

To see the directive # preprocessor can understand that it is the line that has to be resolved. By writing the define command, the preprocessor can understand what will be the action performed by the preprocessor. Then the macro SQUARE(a) and a*a preprocessor replace all the macro Square ( a ) to the character sequence a*a. It means when the compiler compiles the program, it can see a*a in the place of SQUARE(a) everywhere it is used. So we write a macro only once, and it can be used multiple times in our program.

#undef

It is used for undefined macros. Whenever we realize that no more macro is required in the program, we simply write:

#undef macro-name

There is no requirement for any character sequence.

Programming Example 4

#include< stdio.h>

#define AVISHEK avi

int main ()

{

#undef Avi

# if defined(avi)

printf ( “ I am a teacher ”) ;

# else

printf (“ macro has been undefined\n ” ) ;

# endif // defined.

return 0 ;

}

Output

Explanation

Here we define a macro named Avishek, and it replaces by a character sequence avi. It is also undefined by “#undef”. Whenever macro is no longer required in the program, we use the undef preprocessor command to undefine the macro.

Conclusion

Here we learn about the uses of “#” define and # undef in detail. Alternatively, we learn the uses of macro also. How can I apply it, and whenever the macro is no longer required in the program, then undefined it by using “#undef”. So, it is a good concept in the C programming language.

About the author

Bamdeb Ghosh

Bamdeb Ghosh is having hands-on experience in Wireless networking domain.He's an expert in Wireshark capture analysis on Wireless or Wired Networking along with knowledge of Android, Bluetooth, Linux commands and python. Follow his site: wifisharks.com