ü Before
a C program is complied it is passed through another program called
“Preprocessor”.
ü
The C program is known as “Source Code”.
ü
The preprocessor works on the source code
and creates ‘Expanded Source Code’. It is this expanded source code that is
sent to the compiler for compilation.
Preprocessor
Directives
1)
Macro
expansion
(a) Macros
- #define
General Format:
#define macro_name macro_expansion
e.g.
#define UPPER 25
§ This
statement is called ‘macro expansion’.
§ During
preprocessing, the preprocessor replaces every occurance of UPPER in the
program with 25.
§ It is
customary to use capital letters for macro templates as it makes it easy for
programmers to pick out all the macro templates when reading through the
program.
§ A macro
definition is never to be
terminated by a semicolon (;).
(b) Macros
with Arguments
Sample Code:
#include<stdio.h>
#define
AREA(x) (3.14 * x * x)
void
main()
{
float r = 6.25, a;
a = AREA(r );
printf(“ Area of Circle = %f”, a);
}
OUTPUT
Area
of Circle = 122.656250
§ Be
careful not to leave a blank between the macro template and its argument while
defining the macro.
§ The
entire macro expansion should be enclosed within parentheses.
§ Macros
can be split into multiple lines with a ‘\’ (back slash) present at the end of
each line.
Macros
vs. Functions
1. In a
macro call, the preprocessor replaces the macro template with its macro
expansion in a literal way.
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.
2. Macros
make the program run faster but increase the program size, whereas functions
make the program smaller and compact.
2)
File
Inclusion
The preprocessor
command for file inclusion looks like this:
#include “filename”
OR #include<filename>
and it simply
cause the entire contents of filename to be inserted into the source code at
that point in the program.
#include<goto.h>
|
This command would look for the
file in the current directory as well as the specified list of directives as
mentioned in the include search path that might have been set up.
|
#include ”goto.h”
|
This command would look for the
file in the specified list of directories only.
|
When and why this
feature is used?
§ It is a
good programming practice, to keep different sections of a large program
separate. These files are #included at the beginning of main program file.
§ There
are some functions and some macro definitions that we need almost in all
programs. The same can be stored in a file and that file can be included in
every program we write.
3)
Conditional
Compilation
(a) #ifdef
and #endif
§ If we
want we can have the compiler skip over a part of the souces code by inserting
the preprocessing commands #ifdef and #endif.
§ General
Format:
#ifdef macroname
statement1;
statement2;
statement3;
#endif
(b) #if and
#elif
§ The #if
directive can be used to test whether an expression evaluates to a nonzero
value or not. If the result of the expression is nonzero, then sunsequent lines
upto a #else, #elis or #endif are complied, otherwise they are skipped.
§ General
Format:
void
main()
{
#if
TEST < = 5
statement1;
statement2;
statement3;
#else
statement4;
statement5;
statement6;
#endif
}
4) Other Directives
(a) #undef
In
order to undefined a macro that has been earlier #defined, the directive
#undef
macro template
can
be used.
ü All
subsequent macro definitions statements would evaluate to false.
(b) #pragma
§ This
directive can be used to turn on or turn of certain features.
(i)
#pragma startup and #pragma exit
ü These
directives allow us to specify functions that are called upon program startup
(before main() ) or program exit (just before the program teminates).
Example:
void
fun1();
void
fun2();
#prgma
startup fun1
#pragma
exit fun2
void
main()
{
printf(“\n
Inside main”);
}
void
fun1()
{
printf(“\n
Inside fun1”);
}
void
fun2()
{
printf(“\n
Inside fun2”);
}
OUTPUT
Inside
fun1
Inside
main
Inside
fun2
(ii)
#pragma warn
ü The
#prgama warn directive tells the compiler whether or not we want to suppress a
specific warning.
Example:
#pragma warn –rvl /* return value */
#pragma warn –par /* parameter not used */
#pragma warn +rch /* unreachable code */
§ The
negative sign ( - ) would suppress the warnings.
§ The
positive sign (+ ) would flash the warnings.