The C Preprocessor by Satbir Singh
The C Preprocessor by Satbir Singh
Satbir Singh
Assistant Professor
Department of Mathematics
Govt. College for Girls, Palwal, Kurukshetra, Haryana
The C preprocessor
Let Us C By Y Kanetkar
The C preprocessor
Let Us C By Y Kanetkar
• There are several steps involved from the
stage of writing a C program to the stage of
getting it executed.
Let Us C By Y Kanetkar
• There are several steps involved from the
stage of writing a C program to the stage of
getting it executed.
Let Us C By Y Kanetkar
Note that if the source code
is stored in a file PR1.C
Let Us C By Y Kanetkar
Note that if the source code
is stored in a file PR1.C
Let Us C By Y Kanetkar
Note that if the source code
is stored in a file PR1.C
Let Us C By Y Kanetkar
Note that if the source code
is stored in a file PR1.C
Let Us C By Y Kanetkar
Some Important things to remember:
Let Us C By Y Kanetkar
Some Important things to remember:
Let Us C By Y Kanetkar
Some Important things to remember:
Let Us C By Y Kanetkar
Some Important things to remember:
Let Us C By Y Kanetkar
#define UPPER 25
main( )
{
int i ;
for ( i = 1 ; i <= UPPER ; i++ )
printf ( "\n %d", i ) ;
}
Let Us C By Y Kanetkar
#define UPPER 25
main( )
{
int i ;
for ( i = 1 ; i <= UPPER ; i++ )
printf ( "\n %d", i ) ;
}
#define UPPER 25
Let Us C By Y Kanetkar
This statement is called ‘macro definition’ or
more commonly, just a ‘macro’.
Let Us C By Y Kanetkar
This statement is called ‘macro definition’ or
more commonly, just a ‘macro’.
Let Us C By Y Kanetkar
This statement is called ‘macro definition’ or
more commonly, just a ‘macro’.
Let Us C By Y Kanetkar
UPPER and PI in the above programs are often called
‘macro templates’, whereas, 25 and 3.1415 are called
their corresponding ‘macro expansions’.
Let Us C By Y Kanetkar
UPPER and PI in the above programs are often called
‘macro templates’, whereas, 25 and 3.1415 are called
their corresponding ‘macro expansions’.
Let Us C By Y Kanetkar
UPPER and PI in the above programs are often called
‘macro templates’, whereas, 25 and 3.1415 are called
their corresponding ‘macro expansions’.
Let Us C By Y Kanetkar
UPPER and PI in the above programs are often called
‘macro templates’, whereas, 25 and 3.1415 are called
their corresponding ‘macro expansions’.
Let Us C By Y Kanetkar
In C programming it is customary to use capital letters
for macro template.
Let Us C By Y Kanetkar
Note that a macro template and its macro expansion are
separated by blanks or tabs. A space between # and
define is optional. Remember that a macro definition is
never to be terminated by a semicolon.
Let Us C By Y Kanetkar
Note that a macro template and its macro expansion are
separated by blanks or tabs. A space between # and
define is optional. Remember that a macro definition is
never to be terminated by a semicolon.
Let Us C By Y Kanetkar
For example, if the phrase “\x1B[2J” causes the screen to
clear. But which would you find easier to understand in the
middle of your program “\x1B[2J” or “CLEARSCREEN”?
Thus, we would use the macro definition
Let Us C By Y Kanetkar
For example, if the phrase “\x1B[2J” causes the screen to
clear. But which would you find easier to understand in the
middle of your program “\x1B[2J” or “CLEARSCREEN”?
Thus, we would use the macro definition
Let Us C By Y Kanetkar
Suppose a constant like 3.1415 appears many times in your
program. This value may have to be changed some day to
3.141592.
Let Us C By Y Kanetkar
Suppose a constant like 3.1415 appears many times in your
program. This value may have to be changed some day to
3.141592.
Let Us C By Y Kanetkar
Suppose a constant like 3.1415 appears many times in your
program. This value may have to be changed some day to
3.141592.
Let Us C By Y Kanetkar
Suppose a constant like 3.1415 appears many times in your
program. This value may have to be changed some day to
3.141592.
Let Us C By Y Kanetkar
A #define directive is many a times used to define operators as
shown below.
Let Us C By Y Kanetkar
A #define directive is many a times used to define operators as
shown below.
main( )
{
int f = 1, x = 4, y = 90 ;
Let Us C By Y Kanetkar
A #define directive could be used even to replace a
condition, as shown below.
Let Us C By Y Kanetkar
A #define directive could be used even to replace a
condition, as shown below.
if ( ARANGE )
printf ( "within range" ) ;
else
printf ( "out of range" ) ;
}
Let Us C By Y Kanetkar
A #define directive could be used to replace even an entire C
statement.
Let Us C By Y Kanetkar
A #define directive could be used to replace even an entire C
statement.
if ( signature == 'Y' )
FOUND
else
printf ( "Safe... as yet !" ) ;
}
Let Us C By Y Kanetkar
Macros with Arguments
The macros that we have used so far are called simple
macros. Macros can have arguments, just as functions can.
Let Us C By Y Kanetkar
In this program wherever the preprocessor finds the phrase
AREA(x) it expands it into the statement ( 3.14 * x * x ).
Let Us C By Y Kanetkar
#define AREA(x) ( 3.14 * x * x )
main( )
{
float r1 = 6.25, r2 = 2.5, a;
a = AREA ( r1 );
printf ( "\nArea of circle = %f", a );
a = AREA ( r2 );
printf ( "\nArea of circle = %f", a );
}
main( )
{
float r1 = 6.25, r2 = 2.5, a ;
a = 3.14 * r1 *r1 ;
printf ( "Area of circle = %f\n", a ) ;
a = 3.14 *r2 * r2 ;
printf ( "Area of circle = %f", a ) ;
} Let Us C By Y Kanetkar
Here are some important points to remember while writing
macros with arguments:
#define SQUARE(n) n * n
main( )
{
int j ;
j = 64 / SQUARE ( 4 ) ;
printf ( "j = %d", j ) ;
}
The output of the above program would be:
j = 64
whereas, what we expected was j = 4.
Let Us C By Y Kanetkar
What went wrong? The macro was expanded into
j = 64 / 4 * 4 ;
which yielded 64.
If for any reason you are unable to debug a macro then you
should view the expanded code of the program to see how the
macros are getting expanded.
Let Us C By Y Kanetkar
If your source code is present in the file PR1.C then the
expanded source code would be stored in PR1.I.
Let Us C By Y Kanetkar
Macros versus Functions
In the above example a macro was used to calculate the area of the
circle.
Let Us C By Y Kanetkar
This brings us to a question: when is it best to use macros with
arguments and when is it better to use a function?
Usually macros make the program run faster but increase the
program size, whereas functions make the program smaller
and compact.
Let Us C By Y Kanetkar
On the other hand, if a function is used, then even if it is
called from hundred different places in the program, it
would take the same amount of space in the program.
This gets avoided with macros since they have already been
expanded and placed in the source code before compilation.
Let Us C By Y Kanetkar
Moral of the story is—if the macro is simple and sweet like in
our examples, it makes nice shorthand and avoids the
overheads associated with function calls.
Let Us C By Y Kanetkar
Thank You
Let Us C By Y Kanetkar