Close
Close

Pre-processor in C++


Pre-processor processes our program before going to the compiler. You may not get it right now but will soon understand what it means.

How to Declare


Pre-processor directives begin with a hash symbol #. Preprocessor directives are preprocessor commands.

The most common preprocessor directive is

#include

Here, #include is a preprocessor directive and it makes header file like iostream available for us i.e. include header file in our program.

Let's take another example of a pre-processor directive.

#define PI 3.14

Here, #define is a pre-processor directive. It is used to define something like here we have defined that PI is 3.14. So, before going to the compiler, if PI comes anywhere in the program, it will be replaced by 3.14.

Following is a C++ program to find the area of a circle.

#include <iostream>
#define PI 3.14

using namespace std;

int main(){
	int radius;
  	float area;
  	cout << "Enter radius of circle" << endl;
  	cin >> radius;
  	area = PI*radius*radius;
  	cout << "Area of circle is " << area << endl;
	return 0;
}
Output
Enter radius of circle
4
Area of circle is 50.24

The only thing that you should understand here is that in our program, PI is replaced by 3.14 whenever it came in the program even before compiling. So, PI*radius*radius - will be 3.14*radius*radius.

Macros with Arguments


#define can be used to write macro definitions as well as shown below. Macros are also like functions but are a bit different from that. They can also take arguments. Let's see an example to understand how to use these.

#define area(r) (3.14*r*r)

Let's see an example to print the area of a circle using macros

#include <iostream>
#define area(r) (3.14*r*r)

using namespace std;

int main(){
	int radius;
  	float area;
  	cout << "Enter radius of circle" << endl;
  	cin >> radius;
  	area = area(radius);
  	cout << "Area of circle is " << area << endl;
	return 0;
}
Output
Enter radius of circle
4
Area of circle is 50.24

We have defined a macro 'area' which takes one argument that is 'r'. So when we called area(radius), it got replaced by 3.14*radius*radius.

It is like we have defined something and whenever it will appear in our program, will be replaced by that definition.

Let's Compare Macro with Function


We have just seen an example to print the area of a circle using macros. Now let's see the same example using function.

#include <iostream>

using namespace std;

float CircleArea(int r)
{
  	float a;
  	a = 3.14*r*r;
  	int radius;
  	return a;
}

int main(){
	int radius;
  	float area;
  	cout <<; "Enter radius of circle" << endl;
  	cin >> radius;
  	area = CircleArea(radius);
  	cout << "Area of circle is " << area << endl;
	return 0;
}
Output
Enter radius of circle
4
Area of circle is 50.24

Let's have a look at the basic differences between a macro and a function.

The first difference is that macros replace codes by their value ( as it replaced area(r) with (3.14*r*r) ). So, every time code will be allocated some space. This means that every time area(r) will appear, it will allocate space in memory. But this is not the case with function.

Secondly, function call takes some time but macro is defined before the actual program. So, macro is faster.

To learn from simple videos, you can always look at our C++ video course on CodesDope Pro. It has over 750 practice questions and over 200 solved examples.

First of all, convince yourself that you are the best because rest of your life is going to go proving this to others
-Wasim Akram

Ask Yours
Post Yours
Doubt? Ask question