Sunteți pe pagina 1din 53

Operators

Kinds of Operators
Precedence of Operators
Type Casting
C|ass|f|cat|ons
ArlLhmeLlc operaLors
8elaLlonal operaLors
Loglcal operaLors
AsslgnmenL operaLors
lncremenL and decremenL operaLors
CondlLlonal operaLors
8lLwlse operaLors
Speclal operaLors
rithmetic Operators
peraLor for arlLhmeLlc operaLlon
unary operaLor +
8lnary Lerm operaLor + * /
ls purely lnLeger operaLor
7/4 1 47 4
90 / 2 43 74 3
9/4 2 9/20 43
4/7 0 90/20 43
9.0 2;
92.0;
9.02.0;
9.0 2;
92.0;
9.02.0;
ab a+b ab
a*b a*b a/b
nteger ar|thmet|c
-oLe ln modulo dlvlslon Lhe slgn of Lhe resulL
ls always Lhe slgn of Lhe flrsL operand
(dlvldend)
6/3 2
6/3 2
6/3 2
6/3 2
73 1
73 1
73 1
73 1
9recedence of ar|thmet|c operators
LefL Lo rlghL evaluaLlon
Lx
1 a 9 b 12 c 3
x a b/3 + c*2 1
Plgh prlorlLy * /
Low prlorlLy +
9recedence of ar|thmet|c operators
rder of evaluaLlon can be changed by
lnLroduclng parenLheses
Lx
1 a 9 b 12 c 3
x a b/(3 + c)*(2 1)
lor nesLed parenLheses lnner mosL
parenLheses wlll be flrsL evaluaLed
Lx
x 9 ((12/3) + 3*2) 1
#elational Operators
Compare Lwo value
8esulL Lrue (1)or false (0)
Zero ls false non zero lnLeger ls Lrue
#elational Operators
#elational Operators
Lxamples
7~4 1; 7~7 1;
97 0; iI at least one condition is true ,
result is true.
7~4 1; 7~7 1;
97 0; iI at least one condition is true ,
result is true.
7~4 1
7~9 0;
79 1;
7 7 1;
7 9 0;
7 !7 0;
1075 true
ab cd true iI ab is equal to cd
7~4 1
7~9 0;
79 1;
7 7 1;
7 9 0;
7 !7 0;
1075 true
ab cd true iI ab is equal to cd
ogical Operators
peraLors
Loglcal A-
" Loglcal -@
$$ Loglcal 8
An expresslon whlch comblnes Lwo or more relaLlonal
expresslons ls called as loglcal expresslon or compound
relaLlonal expresslon
a < b && b < c
ogical Operators
@ruLh Lable
p1 p2 p1 p2 p1 || p2
-on zero -on zero 1 1
-on zero 0 0 1
0 -on zero 0 1
0 0 0 0
ssignment Operators

