0% found this document useful (0 votes)
33 views22 pages

Module 2 - Operators in C, Type Conversion and Typecasting

Uploaded by

mahanoct1
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
33 views22 pages

Module 2 - Operators in C, Type Conversion and Typecasting

Uploaded by

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

Ope rators in c a)

- An operatr isq symbo\ ton speuifes the matioratial,logial, o


rdatoral operaon to be. perfesmd,
-Tbae opedrs can be angorie ed intof i g :
ay Arithmehic operatrs
b> Relational ooesatrs
c)Equality eperatrs
d> doqical operadts.
ey Unary eperatrs
SyConditonad operets.
q) Bitwise operatrs
hy Assign met operetors.
iyConma Operatrs.
> Size ot operats.
Arithmetie Operators
-Ardhmetie ogerat s Can be agoed do ony inegr
ftoting-point number.
The adlition, kubtration, mu bip leation -divisiun(+,
-, *, 1) operaor's pergrm Ahe usúal. aridhmetie
operastons in Cproooni, So you are alrady tamiliar
iidh hese operatois.
The moduy opuat(. )fnch he emainda of an
indyr divion, Ri opurado r can be aglicl
cannot be usel on
on ly to
inter orancs ono
dautle operandls
pogia) subdract
t induo <stio. h
long integer.

int ma'n ()
Iong in+ numl = 1234661, num2, ett O

printt"n knter th number: ");


Seant ("+ Ad " a-num 2 );
citt = numl- mum;
pint-f ("hn Differnte .kd", diff);

nter he number: 1234


Diferenu |233 933,
latonal Cperatis
A.latnal peratr, olto nnewn ay a tormpaison opend,
perad that ompare! 2 va hel.
Vlational perats durns trt or taols valut,
apenebng en whetar the ondi tioral -nlaten,chifp
htween h 2 permnd holes or not
pratry uieed ot
y. Thit
wil vedurn tru () ! 1 bu Ifan prearon
ý: ctherie
fal:e (o).
Meaning: daampl
give!
Ciratr than
Lu hon o cqual to 100< 160

50> 100

chararters at tenAieael vnlked oprarde Ant


iy art ipt ante d by numerit vaua in h

65<66
=| 20 10<
O 20 > 10
=I 20 ! 10
10<20
l
print(d<1d"
Y); X<: Y, x,
Y); >>= >= d
4, <, d) = dprint4("n
y); >+d,y, -l= -d>print4("n
); x{= =d,x,y, -dd!= ("n printf
); y, , .d d : =d printt(
Y); y, x,d", .d= d< (n prntf
;20 =10,
E xint
() main int
<stdio.h> inlude #
ational of use hshow grarn
pr'to writea
s
Conguage aupparts 2 Mincdh of equalty operadrs
tommpare Hheir operands for s4rict equality
UY
to

cyual to (==) a not eqal to (! =)


Thuy are
opratra. ()) if
the tgua- yerador
eqal- to ( =) edans drue
the sis of the oeratr have the
operanas on both
odhhrike, it turns falke (o).
Bamu vaue;
- On oter hand he not cqual to operatr (!=)
4he operands olo not have the
tursyrue () it
Bome vaue; e it retumt tolre (o).
Equslity operatocs
lperador Meuning
i¢ both operancu are equal,o
TeturnsIi4
oterwise.
Teeturns I if operands do not have he
Som value, o other noise.
doical Opeatrs
qperatrs
Logal AND ( )
oR(II)
NOT (!).
>logial
>lagialAND:
- l4 4 a birary operador, rohich simultares usly
evala 2 valug or relational erpresiorns.
if both the operans are true, then the spererds
iut whole expresien eveaty to true.
4 both or one ot the operandy is folce, then de
whole expre nsion evcte to olse.
Tuth oble
A t
|A

(n) 4a(6>c)
6for ezample exmeuions
ony it boh
- The egrsjon isbistrue
qresatr than both ac.
nre Hrue ie if
ande. oboth Hhan
r à bor cthorn qreadir han
qral b
iseither iftrue isienpres rohole The
-the
t (0cb) lett
is to
theepresion The -
Cb>) (osb)
exampe. 11 For
4
Tabk Trh
volu. drue returns
a o4hrise
+
s heboth ifvae olse eturns
a doq'
oRcal -
o.
dapal
doual NoT
The NOT operasthr tuies a Aingu oxres ron
expesion,
-Thad is bal NOT rooluus a oit t expresion
oT moobues
ewaluai do caa non zero vauue 4 procluu a 1 if
rpesion prodey a 0.
Trath abl
A A.

