C’
PRE-PROCESSOR:
Ø Pre-Processor is
a program that processes the source code before it passes through the
compiler. It operates under the
control of preprocessor directives pre processor directives are placed in the
program before the main line.
Ø All of the
preprocessor directives begins with a # and do not end with a semicolon.
There
are 3 types of pre processor directives in ‘C’:
1.
Macro
Substitution directives
2.
File
inclusion directives.
3.
Compiler
control directives.
1. Macro
Substitution directives:-
Ø Macro
Substitution is a process of replacing an identifier by a pre-defined
string. This is done by # define
statement. This statement is known as
macro.
Syntax:- # define identifier string.
The string may be any
text but the identifier must be a valid ‘C’ name.
A
macro substitution can be of different forms:-
a. Simple macro substitution.
b.
Argument macro substitution.
c.
Nested macro substitution.
a) Here the
identifier is replaced by a simple string.
Eg:-
# define PI 3-142
# define TRUE 1
A macro definition can also include
expressions.
Eg:-
# define c(5*2+1)
Ø To avoid
incorrect results, the expression should always be enclosed with in
parenthesis. A macro definition can also
include any meaningful text.
Eg:-
# define MAIN main ( ) {
# define SUM (a+b)
# define PRINT printf(“%d”, sum);
# define END }
MAIN
int a,b;
SUM;
PRINT;
END
b) Parameters
can also be passed while defining a macro.
Eg:- #define square (x) ((x)*(x))
#define max (a,b) (((a)x(b))) ? (a): (b))
main ( )
{
int a=5,b=6;
printf (“ %d”, max
(a,b));
}
c) One macro definition can be used in defining
another macro.
Eg:- #define square (x) ((x)*(x))
# define cube(x) (square (x) * (x))
There
is also a possibility to undefined a macro.
This is done by using the statement.
# undef identifier
This
is useful when we want to restrict our definition only to a particular part of
a program.
2. File inclusion directives:-
Ø A file
containing macro definitions or function definitions can be included in a
program so as to avoid rewriting the code.
This
is done by the following preprocessor directive.
#
include “filename”
Here
filename indicates the file containing the definitions.
This
can be done in the following form
#
include <filename>
The
difference is, in this case the file is searched only in the standard
directories where as when we include filename in double quotes it is searched
in the current directory also.
Macros versus Functions
Ø
In a macro call the preprocessor
replaces the macro template with its macro expansion, As against this,
in a
function call the control is passed to a function along with certain arguments,
some calculations are performed in the function and a useful value is returned
back from the function.
Ø
Usually macros make
the program run faster but increase the program size, whereas functions make
the program smaller and compact.
Ø
If we use a macro hundred times in a program, the macro expansion
goes into our source code at hundred different places, thus increasing the
program size. 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.
Ø
But passing arguments to a function and getting back the returned
value does take time and would therefore slow down the program. This gets
avoided with macros since they have already been expanded and placed in the
source code before compilation.
No comments:
Post a Comment