A preprocessor is a set of unique statements that are processed before the compilation process begins.
Preprocessor directives are present in almost every C++ program. To insert header files with the extension .h
, the #include
preprocessor directives are mostly used. These header files are already in the include
directory on the computer. In the last class of our C++ course, we used #include <iostream.h>
and #include <conio.h>
. #include<iostream.h>
is used for the C++ object cout
and #include<conio.h>
for the built-in function getch ()
.
#define
, used to declare symbolic constants in C++, is another widely used preprocessor directive. The following is its general syntax:
#define identifier value
For Example:
#define PI 3.14
This defines a new constant: PI
. Once defined, they can be used in the rest of the program as if they were any other regular constant.
A Simple C++ Program that will show the concept of Preprocessor directives in C++.
//Cpp to demonstrate the concept of Preprocessor Directive (#define) #include <iostream> #include<conio.h> using namespace std; #define PI 3.14 //defining new constant int main() { double r=7.5; //radius double circle; circle = 2 * PI * r; cout<< " Area of the Circle= " <<circle; getch(); return 0; }
OUT OF THE PROGRAM:
Area of the Circle= 47.1
Because the #define
directive is a preprocessor directive rather than a C++ statement, it considers the entire line as the directive and does not require a semicolon ;
at the end.
Main ( ) Function
The main function is the point at which all C++ programs begin to execute. Regardless of whether other functions with different names are created before or after it, the instructions included inside this function’s definition will always be performed first in any C++ program. It is also necessary for all C++ programs to have a main function for the same reason. If you don’t have a function named main()
in your program, you’ll get an error when you execute it.
The term main is followed by a pair of parentheses ()
in the code.
Body of The Main () Function
The body of the main function, which is enclosed in braces { }
, begins after the parenthesis of the main ( )
function. The instructions written within this function are executed when it is called. C++ statements are written inside the main function’s body.
Constant Qualifier In C++
The const
keyword is used to define a constant identifier whose values remain constant throughout the program execution. Const identifiers
must be given a value when they are declared, and that value cannot be changed later.
Here’s a piece of code that shows how to use const identifier keywords to define constants.
const int DollarRate = 179.73; // ($1 = 179 PKR)
Declaring a variable as const prevents the programmer from unintentionally changing its value. Consider the following lines of code to change the value of the variable DollarRate.
Const int DollarRate = 179.73;
DollarRate = 124; // Compiler Error
Because the code at line 2
tries to modify the value of the variable DollarRate
from 179.73 to 124
, which is defined as constant
, the compiler will generate an error message. It’s a substitute for the #define
preprocessor directive, which is used to define a constant in a C++ program.
1 comment
Good ?