or oample.
inta = 10, b;
b loj
- Nou the value ot b=0, This is becauee vaue ot
to b,
henu the yut.
Vyary Oprntos. .
Unary operatar at on sna oerands. C targuage
hree unary operadns.
>Uhary Minus.
fos
lhany mins (-) operadof s striningy derent2 qperndd
the binary arithmetie oprador Hfid oparala on 2 opeand
4suhdracds f kecorol operanol from hu fet
- whn an operand s pre eeded by a minus Siyn,
he unasy operator negab1 its vabe.
For Eycample : int o, b 10
a-(b):

variable b hay a poAidive vau.


unary minus eperalorC-) on e
" P4ter oeying o, shich nd'eate it
operanol b, the value beuomes
negative vae.
Incemend eperator (t t)a Dreremand oprat ( -X pro
The inrement operador 3 a unary operadrv that
incre ases he value of ids operanel by 1..
-Smiarly te darennt operatr daereasy te va
- Foy examplu, --X equi vaent to : ing

- for exampe int 0, y5


Y=+t;
equivant to vori ting.

where as
>Write t program to ilhusdall he wwe
intemunt d- ooeremant operatrs. opn)
include <stdio. h>
t man )
int num 3)
7Usina unary prefr inremurd operadr
printtnThe vale
vabu ot num d" num);
tnum
prindt ("n The new valuu of num = l', num);
using unary preaix deerenand eperatr
prind(n\n The value at um dd num);
printft"n The valu of -num);
printnThe new vale ot numtd" num);
return
3
oud pudt
The valut ot num
The ttnum 4
num = 4
The new vau ot
The num
num
The vale ot
num e3.
The new valu
postefra inerement dcrenont epratrt, unary ap
int main ()
int num=3
I Os ingy unary postf ineremunt qpertr
prontf"n The valae of oume Y. d num):
printt ( The vale of num+t = d) num++);
prindf("The new vaeof nun = 1d,num);
l using nary poctfx ocYemnt eperasar
-d num);
um
printf" The vale of numz d, num- -);
rintt("o The ned value ot numz d" pum);

The valu ot
The vas ot num++ 3
The new vaue ot num : 4
value of num

valus of num- 4
The
num 3.
The new
(onditiorad Operatr
jt
-Thh tonditimal operator orrthe ternary (?:)j
LHe an it-ee sdo,tement that tan be wed i thin
epension.
example Largg =(a>b) ?o:b
- Here tonbitional operabr i med to fndl the large r
fiven num bers.
-First pl, hat is (osb) * evaluatd.
-If a is gyreatr than b, ten larqg z o,
=b.
- Hlenu, aree is equal do either a orb but t
both

int az5, b3, cz7 Small


Smal (oacb? (o<c?a:c):6<e? 6:c));
ing tenary ert
* incude <stcio. h>
incluele <conio.h.
int main()
int numl, numz, num), Lare

prit ("n frter the fist number: "):


