Pre-processor in C

Definition

The Pre-processor is the one that processes our program before its compilation.

Right now, the definition may look like something that is out of scope but will become clear very soon.

Declaration

The symbol used for pre-processor is ‘#’ (hash) and generally, in programming language ‘#’ is called the preprocessor.

Pre-processor directives begin with a hash symbol. Pre-processor directives are nothing else but preprocessor commands. The most commonly used preprocessor directive is

#include

We have already used this preprocessor directive while writing C programs. The starting of every program is done with a pre-processor directive by writing the statement:

#include<stdio.h>

Here, #include is a preprocessor directive and it makes the header file “stdio.h” available for us. Moreover, it is also used with other header files like #include<conio.h>, etc. Thus, ‘#include’ links the particular header file with the code before compilation.

To define something, we use #define which is also a preprocessor directive.

#define PI 3.14

In the above statement, we have defined that PI is 3.14. So, before compilation we ‘PI’ is encountered anywhere in the program then it will be replaced by 3.14. Thus, anything defined in #define will be replaced with its value before compilation.

It is important to note that whatever the job preprocessor directive is doing, whether it is including a header file or defining a value, is done before compilation. And this thing segregates it from normal variables because normal variables are processed during the compilation.

Let’s make a program to calculate the area of the circle whose value of radius will be entered by the user. We will define the value of ‘PI’ globally with the use of the preprocessor directive ‘#define’.

Program

#include<stdio.h>
#define PI 3.14
int main()
{
int R;
float A;
printf("Enter radius of circle:\n");
scanf("%d",&R);
A=PI*R*R;
printf("Area of the circle is %.2f\n",A);
return 0;
}

 

Output

#define PI 3.14 this statement defines 3.14 as the value of ‘PI’ and in the program wherever we use ‘PI’ it will be replaced by 3.14.

So, A=PI*R*R; will be A=3.14*R*R.

The above program can be done without using ‘#define’ preprocessor directive by just writing the actual value i.e., 3.14 at the place of ‘PI’ but it is important to provide in-depth knowledge of everything we are provided with this C language.

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.