Preprocessor Directives in C
Preprocessor directive is a text substitution tool that instruct the compiler to pre-processor our program before its actual compilation. Each C preprocessor starts with # symbol. Generally they are defined at the beginning of the program just after the header files. But you can define them anywhere in the program.
Types of Preprocessor Directives in C
There are four types of preprocessor directives which are given below.
1. Macros
2. File inclusion
3. Conditional compilation
4. Miscellaneous directives
Macros
Macros are generally used to give a common name to the values which are used many times in a program. Or we can say that, macros are used to define symbolic constants. Consider below program to understand it.
//C program to calculate area of circle #include <stdio.h> #define PI 3.14 //macro int main() { float r, a; printf("Enter radius of the circlen"); scanf("%f",&r); a=PI*r*r; printf("Area of the circle is %f",a); return 0; }
Output
Points to remember
- A macros always start with #define. In our program #define PI 3.14 is the macros definition. Wherever PI is encountered in the program it is replaced with 3.14.
- In the compilation of the program all the macros definition replaced with the values which are used with #define. It is done in the process when source code is converted into expanded source code.
- Do not add semicolon at the end.
- Generally macros are written in single line. If you want to write it in multiple lines then you have to use macro continuation operator i.e. . An example for this is given below.
Examples
#define OR ||
#define CHECK (a>10 && a<20)
#define MSG
printf(“It is working fine”)
Macros with arguments
Macros with arguments are similar to functions with arguments. Consider the below program to understand its working.
//C program to calculate perimeter of circle #include<stdio.h> #define PERI(x) (2*3.14*x) //macro with argument int main() { float r,per; printf("Enter radius of the circlen"); scanf("%f",&r); per=PERI(r); printf("Perimeter of the circle is %f",per); return 0; }
Output
Points to remember
- In the above program I have used macros with argument with the statement #define PERI(x) (2*3.14*x)
- Be careful while defining macros with arguments. In above example there must be no space between PERI and (x). The body of the macro should be placed in parentheses.
- Whenever the macro calling is done in our program, its calling statement in replaced by its body.
- They are fast as compared to functions because control do not goes to macro definition, the whole body comes at the place where calling is done.
File Inclusion
As its name suggests this preprocessor directive is used to include all the content of file to be included in the source code. Till now we have used preprocessor directives like #include<stdio.h> which includes all the contents from the header file stdio.h. These are predefined header files, you can also make your own and then include it in your program.
Examples
#include<math.h>
#include<stdlib.h>
Its certainly a good question. It is not a good programming practice to write full program into a single file. While writing a very complex program it is recommend to break the program into files and include them in the beginning. Sometimes the macros of the program is also quite large which can only be stored in some files. So it is basically used for better management of the C program.