# C preprocessors

In this tutorial, you will learn about the preprocessor in C. Preprocessor is a very exciting feature of C programming that instructs the compiler to do required preprocessing before the actual compilation. The preprocessor is a program that processes the code before it passes through the compiler. It operates under the control of the preprocessor command lines and directives. We place the preprocessor directives in the source program before the mainline before the source code passes through the compiler. The preprocessor examines the source program for any preprocessor directives. If there are any preprocessor directives, then appropriate actions are taken and then the source program is handed over to the compiler. Preprocessor directives follow the special syntax rules and begin with the ‘#’  symbol in column 1. Preprocessors do not require any semicolon in the end. The preprocessor begins with ‘#’ always before the main of the program.

Syntax:

# define Identifier string

For example,

# define PI 3.14

The C preprocessor allows us to define macros. Macro substitution is a process where an identifier in a program is replaced by a predefined string. The table below lists all the preprocessor directives.

S.No.DirectivesDescription
1#definedefines a preprocessor macro.
2#includeInserts a particular header from another file.
3#undefUndefines a preprocessor macro.
4#ifdef will return true if this macro is defined.
5#ifndefwill return true if this macro is not defined.
6#ifChecks if a compile time condition is true.
7#elseis an alternative for #if.
8#elifThe combination of #else and #if statement.
9#endifEnds preprocessor.
10#errorPrints error message and aborts compilation.
11#pragmaIssues special commands to the compiler, using a standardized method.

## Example of preprocessor:

Below is a program implementing and illustrating the use of preprocessor. Here, the preprocessor replaces the identifier with its equivalent string. This feature will enable the access of the value of the identifier from anywhere within the program.

#include<stdio.h>
#define PI 3.14
int main()
{
int r, A;
printf("Enter the values of the radius of the circle: ");
scanf("%d", &r);
A = PI * r * r;
printf("Area of the circle is: %d", A);
}

The above code will generate the output as below.

Output:

Enter the values of the radius of the circle: 7
Area of the circle is: 153

We can also define the macros for replacing the function in our program same as the way we did for replacing the identifiers its equivalent string.  The program execution with macros is faster than that with the functions. Macros are often preferred in place of short functions. Let us replace the function in the program above with function like macros.

For example,

#define areaCircle(r) (3.1415  * r * r)
The following example will help you to understand the detailed implementation of function like macros.
	#include <stdio.h>
#define PI 3.1415
#define areaCircle(r) (PI*r*r)

int main()
{
int r;
float area;

scanf("%d", &r);
area = areaCircle(r);
printf("Area = %f", area);
return 0;
}

The above program will display its output as below.

Output: