logo codesdope

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 "stdio.h" available for us.

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.

It is different from normal variables because normal variables are processed during compilation. However, anything defined in #define is replaced with its value before compilation.

Similarly, #include links any header file before starting the compilation.

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

#include <stdio.h>
#define PI 3.14
int main()
{
  int radius;
  float area;
  printf("Enter radius of circle\n");
  scanf("%d", &radius);
  area = PI*radius*radius;
  printf("Area of circle is %.2f\n", area);
  return 0;
}
Output
Enter radius of circle
4
Area of circle is 50.24

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 also as follows. Macros are also like function but are a bit different from that. They can also take arguments. Let's see an example to understand how to use them.

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

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

#include <stdio.h>
#define area(r) (3.14*r*r)
int main()
{
  int radius;
  float area;
  printf("Enter radius of circle\n");
  scanf("%d", &radius);
  area = area(radius);
  printf("Area of circle is %.2f\n", area);
  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 (as area(r) is 3.14*r*r).

Unlike functions which are called during compliation, wherever there is area(x) in our code it will be directly replaced by 3.14*x*x before compliation.

So, the above code is equivalent to:

#include <stdio.h>

int main()
{
  int radius;
  float area;
  printf("Enter radius of circle\n");
  scanf("%d", &radius);
  area = 3.14*radius*radius;
  printf("Area of circle is %.2f\n", area);
  return 0;
}

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 <stdio.h>
float CircleArea(int r)
{
  float a;
  a = 3.14*r*r;
  int radius;
  return a;
}
int main()
{
  int radius;
  float area;
  printf("Enter radius of circle\n");
  scanf("%d", &radius);
  area = CircleArea(radius);
  printf("Area of circle is %.2f\n", area);
  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 be space allocated in the 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.

You practice and you get better. It's very simple.
- Phillip Glass

Download Our App.
BlogsDope App
Get it on Google Play
Doubt? Ask question
Close

Welcome.please sign up.

Close

Welcome.please login.