What is a Preprocessor
The preprocessor is a program that processes the source code of the program before it is sent to the compiler for compilation. They are mostly used when we include a header file in a program using a statement like #include but they can also be used to define macros.Before learning how to use preprocessor, let's first learn how a program executes.
Excecution of a program
When we write a program in C and compile it to see the output there are many phases in its life. The program travels through many stages before we see the output on the screen.For example, Suppose you have a program called "sample.c" and you compile it, the following actions will take place in the system to run it.
Excecution of a program
When we write a program in C and compile it to see the output there are many phases in its life. The program travels through many stages before we see the output on the screen.For example, Suppose you have a program called "sample.c" and you compile it, the following actions will take place in the system to run it.
- The source code is stored in "sample.c", so the expanded source code will be stored in a file havingthe same name as the source code file with the extension ".I". Therefore the expanded source code of "sample.c" will store as "sample.i"
- Now when we compile this expanded source code another file is created with the extension ".obj" which stands for "object".
- When this object code is linked with the object code of library functions the resultant executable code gets stored in "sample.exe"
The Preprocessor
The preprocessor offers several features called preprocessor directives. All these preprocessor directives start with a # symbol.In addition, each preprocessing directive must be on its own line. These directives are generally place in the beginning of the program, but can be put anywhere in the program.
Let's learn the following preprocessor directive
a) Macro expansion
b) File inclusion
c)Conditional Compilation
d) Miscellaneous directives
MACRO EXPANSION
Syntax
#define name value
Where name is the name of the macro and value is the value that we want to assign to it.whenever name appears in the file, it will be replaced by the value.The name of the macros are also called "Macro Templates" while the values assigned to them is called "Macro Expression".
Look at the statement given below
#DEFINE DAYS 7
The above statement is defining a macro named "DAYS" and assigning the value 7 to it. During preprocessing, the preprocessor will replace every occurrence of the word DAYS in the program body with 7.
Take a look
#define DAYS 7
void main( )
{
printf ( "\n%d",DAYS) ;
}
When the above program is run, you will 7 printed on the screen as this was the value of the macro "DAYS".
This is another example of macro definition.
#define MAX 20
void main()
{
int no=2,sq;
for(int i=0;i
{
sq=no*no;
printf("\nSquare of %d is %d",no,sq);
no++;
}
}
Points to remember
#define name value
Where name is the name of the macro and value is the value that we want to assign to it.whenever name appears in the file, it will be replaced by the value.The name of the macros are also called "Macro Templates" while the values assigned to them is called "Macro Expression".
Look at the statement given below
#DEFINE DAYS 7
The above statement is defining a macro named "DAYS" and assigning the value 7 to it. During preprocessing, the preprocessor will replace every occurrence of the word DAYS in the program body with 7.
Take a look
#define DAYS 7
void main( )
{
printf ( "\n%d",DAYS) ;
}
When the above program is run, you will 7 printed on the screen as this was the value of the macro "DAYS".
This is another example of macro definition.
#define MAX 20
void main()
{
int no=2,sq;
for(int i=0;i
{
sq=no*no;
printf("\nSquare of %d is %d",no,sq);
no++;
}
}
Points to remember
1. When the program in compiled it is scanned by the preprocessor to check whether it contains any macro
definitions(#define).If there are, it searches for the macro templates in the entire program. When it finds a template it replaces the macro template with the the macro expansion(value). Once the replacement is over the program is sent for compilation.
2. The macro names are normally written in capitals to make them easily readable for the programmers. However,the same can be written in small letters also.
3. There should be a space or a tab between the macro template and the macro expansion. A space between # and define is optional.
4. A macro definition is never terminated by a semicolon.
Why do we use #define ?
There are two main reasons for why we use #define,
a) To make the program easier to read : Using a macro, we can make a program easier to read. Consider this, suppose the phrase “d20@#” causes the screen to pause. But wouldn't it be easier if we can write "PAUSE" instead of "d20@#". So the macro would be defined as
#define PAUSE d20@#
b) If a constant like 365(days in a year) appears multiple times in the program and you want to change its value to 366. To do this you will have to read all the lines of the program and make changes at each occurence of 365. This will consume a lot of time and effort. But if we define a macro for the same, then changing the macro definition will change the value everywhere the preprocessor has been used.
#define DAYS 365
definitions(#define).If there are, it searches for the macro templates in the entire program. When it finds a template it replaces the macro template with the the macro expansion(value). Once the replacement is over the program is sent for compilation.
2. The macro names are normally written in capitals to make them easily readable for the programmers. However,the same can be written in small letters also.
3. There should be a space or a tab between the macro template and the macro expansion. A space between # and define is optional.
4. A macro definition is never terminated by a semicolon.
Why do we use #define ?
There are two main reasons for why we use #define,
a) To make the program easier to read : Using a macro, we can make a program easier to read. Consider this, suppose the phrase “d20@#” causes the screen to pause. But wouldn't it be easier if we can write "PAUSE" instead of "d20@#". So the macro would be defined as
#define PAUSE d20@#
b) If a constant like 365(days in a year) appears multiple times in the program and you want to change its value to 366. To do this you will have to read all the lines of the program and make changes at each occurence of 365. This will consume a lot of time and effort. But if we define a macro for the same, then changing the macro definition will change the value everywhere the preprocessor has been used.
#define DAYS 365
Why not use a variable ?
If you think that we could have used a variable instead of a macro to do the same job, you are to an extent correct but not fully because using macros instead of variables has its own advantages:
1. Macros are processed much faster than the variables.
2. When you don't want to change the value of a variable then it is not recommended to declare it as a variable. It will make the program a bit difficult to understand.
Examples
#define AND &&
void main( )
{
int age,exp;
age=45;
exp=4;
if(age>25 AND exp>2)
printf("\nYou will be considered for promotion");
else
printf("\nWait for some more time...");
}
- A #define directive could be used even to replace a condition, as shown below.
#define PASS (marks>40 && marks<=100 )
void main( )
{
int marks = 50 ;
if ( PASS )
printf ( "Congrats ! You have passed" ) ;
else
printf ( "Sorry try again" ) ;
}
- A #define directive could be used to replace even an entire C statement. This is shown below.
#define MESSAGE printf ( "Let's learn macros" ) ;
void main( )
{
MESSAGE;
}
void main( )
{
MESSAGE;
}
0 comments:
Post a Comment