You are on page 1of 10

CPr

ogr
ammi
ngOper
ator
s
Anoper
atori
sasy
mbol
thatoper
atesonav
alueorav
ari
abl
e.For
exampl
e:+i
sanoper
atort
oper
for
m addi
ti
on.
Chasawi
der
angeofoper
ator
stoper
for
mvar
iousoper
ati
ons.

CAr
it
hmet
icOper
ator
s
Anar
it
hmet
icoper
atorper
for
msmat
hemat
ical
oper
ati
onssuchasaddi
ti
on,
subt
ract
ion,
mul
ti
pli
cat
ion,
div
isi
onet
connumer
ical
val
ues(
const
ant
sand
v
ari
abl
es)
.

Oper
ator Meani
ngofOper
ator

+ addi
ti
onorunar
ypl
us

- subt
ract
ionorunar
ymi
nus

* mul
ti
pli
cat
ion

/ di
vi
sion

% r
emai
nderaf
terdi
vi
sion(
modul
odi
vi
sion)

Exampl
e1:
Ari
thmet
icOper
ator
s

//Wor kingofari
thmet
icoper
ator
s
#include<stdi
o.h>
i
ntmai n()
{
inta=9, b=4, c;
c=a+b;
pr
int
f("
a+b=%d\ n"
,c)
;
c=a-b;
pr
int
f("
a-b=%d\ n",
c);
c=a*b;
pr
int
f("
a*b=%d\ n",
c);
c=a/b;
pr
int
f("
a/b=%d\ n",
c);
c=a%b;
pr
int
f("
Remainderwhenadi
vi
dedbyb=%d\
n",
c);

r
etur
n0;
}

Out
put

a+b=13
a-
b=5
a*b=36
a/b=2
Remai
nderwhenadi
vi
dedbyb=1

Theoper
ator
s+,
-and*comput
esaddi
ti
on,
subt
ract
ion,
andmul
ti
pli
cat
ion
r
espect
ivel
yasy
oumi
ghthav
eexpect
ed.
I
nnor
mal
cal
cul
ati
on,
9/ 25.
4=2. Howev
er,
theout
puti
s2i
nthepr
ogr
am.
I
tisbecausebot
hthev
ari
abl
esaandbar
eint
eger
s.Hence,
theout
puti
sal
so
ani
nteger
.Thecompi
l
ernegl
ect
sthet
erm af
tert
hedeci
mal
poi
ntandshows
answer2i
nst
eadof2.
25.

Themodul
ooper
ator% comput
est
her
emai
nder
.Whena=9i
sdi
vi
dedbyb=4,
t
her
emai
nderi
s1.The% oper
atorcanonl
ybeusedwi
thi
nteger
s.
Supposea=5.
0, 0,
b=2. c=5andd=2.Theni
nCpr
ogr
ammi
ng,

/
/Ei
theroneoft
heoper
andsi
saf
loat
ing-
poi
ntnumber

a/
b=2.
5

a/
d=2.
5

c/
b=2.
5
/
/Bot
hoper
andsar
eint
eger
s

c/
d=2

CI
ncr
ementandDecr
ementOper
ator
s
Cpr
ogr
ammi
nghast
wooper
ator
sincr
ement++anddecr
ement-
-tochange
t
hev
alueofanoper
and(
const
antorv
ari
abl
e)by1.
I
ncr
ement++i
ncr
easest
hev
alueby1wher
easdecr
ement-
-decr
easest
he
v
alueby1.Theset
wooper
ator
sar
eunar
yoper
ator
s,meani
ngt
heyonl
y
oper
ateonasi
ngl
eoper
and.
Exampl
e2:
Incr
ementandDecr
ementOper
ator
s

