After reading this preprocessor topic, you will understand its theory and examples also you will able to implement it in C programming.
The diagram below will tell you in an easier way about C preprocessor,
The preprocessor executes processing of C program source code before compiled. This processing done by using some preprocessor directive. The two most commonly used preprocessor directives are
- #include
- #define
#include
This directive used to include any header file into our program.
The general form of #include directive is given below,
#include<headerfile_name.h>
For example:
#include<stdio.h> #include<conio.h>
#define
The general form of #define directive is given below,
#define name constant_value
where, the name generally written in capital letters and we can write name anywhere in the program. When the program executes name replaced by constant_value.
For example:
#define PI 3.14
Indicate that
- name is PI.
- constant_value is 3.14.
Example:
Program (1): To find the circumference of a circle.
#include<stdio.h> // #include directive to include stdio.h header file in program. #include<conio.h> // #include directive to include conio.h header file in program. #define PI 3.14 // #define directive to replace PI by 3.14 in program. void main(void) { float r,c; printf("Enter radius of circle : "); scanf("%f",&r); c=2*PI*r; printf("The circumference of circle is %f",c); getch(); }
Output (1)
Enter radius of circle : 3 The circumference of circle is 18.840000