5r 5r o5 5r2 5r 5r o5 5r2 5r1 op= Expr 2 5r1 op= Expr 2
* y 1; * y 1; * y 1; * y 1;
* (y1) * (y1)
sum sum i ; sum sum i ; sum i ; sum i ;
ncrement & Decrement Operators
peraLor
++
reflx operaLor (evaluaLlon and Lhen asslgnmenL)
osLflx operaLor (asslgnmenL and Lhen evaluaLlon)
CannoL use aL expresslon only aL varlable
CannoL apply aL real Lype
n 1;
n; // 2, n2
n 1;
n; // 2, n2
n 1;
n; // 1, n2
n 1;
n; // 1, n2
(a b) // error (a b) // error
ncrement & Decrement Operators
Lxamples
1 al++ 10
2 m n++ [+10
al 10
l l+1
JhaL ls Lhe ouLpuL of Lhe followlng C
segmenLs ( 8e careful )
x = 0 ;
if ( x++ )
y = 1;
else
y = -1;
printf (x=%d,y=%d\n, x,y);
JhaL ls Lhe ouLpuL of Lhe followlng C
segmenLs ( 8e careful )
x = 0 ;
if (++x)
y = 1;
else
y = -1;
printf (x=%d,y=%d\n, x,y);
The Conditional Operator
Lxpr1 ? Lxpr2 Lxpr3 (3 @erms peraLor)
m a ~ b ? (c ~ a ? c a) (c ~ b ? c b) ; m a ~ b ? (c ~ a ? c a) (c ~ b ? c b) ;
ma ~ y ? y ; ma ~ y ? y ;
iI ( ~ y)
ma ;
else
ma y;
iI ( ~ y)
ma ;
else
ma y;
itwise Operators
perand should be lnLeger Lype
itwise Operators
recedence
Operator Precedence Operator Precedence
=
~~ ~~~
&
`
,
(H)
(L)
itwise Operators
itwise AND
1001
2
& 0011
2
0001
2
To etract the s5ecial area in variable by masking that
area
it OR
1001
2
, 0011
2
1011
2
clusive OR
1001
2
` 0011
2
1010
2
1`s Com5lement
= 00001010
2
11110101
2
itwise Operators
8lLwlse ShlfL peraLor
$hiIt leIt()
$hiIt right(~~)
x << y = x * 2
y
x << y = x * 2
y
x >> y = x / 2
y
x >> y = x / 2
y
pec|a| operators
1 1he Comma operator
used Lo llnk Lhe relaLed expresslons LogeLher
LvaluaLed lefL Lo rlghL
has lowesL precedence parenLheses are
necessary
Lx
value (x10y3x+y)
pec|a| operators
2 1he s|zeof operator
lL reLurns Lhe number of byLes Lhe operand
occuples
Lhe operand can be a varlable consLanL or a
daLa Lype quallfler
Lx
m slzeof(sum)
n slzeof(long lnL)
k slzeof(233L)
CaLegory peraLor AssoclaLlvlLy
osLflx () ++ LefL Lo rlghL
unary
+ " ++ (Lype) *
slzeof
8lghL Lo lefL
MulLlpllcaLlve * / LefL Lo rlghL
AddlLlve + LefL Lo rlghL
ShlfL LefL Lo rlghL
8elaLlonal LefL Lo rlghL
LquallLy " LefL Lo rlghL
8lLwlse A- LefL Lo rlghL
8lLwlse x8 LefL Lo rlghL
8lLwlse 8 $ LefL Lo rlghL
Loglcal A- LefL Lo rlghL
Loglcal 8 $$ LefL Lo rlghL
CondlLlonal ? 8lghL Lo lefL
AsslgnmenL
+ * /
$
8lghL Lo lefL
Comma LefL Lo rlghL
O5erator Precedence
Lxample
3 + 4*3 6/2 + 7*8
3 + 20 6/2 + 7*8
3 + 20 3 + 7*8
3 + 20 3 + 36
23 3 +36
20 + 36
76
O5erator Precedence
Lxample
x y + z 4
x y + (z 4)
x (y + (z 4))
(x y + (z 4))
O5erator Precedence
a x + y z // LefL AssoclaLlon
b x // 8lghL AssoclaLlon
c x++
d ++x
e x + z
e ++z + z
f p++ + p++
@ype converslon
AuLomaLlc Lype converslon
perands of lower Lype ls auLomaLlcally
converLed Lo hlgher Lype and resulL ls of
hlgher Lype
1 lloaL Lo lnL causes LruncaLlon of fracLlonal
parL
2 ouble Lo floaL causes roundlng of dlglLs
3 Long lnL Lo lnL causes dropplng of excess
hlgher order blLs
@ype converslon
Lxample
lnL lx
floaL f
double d
long lnL l
Lxpresslon
x l / l + l * f d
long
long
floaL
floaL
floaL
floaL
lnL
double
double
Ty5e Conversion
Casting a value
The 5rocess oI local conversion is known as casting a
value.
Ty5e Conversion
Casting a value
Lxamples
x (lnL)73
A (lnL)213/ (lnL)43
8 (double)sum/n
? (lnL)(a + b)
cos ((double)x)
,anag|ng $ lnpuLuLpuL
SLaLemenLs formaLLed l/
geLchar()
used Lo read a slngle characLer from Lhe
sLandard lnpuL unlL (keyboard)
#lncludecLypeh
Lx char name
namegeLchar()
varlable name geLchar()
CharacLer LesL funcLlons
char characLer
characLergeLchar()
lsalpha(characLer) lL assumes a nonzero
value(@8uL) lf Lhe argumenL characLer
conLalns an alphabeL oLherwlse lL assumes
zero
lsdlglL(characLer)
CharacLer LesL funcLlons
nct|on 1est
lsalnum(c) ls c an alphanumerlc characLer?
lsalpha(c) ls c an alphabeLlc characLer?
lsdlglL(c) ls c a dlglL?
lslower(c) ls c a lower case leLLer?
lsprlnL(c) ls c a prlnLable characLer?
lspuncL(c) ls c a puncLuaLlon mark ?
lsspace(c) ls c a whlLe space characLer?
lsupper(c) ls c an upper case leLLer?
CharacLer LesL funcLlons
char c
cgeLchar()
lf(lsalpha(c)0)
prlnLf(leLLer")
else
lf(lsdlglL(c)0)
prlnLf(dlglL")
else
prlnLf(noL alphanumerlc")
puLchar()
used Lo wrlLe characLers one aL a Llme Lo Lhe
Lermlnal
Lx answer '?'
puLchar(answer)
puLchar(varlable name )
puLchar()
Lx
char alphabeL
alphabeL geLchar()
lf(lslower(alphabeL))
puLchar(alphabeL)
lormaLLed lnpuL Scanf () funcLlon
lLs an lnpuL funcLlon used Lo read values ln
run Llme
lLs a call by address funcLlon
lL ls predeflned funcLlon ln std|oh
lf ls mlsslng varlable conLalns garbage value
scanf (conLrol sLrlng" var1 var2) scanf (conLrol sLrlng" var1 var2)
lleld speclflcaLlons
@he fleld speclflcaLlon for readlng an lnLeger
number ls
wc wc
conversion s5eciIication
w Iield width oI the number to be read
d data ty5e character in integer mode
c character mode
s string mode
wd wd ws ws
l / lormaLs
ls called formaL speclfler
d eclmal lnLeger
0 cLal lnLeger
0x Pexadeclmal lnLeger
ata type format
lnL d
lloaL f
Char c
Long ld
ouble lf
unslgned u
SLrlng s
Scanf () funcLlon
Lxample2 lnL a b c
scanf(d d d" a b c)
Lxample3 lnL d m y
scanf(d d d" a b c)
Lxample4 lnL x floaL y char ch
scanf(d f c" x y ch)
rder of Lhe varlables and order of formaLs musL
maLch
@ry Lhls
1 scanf(2d 3d" ab)
lnpuL1 30 31426
lnpuL2 31426 30
2 scanf(d *d d" a b)
lnpuL 123 436 789
sklpped because of *
geLs()
recleves a sLrlng from keyboard unLll Lhe user
presses enLer key
Spaces and Labs are accepLable as parL of
lnpuL sLrlng
scanf("s" a) gets(a)
lf user enLers 'sankar dayal
sharma' only sankar ls sLored ln
array a
a
enLlre 'sankar dayal sharma' ls
sLored ln array a
a
scanf() reads characLers unLll
user press space bar
geLs() reads characLers unLll
user presses enLer key
scanf() reads only one word geLs() funcLlon can read
mulLlword sLrlngs
geLs(arrayname)
lormaLLed funcLlon unformaLLed funcLlon
Sankar 0
Sankar dayal sharma 0
lormaLLed ouLpuL rlnLf()
lL ls an ouLpuL funcLlon
lL's a call by value funcLlon
lL ls predeflned funcLlon ln std|oh
Lxamp|e |nt a 2S
9r|ntf("d"a)
prlnLf (conLrol sLrlng" arg1 arg2) prlnLf (conLrol sLrlng" arg1 arg2)
prlnLf()
@ype Lhe followlng and observe Lhe resulLs
lnL x 23 floaL y 1068f char ch 'g'
1 prlnLf(d f c"xych)
2 prlnLf(dn fn c"xych)
3 prlnLf(dL f Lc"xych)
4 prlnLf(d f c"xych)
n and L cannoL be used ln scanf() buL can be
used ln prlnLf
prlnLf(resulL ls dn"x*2+y)
scanf() pr|ntf()
lnpuL funcLlon uLpuL funcLlon
@o read values aL run Llme @o dlsplay values
Call by address funcLlon Call by value funcLlon
deals wlLh keyboard deals wlLh monlLor
Je can'L wrlLe n and L -oL allowed
lL can conLaln only
varlables
lL can conLaln varlables
messages and expresslons
puLs()
lL ouLpuLs a sLrlng Lo Lhe screen
puLs(C8lL@")
puLs()
@ype Lhe followlng and observe Lhe resulLs
1 rlnLf(hydn")
rlnLf(secn")
rlnLf(cybn")
2 uLs(hyd")
uLs(sec")
uLs(cyb")
3 prlnLf(d"'')
prlnLf(d"'A')
prlnLf(d"'7')
prlnLf(d"'0')
4 prlnLf(c"'0')
3 floaL a 1236789
prlnLf(2f"a)
prlnLf(3f"a)
prlnLf(f"a)
6 lnL a4367
prlnLf(7d"a)
prlnLf(07d"a)
prlnLf(2d"a)
prlnLf(d"a)
@ry Lhls
lsplay 070809
7 prlnLf(n")
prlnLf(")
prlnLf("")
prlnLf(")
prlnLf(""")
prlnLf(")
prlnLf(")

S-ar putea să vă placă și