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.

Loading…

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. Directives Description
1 #define defines a preprocessor macro.
2 #include Inserts a particular header from another file.
3 #undef Undefines a preprocessor macro.
4 #ifdef  will return true if this macro is defined.
5 #ifndef will return true if this macro is not defined.
6 #if Checks if a compile time condition is true.
7 #else is an alternative for #if.
8 #elif The combination of #else and #if statement.
9 #endif Ends preprocessor.
10 #error Prints error message and aborts compilation.
11 #pragma Issues 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
#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 
        #define PI 3.1415
        #define areaCircle(r) (PI*r*r)
        
        int main()
        {
            int r;
            float area;
        
            printf("Enter the radius: ");
            scanf("%d", &r);
            area = areaCircle(r);
            printf("Area = %f", area);
            return 0;
        }

The above program will display its output as below.

Output:

Enter the radius: 7
Area = 153.933502
Loading…