a processor Direct,
tre source code, the lines thar es
alt hese directives can by
Greet the beginning of the
on because they are not
sri is discussed next
ir
begin with
e na ,
wo pad anyone gt ae known
Part of the le. These ins; P™Btam, but us othe hee
CHE age ectveS are not te usually they are
BvaKe. The lst of the pees
gdefine \¢ preprocessor
Finclude
#if
gerror
zelif
else
Fidel
line
endif
ifndef
undef
you can place only one preprocessor directive j
¢ in one line,
the #define Directive
the #define directive defines an identi
diective is encountered in i identifier and a replacement token, Wh
the subsequent occurrenc eae code, the replacement token will Spahbiiiot
abseqen ces ofthe tener ge acement sok il be subsiuted foal
he macro name a e. ‘ifier
as the veal the proce aan some code The enter is aso known
gintax is as follows: own as macro replacement. The
define macro_name replacement_token
For example, to use the word PI for the value 3.14159, you can write the following statement:
#define PI 5
14159
Ie above example, the word PI represents the macro name and the value 3.14159 represents
tereplacement token. "The #define directive directs the preprocessor to replace the word
with the specified value each time it is encountered in the source code.
Inthe above example, a single identifier is replaced with a specified replacement token, This
Process is also known as object-like mactos-
sed as function-like macros. In the function-like macros,
var pave an argument ist enclosed within the
is encountered, the
time the identifier i
passed in the program.
we fieine directive can also be uses
parent ine directive defines an identifier that
Hl heses and the replacement token. Each aria
ments within it are replaced with the actual argumentsfollows’
ayed by commas) replace
ar
The syntax for th oe
sar
ae
#define macro”
For example:
#define SUM(a,b) (atb)
)
UM(2,3)5
js encountered in the progra,
ifier SUM is ae ncaa
db, respectively. T
ve value of (a+B).
when an ident
variables @ al
the resultant ¥
¢ of the #define directive.
In the above example, 1
arguments (2,3) are assigned 10 ¥
the replacement token, which is
The following example illustrates the us
[Example 4
rogram to calculate the
area of ac
‘The following program will prompt the user to enter the radius ofa circle, calculate
and display the resultant value on the screen.
/Mrite a program to find the area of a circle.
#include
using namespace std:
#define PI 3.14//Object-like macro
#define AREA(t) (rr) //Function-like macro
int main()
double i;
intj;
cout<<“Enter the radius:” 7
cen lius:”<) or in double quotes. The syntax is as follows:
#include
#include “filename.h”
Inthe above syntax, the .h is used as a file extension.
for example:
— Findude
or
#include
10 read the file named
t
ts the compiler
i fre the same. The use
ive dire
( n above
Inthe #include direct el
«the above example, t it
mple, the #inclu ve
P th statements gh
of, at and include it in the progran’ Bo
i ;
ithe lower statement is optional.612. s
rective:
ion pl 's source ‘=
ional Compllatl tion ofthe Pragrarn’s source anna
Elia eagle eet that a ee conditions. There are f aresti
Conditional compilation mea in soe, #elif, Hendif, #ifdef, ang gv
compiled or ignored bein ‘are #if, #else,
conditional compilation.
Ss
The #if and #endif eee
The syntax for the #if directive is a
#if condition or expression
statement or block of code
#endif
ove syntax, the statemel ck of code between the #if and
nt or block of cos wee! a
¢ above syntax, em
ig
following the #if directive is true,
ec i pression following the ee
beighored Hers tie denaitabecnne refers tothe end ofthe Ht block
For example:
#define COUNT 10
int main( )
#if COUNT>9 .
cout<<“Count is greater”;
#endif
}
In the above example, the #define directive directs the preprocessor to substitute ay
in place of COUNT. When the execution begins, the condition COUNT>9 is checked
tmue. Here, the condition is true because the value 10 is greater than the value 9, So, the
statement will be executed and it will display the following Statement on the screen:
Count is greater
The #else Directive
The working of the Helse directive ig the same as the
“pression specified with the #if directive is false, the
directive will be compiled. The syntax is as follows:
#if condition or expression
Statement or block of code
#else
statement or block of
#endif maneqomeration, and Preproce
or
OUNT>20
cout<<“Count is greater”;
gelse .
cout<<"Count is smaller’
}
sample, the condition COUNT:
mple i UNT >20 is false. S :
vil be compiled and itil display the along statement on te sere
screen
theabove Or
i gse dient
iis smaller
lif Directive
‘eis also known as the else if directive. The working of the #elif directive is
ent. The syntax is as follows:
e #el r
spe esi rect
shesame as HE elseif statem!
#if condition or expression
statement]
#elif condition oF &XP'
statement?
4elif condition or expre
statement3
elif condition or exPre
statement
#endif
ression
ssion
ssion
if the condition or expression following the #f directive is tues he
ill be compited and “iL the #elif directives will be skipped.
sted.
Inthe above syntax,
satement associated with it
Othernise, the next #elif will be te
for example:
#define points_scored 50
int main()
#if points_scored>=90
cout< <"Grade A
#elif poimts_scored>=70
cout<<"Grade BY:6-14
=50
if points_scored> =50
tee Pe < rade C i
#elif points_scored> =
cout<<“Fail”;
#endif
t ubstitutes the value 50 in the 3
. e directive substi The #if directing
i hone ml ean gr ih he HE Scie
identifier points_scored. Here, ht Ci ive will be checked. If it is true,
it vith the next #elif direct ext #elif directive will be
sete Sea ie compiled. Otherwise, the nex! tes
on,
jirectives i n
Tne Mifdet and #ifndet Directives are also known as iCdefined and itnotg
The #ifdef directis
directives, respectively.
The syntax of the #ifdef is as follows:
#ifdef macto_name
statement
#endif,
define statement
¢ skipped. .
In the above syntax, if macro_name has already been defined in a #
statement associated with #ifdef will be compiled. Otherwise, it will be
For example:
#ifdef JOHN
cout<<"Hi, John”;
#endif
In this example, if the macro name JOHN h
‘ample, facto na has alteady been defined i je state
the next cout statement will be compiled, : tina sein
The syntax of the #ifndef is as follows:
#ifndef macro_name
Statement
#endif
In the above syntax, if the
e e syntax, macro_name is i
associated with #ifndet will be compiled, Other eehivernenaay
. ‘ise, it will be skipped. he6-15
ple
é
oHN 7
oot eth John”;
‘
onl
empl ifthe macro name JOHN ha
poets i compe “rein
yom
« def pirective
ut
fe g directive 1s used to remove a
Aone ant. The syntax is as fol & macro that has been prev it
qh eso Y yntax is as follows: previously defined in a
se
fined in a #define statement, the
onde macro_name
eile
fo
the #define directive defines the macro COUNT. The #undef
sie presions example,
COUNT, which was previously defined ina #define statement.
iective undefines the macro
the #line Directive
qfyouwant to change the contents of __LINE__and _FILE_, youcan do itwith the help of
ie fline directive. The _ E__ and _FILE__ identifiers are predefined to a compiler.
1s the value, whichis the line number ofthe currently compiled
identifier contan
the_L
Feafthe source code. The FILE i er contains a string value that specifies the
sof the source file being compiled. rhe syntax ofthe #line directive is as follows:
Aline value “filename”
Inthe above syntax, the value spe a positive integet value, which is the new value of the
-LINE_ identifier: The filename is optional. If you specify any valid filename, it becomes
Denorvalue of the _ FILE_ identifier
fr xample:
line 10 line_directive
t main() jjLine 10
Line av
jjline 12
cout<<_ LID
cout<<_ FILE,
return 0;6-16
becomes the
, unter and
enti name, which becomes qh.
se displays the line number, ;
‘which is Kine_directive on het
sets th
In this example, the value 10 reset t
LINE__ identifier and line_directive ©
FILE _ identifier. The first Cpe
other cout statement displays
The #error Directive
ilation of the program. The sympa,
The #error directive is used to stop the compilatio i
er
#etror error_message
Dy,
vhich is displayed when the #e, 4
B cilfies the message, which ef al by the compiler,
The error_message spe ee enldein "
encountered along with some other infor
The Operator This operator converts the argument in
The # is known as the stringize operator Thi
string that follows it. The syntax is as follows:
# argument
The stringize operator converts the value that is stored in this argument into a qu
For example:
#define demo(a) #a
int main( )
cout<