//Wor kingofi
ncrementanddecr
ementoper
ator
s
#include<stdi
o.h>
i
ntmai n()
{
inta=10, b=100;
floatc=10.5,d=100.5;

pr
int
f("
++a=%d\n",++a);
pr
int
f("
--
b=%d\n",-
-b);
pr
int
f("
++c=%f\n",
++c);
pr
int
f("
--
d=%f\n",
--d)
;

r
etur
n0;
}

Out
put

++a=11
-
-b=99
++c=11.
500000
-
-d=99.
500000

Her
e,t
heoper
ator
s++and-
-ar
eusedaspr
efi
xes.Theset
wooper
ator
scan
al
sobeusedaspost
fi
xesl
i
kea++anda-
-.Vi
si
tthi
spaget
olear
nmor
eabout
howi
ncr
ementanddecr
ementoper
ator
swor
kwhenusedaspost
fi
x.

CAssi
gnmentOper
ator
s
Anassi
gnmentoper
atori
susedf
orassi
gni
ngav
aluet
oav
ari
abl
e.Themost
commonassi
gnmentoper
atori
s=
Oper
ator Exampl
e Sameas

= a=b a=b

+= a+=b a=a+b

-
= a-
=b a=a-
b

*
= a*
=b a=a*
b

/
= a/
=b a=a/
b

%= a%=b a=a%b

Exampl
e3:
Assi
gnmentOper
ator
s

//Wor kingofassignmentoper
ator
s
#include<st di
o.h>
i
ntmai n()
{
inta=5, c;

c=a; /
/ci
s5
pr
int
f("
c=%d\n",
c);
c+=a; //cis10
pr
int
f("
c=%d\n",
c);
c-
=a; / /cis5
pr
int
f("
c=%d\n",
c);
c*=a; //cis25
pr
int
f("
c=%d\n",
c);
c/=a; //cis5
pr
int
f("
c=%d\n",
c);
c%=a; / /c=0
pr
int
f("
c=%d\n",
c);

r
etur
n0;
}

Out
put

c=5
c=10
c=5
c=25
c=5
c=0

CRel
ati
onal
Oper
ator
s

Ar
elat
ional
oper
atorcheckst
her
elat
ionshi
pbet
weent
wooper
ands.I
fthe
r
elat
ioni
str
ue,
itr
etur
ns1;
ift
her
elat
ioni
sfal
se,
itr
etur
nsv
alue0.

Rel
ati
onal
oper
ator
sar
eusedi
ndeci
sionmaki
ngandl
oops.
Oper
ator Meani
ngofOper
ator Exampl
e

== Equal
to 5==3i
sev
aluat
edt
o0

> Gr
eat
ert
han 5>3i
sev
aluat
edt
o1
Oper
ator Meani
ngofOper
ator Exampl
e

< Lesst
han 5<3i
sev
aluat
edt
o0

!
= Notequal
to =3i
5! sev
aluat
edt
o1

>= Gr
eat
ert
hanorequal
to 5>=3i
sev
aluat
edt
o1

<= Lesst
hanorequal
to 5<=3i
sev
aluat
edt
o0

Exampl
e4:
Rel
ati
onal
Oper
ator
s

//Wor kingofr el
ati
onal
oper
ator
s
#include<st di
o.h>
i
ntmai n()
{
inta=5, b=5, c=10;

pr
int
f("
%d==%dis%d\ n",a, b,a==b);
pr
int
f("
%d==%dis%d\ n",a, c,a==c);
pr
int
f("
%d>%dis%d\n" ,
a, b, a>b);
pr
int
f("
%d>%dis%d\n" ,
a, c, a>c);
pr
int
f("
%d<%dis%d\n" ,
a, b, a<b);
pr
int
f("
%d<%dis%d\n" ,
a, c, a<c);
pr
int
f("
%d!=%di
s%d\ n",a, b,a!=b);
pr
int
f("
%d!=%di
s%d\ n",a, c,a!=c);
pr
int
f("
%d>=%dis%d\n" ,a,b, a>=b);
pr
int
f("
%d>=%dis%d\n" ,a,c, a>=c);
pr
int
f("
%d<=%dis%d\n" ,a,b, a<=b);
pr
int
f("
%d<=%dis%d\n" ,a,c, a<=c);

r
etur
n0;
}

Out
put

5==5is1
5==10is0
5>5is0
5>10is0
5<5is0
5<10is1
5!=5is0
5!=10is1
5>=5is1
5>=10is0
5<=5is1
5<=10is1

CLogi
cal
Oper
ator
s

Anex
pressi
oncont
aini
ngl
ogi
cal
oper
atorr
etur
nsei
ther0or1dependi
ng
uponwhet
herexpr
essi
onr
esul
tst
rueorf
alse.Logi
cal
oper
ator
sar
e
commonl
yusedi
ndeci
sionmaki
ngi
nCpr
ogr
ammi
ng.
Oper
ator Meani
ng Exampl
e

Logi
calAND.Trueonl
yifal
l I
fc=5andd=2t hen,
expr
essi
on(
(c==5
&&
oper
andsaretr
ue ( )e
d>5) qual
sto0.

Logi
cal
OR.Trueonl
yifei
therone I
fc=5andd=2t hen,
expr
essi
on(
(c==5
|
|
oper
andist
rue ( )e
d>5) qual
sto1.

Logi
cal
NOT.Tr
ueonl
yift
he
! I
fc=5t
hen,
expr
essi
on!
(c==5)equal
sto
oper
andis0

Exampl
e5:
Logi
cal
Oper
ator
s

/
/Wor
kingofl
ogi
cal
oper
ator
s

#i
nclude<stdi
o.h>
i
ntmai n(
)
{
i
nta=5, b=5, c=10,
resul
t;

resul
t=( a==b)&&(c>b)
;
print
f("
(a==b)&&(c>b)i
s%d\
n",
resul
t)
;
resul
t=( a==b)&&(c<b)
;
print
f("
(a==b)&&(c<b)i
s%d\
n",
resul
t)
;

resul
t=( a==b)||(
c<b)
;
print
f("
(a==b)||
(c<b)i
s%d\
n",
resul
t)
;

resul
t=( a!=b)||(
c<b)
;
print
f("
(a!=b)||
(c<b)i
s%d\
n",
resul
t)
;

resul
t=! (
a!=b)
;
print
f("
!(
a!=b)i
s%d\
n",
resul
t)
;

resul
t=! (
a==b)
;
print
f("
!(
a==b)i
s%d\
n",
resul
t)
;

r
etur
n0;
}

Out
put

(
a==b)&&( c>b)i
s1
(
a==b)&&( c<b)i
s0
(
a==b)||(
c<b)is1
(
a!=b)|
|(c<b)is0
!
(a!
=b)is1
!
(a==b)i
s0

Expl
anat
ionofl
ogi
cal
oper
atorpr
ogr
am
 ( c>5)e
a==b)&&( val
uat
est
o1becausebot
hoper
ands(
a==b)and(
c>b)i
s1
(
true)
.
 ( c<b)e
a==b)&&( val
uat
est
o0becauseoper
and(
c<b)i
s0(
fal
se)
.
 (
a==b)|
|(c<b)eval
uat
est
o1because(
a=b)i
s1(
true)
.
 (
a!=b)|
|(c<b)eval
uat
est
o0becausebot
hoper
and(
a!=b)and(
c<b)ar
e0(
fal
se)
.
 !
( =b)e
a! val
uat
est
o1becauseoper
and(
a!=b)i
s0(
fal
se)
.Hence,
!(a!
=b)i
s1
(
true)
.
 !
(a==b)eval
uat
est
o0because(
a==b)i
s1(
true)
.Hence,
!a==b)i
( s0(
fal
se)
.
CBi
twi
seOper
ator
s

Dur
ingcomput
ati
on,
mat
hemat
ical
oper
ati
onsl
i
ke:
addi
ti
on,
subt
ract
ion,
mul
ti
pli
cat
ion,
div
isi
on,
etcar
econv
ert
edt
obi
t-
lev
elwhi
chmakespr
ocessi
ng
f
ast
erandsav
espower
.

Bi
twi
seoper
ator
sar
eusedi
nCpr
ogr
ammi
ngt
oper
for
m bi
t-
lev
eloper
ati
ons.

Oper
ator
s Meani
ngofoper
ator
s

& Bi
twi
seAND

| Bi
twi
seOR

^ Bi
twi
seexcl
usi
veOR

~ Bi
twi
secompl
ement

<< Shi
ftl
eft

>> Shi
ftr
ight

Vi
sitbi
twi
seoper
atori
nCt
olear
nmor
e.
Ot
herOper
ator
s

CommaOper
ator

Commaoper
ator
sar
eusedt
oli
nkr
elat
edexpr
essi
onst
oget
her
.Forexampl
e:

i
nta,
c=5,
d;
Thesi
zeofoper
ator

Thesi
zeofi
saunar
yoper
atort
hatr
etur
nst
hesi
zeofdat
a(const
ant
s,v
ari
abl
es,
ar
ray
,st
ruct
ure,
etc)
.
Exampl
e6:
sizeofOper
ator

#i
nclude<stdi
o.h>
i
ntmai n()
{
i
nta;
fl
oatb;
doublec;
chard;
pri
ntf(
"Si
zeofint=%l
ubytes\n",
sizeof(a));
pri
ntf(
"Si
zeoffloat
=%lubytes\n",
sizeof(b));
pri
ntf(
"Si
zeofdouble=%l
uby tes\n",si
zeof(c))
;
pri
ntf(
"Si
zeofchar=%lubyte\n"
,sizeof(d))
;

r
etur
n0;
}

Out
put

Si
zeofi
nt=4bytes
Si
zeoffl
oat=4byt
es
Si
zeofdoubl
e=8by t
es
Si
zeofchar=1byt
e

You might also like