Pre Processor
Pre Processor
The preprocessor is a program that process our source code before it is passed to the
compiler.
Features of Preprocessor
The preprocessor offers several features called preprocessor directives. Each of
these preprocessor directives begins with a hash symbol (#). The directives can be placed
anywhere in the program but often placed at the beginning of the program before the first
function definition.
Macro Expansion
#define MAX 20
Void main ()
{
int i;
for(i=1;i<=MAX;i++)
printf(“%d”,i);
}
In the above program, the #define MAX 20 is called Macro Definition or more
commonly just a Macro.
During preprocessing, the preprocessor replaces every occurrence of MAX in the
program with 20. After preprocessing the above program changes as follows:-
void main()
{
int i;
for(i=1;i<=20;i++)
printf(“%d”,i);
}
In the above program, MAX is called Macro Template whereas 20 is called their
corresponding Macro Expansion.
Some Important Points / Guideline
In “C” programming, Capital letters are used for Macro Template. This makes it
easy for programmer to pick out all the Macro Templates when reading through
the program.
Macro Template & its macro expansions are separated by blanks or tabs, a space
between # and define is optional. Remember that a macro definition doesn’t end
with a semicolon.
Note – Macro can be split into multiple lines with a “\” (backslash) present at the end of
each line.
Ex.-
#define SMILE for(i=0;i<=79;i++)\
printf(“%c”,i)
void main()
{
SMILE;
}
File Inclusion
The 2nd preprocessor directive is File Inclusion. This directive causes one file to
be included in another. The preprocessor command for file inclusion is :-
#include “file name”
Actually there exists two ways to write #include statement. These are:-
#include “file name” - This command will look for the file in the current
directory as well as in the specified list of directories.
#include <file name> - This command will look for the file in the specified
list of directories only.
Conditional Compilation
There are several directives that allow us to selectively compile portions of our
program's source code. This process is called conditional compilation and is used widely
used by commercial software houses that provide and maintain many customized
versions of one program.
Perhaps the most commonly used conditional compilation directives are #if, #else, #elif,
and #endif.
These directives allow us to conditionally include portions of code based upon the
outcome of a constant expression.
The general form of #if is :-
#if constant-expression
statement sequence
#endif
If the constant expression following #if is true, the code that is between #if and #endif is
compiled. Otherwise, the intervening code is skipped. The #endif directive marks the end
of an #if block. For example:
#include <stdio.h>
#define MAX 10
int main(void)
{
#if MAX>99
printf("Compiled for array greater than 99.\n");
#else
printf("Compiled for small array.\n");
#endif
return 0;
}
The #elif directive means "else if" and establishes an if-else-if chain for multiple
compilation options.
#if expression
statement sequence
#elif expression 1
statement sequence
#elif expression 2
statement sequence
#elif expression 3
statement sequence
#elif expression 4
...
#elif expression N
statement sequence
#endif
ex.-
#define US 0
#define ENGLAND 1
#define FRANCE 2
#define ACTIVE_COUNTRY US
#if ACTIVE_COUNTRY == US
char currency[] = ''dollar";
#elif ACTIVE_COUNTRY == ENGLAND
char currency[] = "pound";
#else
char currency[] = "franc";
#endif
#ifdef and #ifndef
Another method of conditional compilation uses the directives #ifdef and #ifndef, which
mean ''if defined" and "if not defined," respectively. The general form of #ifdef is :
#ifdef macro-name
statement sequence
#endif
If macro-name has been previously defined in a #define statement, the block of code will
be compiled.
The general form of #ifndef is
#ifndef macro-name
statement sequence
#endif
If macro-name is currently undefined by a #define statement, the block of code is
compiled.
Note :- Both #ifdef and #ifndef may use an #else or #elif statement.
Ex.-
#include <stdio.h>
#define TED 10
int main(void)
{
#ifdef TED
printf("Hi Ted\n");
#else
printf("Hi anyone\n");
#endif
Miscelleneous Directives
#undef
The #undef directive removes a previously defined definition of the macro name that
follows it— that is, it ''undefines" a macro. The general form for #undef is
#undef macro-name
For example:
#define LEN 100
#define WIDTH 100
char array[LEN][WIDTH];
#undef LEN
#undef WIDTH
/* at this point both LEN and WIDTH are undefined */
Note :- #undef is used mainly to allow macro names to be localized to only those
sections of code that need them.