100% found this document useful (1 vote)
904 views5 pages

Pre Processor

The preprocessor is a program that processes source code before it is passed to the compiler. It performs tasks like macro expansion, file inclusion, and conditional compilation using preprocessor directives that begin with #. Some key preprocessor directives are #define for macro definition, #include for file inclusion, #if/#else for conditional compilation, and #undef to undefine macros. Macros can be defined with or without arguments and can be used to replace code, operators, or entire statements.

Uploaded by

Saket Kumar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
100% found this document useful (1 vote)
904 views5 pages

Pre Processor

The preprocessor is a program that processes source code before it is passed to the compiler. It performs tasks like macro expansion, file inclusion, and conditional compilation using preprocessor directives that begin with #. Some key preprocessor directives are #define for macro definition, #include for file inclusion, #if/#else for conditional compilation, and #undef to undefine macros. Macros can be defined with or without arguments and can be used to replace code, operators, or entire statements.

Uploaded by

Saket Kumar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 5

PREPROCESSOR

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.

Following are some preprocessor directives:-


1. Macro Expansion
2. File Inclusion
3. Conditional Compilation
4. Miscellaneous Directives

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.

Use of #define Directives


 It can be used to define operators.
#define AND &&
void main()
{
int age=20;
if(age>=13 AND age<=19)
printf(“Teenager”);
else
printf(“Not”);
}

 It can be used to replace a condition.


#define CHECK (age>=13 && age<=19)
void main()
{
int age=20;
if CHECK
printf(“Teenager”);
else
printf(“Not”);
}

 It can be used to replace an entire ‘C’ statement.


#define TN printf(“Teenager”)
#define NTN printf(“Not Teenager”)
void main()
{
int age=20;
if(age>=10 && age<=19)
TN;
else
NTN;
}
Macro With Arguments
The macros that we are using till now are simple macros. Macros can have
arguments just as functions.
ex.-
#define Area(r) (3.14*r*r)
void main()
{
float a,x=5;
a=Area(x);
printf(“Area %f”,a);
}

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.

#if, #else, #elif, and #endif

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.

You might also like