Shubham
Ramdeo


November 19, 2015

Define Preprocessor

That’s how the #define pre processor can be used in many ways !

Define Preprocessor

Welcome to the Newbie Programmer Series. With the last post about auto and register variables (click here), we have ended scope of the variables. So now, we are going towards the end of this program structure topic and hence we are now going to start the Pre-Processors. You can read an introduction about the preprocessors [here]. So if you are new to this series, please go to the index (click here) and read out all the previous parts so that you can easily understand what I am talking about.

Suppose a letter is to be written and sent to the President. A person will write the letter, but as instructed by his boss. And then his boss will recheck it or we can say “Pre Process” it and will make required changes. Then it will be sent. Similarly, in the C language we can do a similar thing. It’s the Pre Processing your code before it get compiled and build. The easiest one to get started with is called the #define Preprocessor. So lets discuss the coding stuff.

#define

#define is simply used to change the words in the code. For an example, you see this piece of code below :

#include <stdio.h>
main()
{
float circum;
float rad = 22;
circum = 2 * 3.14 * 22;
printf("%f \n", circum);
}

This is a simple one but suppose if we had to write this value of pi, 3.14 a thousand times, how will you manage it ? Well, you can make a variable as ‘float pi = 3.14’ but why waste memory ? So we have a better alternative. As we just want to replace 3.14 by pi, you write this :

#include <stdio.h>
#define PI 3.14
main()
{
float circum;
float rad = 22;
circum = 2 * PI * 22;
printf("%f \n", circum);
}

And when you build and run it, it will be automatically get converted into previous code. Just replacing PI to 3.14. It’s a convention followed from a long time that variables are written in small letters and #define replacements in capitals so as to make them easy to distinguish while writing code. You can do real fun with #define. You can replace anything.

Replacing Values

Just as we did in the above example, We can replace any type of value. Like this one below :

#include <stdio.h>
#define MAX 500
#define HELLO "Welcome to the this program \n"
main()
{
int no;
printf ( HELLO );
printf ("Enter a number \n );
scanf("%d", &no );
if ( no < MAX )
{
printf (" Its a small number \n");
}
printf("bye \n");
return 0;
}

the above code will be replaced to :

#include <stdio.h>
main()
{
int no;
printf ( "Welcome to the this program \n" );
printf ("Enter a number \n );
scanf("%d", &no );
if ( no < 500 )
{
printf (" Its a small number \n");
}
printf("bye \n");
return 0;
}

Isn’t that’s like a magic ?

Making Macros

Macros means a contract form for a large expansion, its more like a function. For example : instead of writing

area = 3.14 * r * r

again and again, we can make a simple macro substitution as

#define AREA(R) (3.14 * R * R)

Its work like function

#include <stdio.h>
#define AREA(R)  ( 3.14 * R * R )
main()
{
float rad, area;
printf("Enter the radius \n");
scanf ("%f" ,&rad );
answer = AREA(rad);
printf("%d", answer);
}

And the above code will be seen as :

#include <stdio.h>
main()
{
float rad, area;
printf("Enter the radius \n");
scanf ("%f" ,&rad );
answer = 3.14 * rad * rad ;
printf("%d", answer);
}

Replace the language itself !

Yes you can change the C language’s codes to your own !

#include <stdio.h>

#define float DEC;
#define EXIT return(0);

main()
{
DEC num = 22.7;
printf("%f", num );
EXIT
}

And the above code gets replaced as :

#include <stdio.h>
main()
{
float num = 22.7;
printf("%f", num );
return(0);
}

A careful use of this process can change functions too like as below :

#include <stdio.h>
#define Say(x) printf(x);
#define AskNum(n) scanf("%d", &n);
main()
{
int h;
Say("Hello, Enter a number \n");
AskNum(h);
}

And the above code will get converted into :

#include <stdio.h>
main()
{
int h;
printf("Hello, Enter a number \n");
scanf("%d", &h);
}

Few things to be consider :

  1. There is no semicolon (;) used during #define
  2. Number of arguments should be same as used in #define. Like if we had used “ Say(“%d”, h);” It will cause some errors, well we will learn about it later.
  3. There is no data type like things needed. For #define replacements, there is no int or float or char etc. It’s just a replacement tool !

I think its enough for today, we will discuss a lot more about fun with pre processors So Stay Connected.


You might also like:

(prev)
(next)

(rand) How to increase torrent downloading speed ?
(rand) More Concepts of Functions
(rand) libgdx tutorial how to setup and begin game development

© Shubham Ramdeo, 2020
you are really awesome!

Site Designed with Beauter🌸