Вы находитесь на странице: 1из 18

PRE PROCESSOR

DIRETIVES

BY
C MANASA
ASST. PROFESSOR
DEPT. OF ISE
PRE PROCESSOR

• Program that processes the source code before it passes through the
compiler
Directive Function
#define Defines a macro substitution
#undef Undefines a macro
#include Specifies the files to be included
#ifdef Test for macro substitution
#endif Specifies the end of #if
#ifndef Test whether a macro is not
defined
#if Test a compile-time condition
#else Specifies alternatives when #if
test fails
THREE CATEGORIES

• Macro Substitution Directives


• File Inclusion Directives
• Compiler Control Directives
MACRO SUBSTITUTION

• Process where an identifier in a program is replaced by a predefined string


composed of one or more tokens

#define identifier string

• Different forms of macro substitution


1. Simple Macro substitution
2. Argumented Macro substitution
3. Nested Macro substitution
SIMPLE MACRO SUBSTITUTION
• Used to define Constants
Example
#define COUNT 100
#define PI 3.1415
#define M 5 => total =M * 10 => total =50
#define AREA 5*12.46
#define TEST if(x>y)
#define AND
#define PRINT printf(“x is greater\n”);
TEST AND PRINT
#define EQUALS ==
SIMPLE MACRO EXAMPLE
MACROS WITH ARGUMENTS
• More complex and more useful form of replacements.
• Also called Macro call
#define identifier(f1, f2, f3, ….., fn) string
Ex: #define CUBE(x) (x*x*x)
volume = CUBE(10);
volume = CUBE(2+3) => ??
Ex: #define MAX(a, b) (((a)>(b)) ? (a): (b))
z = MAX(8,9)
#define STREQ(s1, s2) (strcmp((s1),(s2))==0)
res= STREQ(str1, sstr2)
MACROS WITH ARGUMENTS EXAMPLE
NESTING OF MACROS

• One macro in the definition of other macro


Ex:
#define M 5
#define N M+1
Ex:
#define SQUARE(x) ((x) * (x))
#define CUBE(x) (SQUARE(x) * (x))
EXAMPLE OF NESTING MACRO
UN DEFINING A MACRO

• A defined Macro can be undefined, using the statement


• Restrict the definition only to particular part of the program

#undef identifier
FILE INCLUSION

• External file containing functions or macro definitions can be included as a


part of the program
#include “filename”
or
#include <filename>
FILE INCLUSION EXAMPLE

fact.h p1.c
COMPILER CONTROL DIRECTIVES
Compiler control directives are: #ifdef ,#endif ,#ifndef ,#else, #if
• Situation 1: Suppose there is a file ontaining some macro definitions. It is not know
whether a particular macro has been defined or not. To test whether a particular
macro has been defined in that header file use #ifndef

If #define Max 100 is removed, it will print 90.


• Situation 2: Suppose a customer has two different types of computers and
you are required to write a program that will run on both the systems. You
want to use same program, although certain lines differ for each system,
make use of #ifdef, #else, #endif
• Situation 3: If a customer may insist on having additional features for the
program u have written, and you would like to have a single program that
would satisfy both types of customers.
• Situation 4: Suppose you are testing a large program, you would like to have
print calls inserted in certain places to display intermediate results in order to
trace the flow of execution and errors, such statements are called debugging
statements. Make use of #ifdef.
ANSI ADDITIONS

• #elif : Provides alternative test facility


• #pragma : Specifies certain instructions
• #error :Stops compilation when an error occurs
• # :Stringizing operator
• ## :Token-pasting operator

Вам также может понравиться