Seant (".dtnumi);
printf ("ntoter he Beone nu mber:");

pintf ("n kntur he Rird numberi");


Sean ("y d, num3);
Lne numl> num2?(num)>num 7 numi num):
(hum 2>num3 ? num2 num3);

pof (" The largut number is : hd" large J;


yeturno;
fnter th tist nuber |2

Seiod 34
23
Thd
number s : 34
Lagt mumber
BHwise bperator
-Bidaise opndbrs are oe opradrs udt prform
operadhons ot bit bve.Thy inc uca bitoise AND,
bidoise oR, bitwise x0R, 4-shift operators,
When we ugehe bitwise AND perast r ke bit in the
` ANDed widh he orrponclin bit in the
Beconed oprarel. The truth dabk is gane
logical AD operaston.

de Hram to show se of bitwise operetort.


ineuele <stdio.h

VOi mei n )
4
int a=21, b:3 ;

+b);
a lb):;
printt ("
printf ("
(
printf b= d,vb);
printf ("n s<i);
print4( n a<<|=d
b>>|rd) b>>);
rint4(N
2); num hum
,
=num2 nuond
and =d num 1
thetvalustion
of the After (n1 printf
2); num numl, d num2
num: (nitial
ofvelue printt
nd dd
numl3,
numn2 int
man() int
stio.h induce
< opeetns #
the
ot use monstruli de torvercr
kr<<Exp var=Var<K=Exp
Nar-brip ExVar^:
p
ar=hr4En cExp Varg
Var-tup Narz =Exp Var-
tExp Var Varc &xp Vart
are *xp Var
Vor_or\fp énp var
Varls. Var:
Eaivdo syoax. Operatr
ruponsibu
fo
opl6
indial vabe anel hum2 5
AfHer fhe valuatn ot the xpesiin num1l6
and num2=5.

lommo operaor.
ope
rancle. l+
- The tovma operastor in Ctokes 2
Worhs
by evalaing te frst + dis cardng
rurns
Ylue, evauat
ken seconel

int o=2 b= 3, X=0;


Xz (+ +o, bt o);
=6.

Size ot oprator. operator used to


Ra unary
-The gize ot speato r
operater, the Heyuord Aizeof i
When, ns inq uname, variable,or eyrmon.
followed by a type
-for example int a I0)
unsiqned int Tesu H;
to read a charatr in

thin pint iet in lower case.


t includo stdi0 h>
+* induele <conio h
int moin ()
char chi
cltscr);
printf ("n nter an y charater in upper
Scanf (" . c +ch);
printf(o The charater in lower cae is :
':C) ch+2);

cherer : A
The cha'iati in lower ese is: q.
Write a roran to swap two nmbers using 4
Jen porary voiriable.
F inclucde <stcdio.h
int main ()
lint nunl, num2, tenpi
clrserl); tatec the trst nun be r: ");
printf (
scan t ( num 1);
priotfC"o koder RSecond humber: );

Hem
tmum 2 temp)
print f(n The trstnumber is d num )
printf( n The adnd number it Td num2);

Saanel
The tirst number si 5
Beenl
two numbers othout
>Ne a proyram do Bwa
wsing a temporary variable.
incude cstdioh> ati tf tist o : 3

int man () The firs t hunber is 5


The Slornol number a 3
líot num , num 2,
clrserl,
printt("n Enter the st number: ");
Scanf ( d dnum 1);
printt(n fnter the uand number: ";

num 1 ¿ num 1 + num2j


num 2 num 1 - nsm2j
num 2
is 1.d num 1);
Prntf("h The firet number t-dnum 2);
<prindf"ln The suond number is
onversion Typecas hing.
-Tl now ,Ne have asumd hed all
we have thef expresion
involve d dt ot e tame type.
-But what hagpenx hun expresions involed -
2 dtfrod
dota ypu, tikeh multiplying a fhting pont number

puch aaype of iuatons are haneled eithertnug


ype tonversíon or
-Type eonveeion or type cas ting t variables refers to
Changing a variable of one data ype into ano her.
ype Con vesion
-Iype tonvecsion 3 lon hun he exprens'on hay
var iabls ot diterent data typy.
ewpredion, he data type is promo td
fam lo wer to higker evl tere A hierarchy o data tp
CArom hirher do liuer) can be qiven as: double, flbat, lang
int, short nd char.
-pamation &done as belaw:
>fbat operards are con verded do doube.
>char or shot oprands heher xqrd er
converded oint.
>it ny one operand is doubl, oelher operand abo
>1} ony ont oporand is
i bng, e oher oprand i also
tenserted do Long, Hene he rsult is ato o ypelog
-these ehanges arare unaaiclobe whon gfeming pe
Eonversion, int vaka,
- when a float vabu s tonverded o an
Hhe ontional pard s truncitd.
whena euble vaue (onvted to a -(loat valu,
7ouneling of dryts is olond into int, k ees
-when a a dona int is (onvered
hiqher order bids ore dagd.

-ype casding is also Known as


Lon verS fonr

an arithmetie expre gions telly ke empi hr to


tasting
veprsent he vae ot he ppres'on in aa ardain ay
te a progran do teh at a floating peint namter
inclaels stdio.h>'
inlude <on/o.h
int moin C) - tnderpointanynubey
beg 3us

float f_num; -The indeger voriont dt


23,4 e2{
int i numj
lrser
pintf(n brder any floating pont number: ");
Rant ("t f" f-num);
inum (int)fnumj
printfln-the inteyer vaviont offis = d)
{-num, i-nun))
Node a gr qa) to
do con ver in to h
Grspondling floag pont number.
Fincde estio.h>
inclele <onio h
int main )
Yoat f-numj
iot inum;
clrser ()j );
prrt4( n Eoter any
+_num (fload )inumj
pontf ("hn tk foating
foadng pont
point ariant of
inum;fnum);

- 12
voiart of i2
12 is 12-00000.
The fosing goid

You might also like