Home » C++ » Preprocessor Directives & Main () Function In C++ | Class: 004 | C++ Complete Course

Preprocessor Directives & Main () Function In C++ | Class: 004 | C++ Complete Course1 min read

What are Directives & Main Function In C++

by TechABU
Published: Last Updated on
Preprocessor Directives & main () function in c++

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.

1 comment

Zahid August 25, 2021 - 11:45 am

Good 🙂

Reply

Leave a Comment

Related Articles