fuddlepixelFuddlepixel coz tech is fun();

Define Preprocessor

Let us get started with the processors and discuss the

By Shubham Ramdeo
19 November 2015 · 4 mins read
# c  newbie 

Define Preprocessor

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

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.


Share this :



Update me weekly ✉

Subscribe below and we’ll send you a weekly email summary of all new Code tutorials. Never miss out on learning about the next big thing.

I'm in ♥


Or follow us
   


About author

Shubham Ramdeo

Shubham Ramdeo

Shubham Ramdeo is the founder of Fuddlepixel. He is a geek, a writer, involved with many technical and humanity projects.

Meet me!



Recent





Related

c 

newbie 

Top