0% found this document useful (0 votes)
80 views

PPSC

The document provides an introduction to programming using the C language (ES1101). It discusses three main topics: 1. Introduction to computers, including computer systems, environments, languages, and how programs are created and run. 2. Introduction to the C language, covering background, identifiers, types, variables, input/output, and common errors. 3. The structure of a C program, including expressions, precedence, side effects, statements, and command line arguments.

Uploaded by

arjunguttula11
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
80 views

PPSC

The document provides an introduction to programming using the C language (ES1101). It discusses three main topics: 1. Introduction to computers, including computer systems, environments, languages, and how programs are created and run. 2. Introduction to the C language, covering background, identifiers, types, variables, input/output, and common errors. 3. The structure of a C program, including expressions, precedence, side effects, statements, and command line arguments.

Uploaded by

arjunguttula11
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 109

PROGRAMMI

NGFORPROBLEM SOLVI
NGUSI
NGC(
ES1101)

UNITI
(SYLLABUS)

I
ntroductiont oComput ers:Comput erSystems, ComputingEnv i
ronment s,Comput er
l
anguages, Creat
ingandr unni
ngPr ograms, Comput erNumber i
ngSy st
em, Stori
ngI nteger
s,
St
or i
ngReal Number s
I
ntroductiont otheCLanguage:Backgr ound,CPr ograms,Identif
ier
s, Types,Variable,
Constants, I
nput/output
,Programmi ngExampl es,Scope,StorageCl assesandTy pe
Qualif
ier
s, TipsandCommonPr ogrammi ngEr r
orsKeyTer ms, Summar y,Practi
ceSeat .
Structur
eofaCPr ogram:ExpressionsPr ecedenceandAssoci ati
v i
ty,Si
deEf fects,
Evaluati
ngExpr essions,
Ty peConver si
onSt at
ement s,Si
mplePr ograms, CommandLi ne
Argument sTi psandCommonEr r
ors, KeyTerms, Summar y,Practi
ceSet s.

I
ntr
oduct
iont
oComput
ers:

Comput erisanelect
roni
cdevi
cethatrecei
vesi
nput
,stor
esorpr
ocessest
hei
nputas
peruserinstr
uct
ionsandprov
idesout
putindesi
redf
ormat.

I -
nputPr
oces
s-Out
putModel
Comput eri
nputiscall
eddataandtheoutputobt
ainedafterprocessingit,basedon
user’
si nst
ructi
ons i
s cal
ledi
nfor
mation.Raw facts and f i
gur
es whi ch can be
processedusingar
it
hmeti
candlogi
caloper
ati
onstoobtai
ni nf
ormationarecalleddat
a.

Thepr
ocessest
hatcanbeappl
i
edt
odat
aar
eoft
wot
ypes−

 Ar
it
hmet
ic oper
ati
ons− Exampl
es i
ncl
ude cal
cul
ati
ons l
i
ke addi
ti
on, subt
ract
ion,
di
ff
erent
ial
s,squar
eroot
,et
c.

 Logi
caloper
ati
ons−Exampl
esi
ncl
udecompar
isonoper
ati
onsl
i
kegr
eat
ert
han,
lesst
han,
equal
to,
opposi
te,
etc.
Thecor
respondi
ngf
igur
eforanact
ual
comput
erl
ookssomet
hingl
i
ket
his−

Thebasi
cpar
tsofacomput
erar
easf
oll
ows−

 I
nputUni
t−Dev
icesl
i
kekey
boar
dandmouset
hatar
eusedt
oinputdat
aandi
nst
ruct
ionst
o
t
hecomput
erar
ecal
l
edi
nputuni
t.

 Out
putUni
t−Dev
icesl
i
kepr
int
erandv
isualdi
spl
ayuni
tthatar
eusedt
opr
ovi
dei
nfor
mat
ion
t
otheuseri
ndesi
redf
ormatar
ecal
l
edout
putuni
t.

 Cont
rolUni
t−Ast
henamesuggest
s,t
hisuni
tcont
rol
sal
lthef
unct
ionsoft
hecomput
er.Al
l
dev
icesorpar
tsofcomput
eri
nter
actt
hrought
hecont
rol
uni
t.

 Ar
it
hmet
icLogi
cUni
t−Thi
sist
hebr
ainoft
hecomput
erwher
eal
lar
it
hmet
icoper
ati
onsand
l
ogi
cal
oper
ati
onst
akepl
ace.

 Memor
y−Al
linputdat
a,i
nst
ruct
ionsanddat
aint
eri
mtot
hepr
ocessesar
est
oredi
nthe
memor
y.Memor
yisoft
wo t
ypes–pr
imar
ymemor
yandsecondar
ymemor
y.Pr
imar
y
memor
yresi
deswi
thi
ntheCPUwher
eassecondar
ymemor
yisext
ernal
toi
t.

Controlunit,arithmeti
cl ogi
c unitand memor y aret oget
hercal l
ed thecentral
processingunitorCPU.Comput erdevi
cesli
kekeyboard,mouse,printer
,etc.thatwe
can see and t ouch aret hehardwarecomponents ofa comput er.The setof
i
nstructi
onsorpr ogramsthatmakethecomputerfuncti
onusingthesehar dwareparts
arecall
edsof t
war e.Wecannotseeortouchsof
tware.Bothhardwareandsof t
war ear
e
necessaryforwor ki
ngofacomputer.
Char
act
eri
st
ic
sofComput
er
Tounder
standwhycomput
ersar
esuchani
mpor
tantpar
tofourl
i
ves,l
etusl
ookat
someofi
tschar
act
eri
sti
cs−

 Speed−Ty
pical
l
y,acomput
ercancar
ryout3-
4mi
l
li
oni
nst
ruct
ionspersecond.

 Accur
acy−Comput
ersexhi
bitav
eryhi
ghdegr
eeofaccur
acy
.Er
ror
sthatmayoccurar
e
usual
l
yduet
oinaccur
atedat
a,wr
ongi
nst
ruct
ionsorbugi
nchi
ps–al
lhumaner
ror
s.

 Rel
iabi
li
ty−Comput
erscancar
ryoutsamet
ypeofwor
krepeat
edl
ywi
thoutt
hrowi
ngup
er
ror
sduet
oti
rednessorbor
edom,
whi
char
ever
ycommonamonghumans.

 Ver
sat
il
it
y−Comput
erscancar
ryoutawi
der
angeofwor
kfr
om dat
aent
ryandt
icket
booki
ngt
ocompl
exmat
hemat
icalcal
cul
ati
onsandcont
inuousast
ronomi
calobser
vat
ions.
I
fyou can i
nputt
he necessar
ydat
a wi
th cor
recti
nst
ruct
ions,comput
erwi
l
ldo t
he
pr
ocessi
ng.

 St
orageCapaci
ty−Comput
erscanst
oreav
eryl
argeamountofdat
aataf
ract
ionofcostof
t
radi
ti
onalst
orageoff
il
es.Al
so,dat
aissaf
efr
om nor
malwearandt
earassoci
atedwi
th
paper
.

Adv
ant
age
sofUs
ingComput
er
Nowthatweknowt
hechar
act
eri
sti
csofcomput
ers,wecanseet
headv
ant
agest
hat
comput
ersof
fer

 Comput
erscandot
hesamet
askr
epet
it
ivel
ywi
thsameaccur
acy
.

 Comput
ersdonotgett
ir
edorbor
ed.

 Comput
erscant
akeupr
out
inet
askswhi
l
erel
easi
nghumanr
esour
cef
ormor
eint
ell
i
gent
f
unct
ions.

Di
sadv
ant
age
sofUs
ingComput
er
Despi
tesomanyadv
ant
ages,
comput
ershav
esomedi
sadv
ant
agesoft
hei
rown−

 Comput
ershav
enoi
ntel
l
igence;t
heyf
oll
owt
hei
nst
ruct
ionsbl
i
ndl
ywi
thoutconsi
der
ingt
he
out
come.

 Regul
arel
ect
ri
csuppl
yisnecessar
ytomakecomput
erswor
k,whi
chcoul
dpr
ovedi
ff
icul
t
ev
ery
wher
eespeci
all
yindev
elopi
ngnat
ions.
Comput
erSy
stems:

Hi storical
ly computers were classi
fi
ed according t
o processor t
ypes because
dev elopment i n processor and pr ocessi
ng speeds wer e the devel
opment al
benchmar ks.Earli
estcomputersusedv acuum tubesforprocessi
ng,werehugeand
brokedownf requent
ly.However,asvacuum tubeswer er
eplacedbytransi
stor
sand
t
henchi
ps,
thei
rsi
zedecr
easedandpr
ocessi
ngspeedsi
ncr
easedmani
fol
d.

Al
lmoder
ncomput
ersandcomput
ingdev
icesusemi
cropr
ocessor
swhosespeedsand
stor
agecapaci
ti
esareskyr
ocket
ingdaybyday
.Thedev el
opmentalbenchmarkfor
computer
sisnowthei
rsi
ze.Computer
sar
enowclassi
fi
edonthebasi
soft hei
ruseor
si
ze−

 Deskt
op

 Lapt
op

 Tabl
et

 Ser
ver

 Mai
nfr
ame

 Super
comput
er

Letusl
ookatal
ltheset
ypesofcomput
ersi
ndet
ail
.

De
skt
op
Desktopcomput er
sarepersonalcomput
ers(PCs)desi
gnedforusebyanindivi
dualat
af i
xed locat
ion.IBM wast hefir
stcomputertointr
oduceand popular
izeuseof
desktops.A desktop uni
ttypi
call
yhasaCPU ( Cent
ralProcessi
ng Uni
t),monitor
,
keyboardandmouse.I ntr
oduct
ionofdeskt
opspopular
izeduseofcomput ersamong
commonpeopl easitwascompactandaff
ordabl
e.

Ridi
ngonthewaveofdeskt
op’
spopul
ari
tymanysoft
wareandhar
dwaredev
iceswere
devel
opedspeci
all
yfort
hehomeorof f
iceuser
.Thefor
emostdesi
gnconsider
ati
on
her
ewasuserf
ri
endl
i
ness.

Lapt
op
Despi
teit
shugepopul ari
ty,deskt
opsgavewayt o amor ecompactandpor table
per
sonalcomputer call
ed laptop i
n 2000s.Laptops are al
so callednot
ebook
computer
sorsi
mplynotebooks.Laptopsr
unusingbatter
iesandconnecttonet
wor ks
usingWi-
Fi(Wir
elessFi
del
i
ty)chi
ps.Theyalsohavechi
psforener
gyef
fi
ciencysot
hat
theycanconser
vepowerwheneverpossi
bleandhavealongerl
i
fe.

Moder
nlapt
opshaveenoughprocessi
ngpowerandst
oragecapaci
tyt
obeusedforal
l
of
fi
cework,
websi
tedesi
gning,
softwar
edevel
opmentandevenaudi
o/vi
deoedi
ti
ng.

Tabl
et
Afterlaptopscomputerswerefurt
hermini
atur
izedtodevel
opmachinesthathave
processingpowerofadesktopbutaresmal
lenoughtobehel
dinone’
spalm.Tabl
ets
havet ouchsensi
ti
vescreenoftypi
call
y5to10i ncheswher
eonef i
ngeri
susedt o
touchiconsandinvokeappl
i
cati
ons.

Keyboardi salsodispl
ayedvirtuall
ywheneverrequir
edandusedwi t
ht ouchstrokes.
Appli
cationst hatr
un on tabletsar ecall
edapps.Theyuseoper ati
ng systemsby
Microsoft(Windows8andl aterv ersi
ons)orGoogle(Android).Appl
ecomput ershave
developedtheirowntabl
etcall
edi Padwhichusesapr opr
ietaryOScall
edi OS.

Se
rve
r
Ser
ver
sar
ecomput
erswi
thhi
ghpr
ocessi
ngspeedst
hatpr
ovi
deoneormor
eser
vices
toothersy
stemsonthenet
work.Theymayormaynothavescreensat
tachedt othem.
A groupofcomputersordigi
taldev
icesconnect
edt
ogethertosharer esourcesis
cal
ledanetwork.

Ser
vershav
ehi
ghprocessi
ngpower
sandcanhandl
emul
ti
pler
equest
ssi
mul
taneousl
y.
Mostcommonl
yfoundserver
sonnet
wor
ksincl
ude−

 Fi
l
eorst
orageser
ver

 Gameser
ver

 Appl
i
cat
ionser
ver

 Dat
abaseser
ver

 Mai
lser
ver

 Pr
intser
ver

Mai
nfr
ame
Mainf
ramesar
ecomputer
susedbyor
ganizat
ionsl
ikebanks,ai
rl
inesandr
ail
waysto
handl
emill
i
onsandt
ri
ll
i
onsofonli
net
ransacti
onspersecond.I
mpor t
antf
eatur
esof
mainf
ramesar
e−

 Bi
ginsi
ze

 Hundr
edst
imesFast
ert
hanser
ver
s,t
ypi
cal
l
yhundr
edmegaby
tespersecond

 Ver
yexpensi
ve

 Usepr
opr
iet
aryOSpr
ovi
dedbyt
hemanuf
act
urer
s

 I
n-bui
l
thar
dwar
e,sof
twar
eandf
ir
mwar
esecur
it
yfeat
ures
Supe
rcomput
er
Supercomput er
sarethefastestcomputer
sonEar t
h.Theyareusedf orcarr
yingout
complex,f ast and ti
me int ensi
ve cal
cul
ati
ons for sci
ent
if
ic and engineeri
ng
appl
icati
ons.Supercomput
erspeedorper f
ormanceismeasuredint
erafl
ops,i
.e.1012
fl
oati
ngpointoperati
onspersecond.

Chinesesuper
computerSunwayTai
huLighti
stheworl
d’sfast
estsuper
computerwi
th
aratingof93petaf
lopspersecond,i
.e.93quadr
il
li
onf l
oati
ngpointoper
ati
onsper
second.

Mostcommonusesofsuper
comput
ersi
ncl
ude−

 Mol
ecul
armappi
ngandr
esear
ch

 Weat
herf
orecast
ing

 Env
ironment
alr
esear
ch

 Oi
landgasexpl
orat
ion

Comput
ingEnv
ironment
s:-

C-Env
ironmentSet
up
I
fy ouwanttosetupyourenv
ironmentf
orCpr ogramminglanguage,youneedt
he
fol
l
owingtwosof
twar
etool
savailabl
eonyourcomputer
,(a)TextEdi
torand(
b)TheC
Compil
er.

TextEdi
tor
Thi
swillbeusedtotypeyourpr
ogr
am.Exampl
esoff
ew aedi
tor
sincl
udeWi
ndows
Not
epad,OSEditcommand.
Thenameandv
ersi
onoft
extedi
tor
scanv
aryondi
ff
erentoper
ati
ngsy
stems.
Thefil
esyoucreat
ewithy
ouredi
torar
ecal
l
edt
hesour
cef
il
esandt
heycont
aint
he
progr
am sour
cecodes.
Thesour
cef
il
esf
orCpr
ogr
amsar
ety
pical
l
ynamedwi
tht
heext
ensi
on"
.c"
.
Bef
orest
art
ingy
ourpr
ogr
ammi
ng,
makesur
eyouhav
eonet
extedi
tori
npl
aceandy
ou
hav
eenoughexper
iencet
owr
it
eacomput
erpr
ogr
am,sav
eiti
naf
il
e,compi
l
eitand
f
inal
l
yexecut
eit
.

TheCCompi
l
er
Thesour
cecodewri
tteni
nsourcefi
lei sthehumanr
eadablesour
cefory
ourprogr
am.
I
tneedstobe"
compil
ed",i
ntomachinelanguagesot
hatyourCPUcanact
ual
lyexecut
e
thepr
ogr
am aspert
heinst
ruct
ionsgiven.
Thecompi
l
ercompi
l
est
hesour
cecodesi
ntof
inal
execut
abl
epr
ogr
ams.

Comput
erLanguages:
-

Theuserofacomputermustbeabletocommuni cat
ewit
hit.Thatmeans,
hemustbeabletogivethecomputercommandsandunderstandtheout
put
thatt
hecomputergener
ates.Thi
sispossi
bleduetothei
nventi
onof
computerl
anguages.

Basical
ly,t
her
earetwomaincat egor
iesofcomputerl
anguages,
namelyLow
LevelLanguageandHighLevelLanguage.Letust
akeabriefl
ookatboth
thesetypesofcomput
erlanguages.

1)LowLev
elLanguages:
-

Lowlevell
anguagesarethebasiccomput erinst
ruct
ionsorbet t
erknownas
machinecodes.Acomput ercannotunderstandanyinstructi
ongivent
oitby
theuseri
nEnglishoranyotherhighlevell
anguage.Thesel owlevel
l
anguagesareveryeasil
yunderstandablebythemachi ne.

Themai nfuncti
onofl owlevell
anguagesist
ointer
actwi
ththehardwareof
thecomput er.
Theyhel pinoper
ati
ng,synci
ngandmanagingallthehar
dware
andsystem component softhecomput er
.Theyhandl
eal
ltheinstr
ucti
ons
whichformt hearchi
tectureoft
hehardwaresyst
ems.

Machi
neLanguage

Thisisoneofthemostbasicl
owl ev
ellanguages.Thelanguagewasfi
rst
devel
opedtointer
actwi
ththefi
rstgenerat
ioncomput er
s.Iti
swri
tt
enin
binar
ycodeormachinecode,whichmeansi tbasical
l
ycompr i
sesofonl
y
twodigit
s–1and0.

Assembl
yLanguage

Thi
sist
hesecondgener
ati
onpr
ogr
ammi
ngl
anguage.I
tisadev
elopmenton
themachinelanguage,whereinst
eadofusi
ngonlynumbers,weuseEngl
i
sh
words,names,andsy mbols.I
tisthemostbasi
ccomputerlanguage
necessaryf
oranypr ocessor
.

2)Hi
ghLev
elLanguage

Whenwetal
kabouthi
ghlev
ellanguages,t
hesear
eprogr
ammingl
anguages.
Somepr
ominentex
amplesarePASCAL, FORTRAN,C++et
c.

Theimpor t
antfeatureaboutsuchhi ghlevel
languagesisthatt
heyallowthe
programmert owriteprogramsf orall
typesofcomput er
sandsy stems.
Everyinst
ructi
oninhighl evel
languageisconvert
edt omachinelanguagefor
thecomput ertocompr ehend.

Scr
ipt
ingLanguages

Scri
pti
nglanguagesorscri
ptsareessent
ial
l
yprogr
ammi ng
l
anguages.Theselanguagesemployahighlev
elconst
ructwhi
ch
al
lowsitt
oi nt
erpr
etandex ecut
eonecommandatat i
me.

Scri
ptingl
anguagesar eeasi
ertol
earnandexecutethan
compiledlanguages.Someex ampl
esareAppleScri
pt,Jav
aScr
ipt
,
Pearletc.

Obj
ect
-Or
ient
edLanguages
Thesear
ehighlev
ell
anguagesthatfocusonthe‘
obj
ect
s’r
athert
hant
he

acti
ons’
.Toaccompl
ishthi
s,t
hef ocuswil
lbeondat
athanl
ogic.

Thereasoningbehindisthatthepr ogr
ammer sr eal
l
ycar esabouttheobj
ect
theywishtomanipulateratherthanthelogicneededt omanipulat
ethem.
Someex amplesincludeJav a,C+,C++,Python,Swiftetc.

Pr
ocedur
alPr
ogr
ammi
ngLanguage
Thisi
satypeofpr
ogr
amminglanguaget
hathaswell
str
uct
uredstepsand
complexpr
ocedur
eswi
thi
nit
sprogrammingtocomposeacomplete
progr
am.

I
thasasystemat
icorderf
unct
ionsandcommandst
ocompleteataskora
pr
ogram.FORTRAN,ALGOL,BASIC,COBOLar
esomeexamples.

Cr
eat
ingandRunni
ngPr
ogr
ams:
-
Cr
eat
ingandRunni
ngCPr
ogr
am
Gener
all
y,t
hepr
ogr
amscr
eat
edusi
ngpr
ogr
ammi
ngl
anguagesl
i
keC,ar
ewr
it
tenusi
ng
ahi
gh-
lev
ell
anguagel
i
keEngl
i
sh.But
,thecomput
ercannotunder
standt
hehi
gh-
lev
el
l
anguage.I
tcanunder
standonl
ylow-
lev
ell
anguage.So,
thepr
ogr
am wr
it
teni
nthehi
gh-

l
evell
anguage needs t
o be conv
ert
ed i
ntot
he l
ow-
lev
ell
anguage t
o make i
t
under
standabl
efort
hecomput
er.

Popul
arpr
ogr
ammi
ngl
anguagesl
i
keC,C++,Jav
a,et
c.,uset
hecompi
l
ert
oconv
erthi
gh-
lev
el

l
anguagei
nst
ruct
ionsi
ntol
ow-
lev
ell
anguagei
nst
ruct
ions.Acompi
l
eri
sapr
ogr
am t
hatconv
ert
s

hi
gh-
lev
ell
anguage i
nst
ruct
ions i
ntol
ow-
lev
ell
anguage i
nst
ruct
ions.Gener
all
y,t
he compi
l
er

per
for
mst
wot
hings,f
ir
sti
tver
if
iest
hepr
ogr
am er
ror
s,i
fer
ror
sar
efound,i
tret
urnsal
i
stofer
ror
s

ot
her
wisei
tconv
ert
sthecompl
etecodei
ntot
hel
ow-
lev
ell
anguage.

Tocr
eat
eandexecut
eCpr
ogr
amsi
ntheWi
ndowsOper
ati
ngSy
stem,weneedt
oinst
allTur
boC

sof
twar
e.Weuset
hef
oll
owi
ngst
epst
ocr
eat
eandexecut
eCpr
ogr
amsi
nWi
ndowsOS…

St
ep1:Cr
eat
ingaSour
ceCode

Sour
cecodei
saf
il
ewi
thCpr
ogr
ammi
ngi
nst
ruct
ionsi
nahi
gh-
lev
ell
anguage.Tocr
eat
esour
ce

code,
weuseanyt
extedi
tort
owr
it
ethepr
ogr
am i
nst
ruct
ions.Thei
nst
ruct
ionswr
it
teni
nthesour
ce

codemustf
oll
ow t
heC pr
ogr
ammi
ngl
anguager
ules.Thef
oll
owi
ngst
epsar
eusedt
ocr
eat
ea

sour
cecodef
il
einWi
ndowsOS…

 Cl
i
ckont
heSt
artbut
ton

 Sel
ectRun

 Ty
pecmdandpr
essEnt
er
 Ty
pecdc:
\TC\
bini
nthecommandpr
omptandpr
essEnt
er

 Ty
peTCpr
essEnt
er

 Cl
i
ckonFi
le-
>Newi
nCEdi
torwi
ndow

 Ty
pet
hepr
ogr
am

 Sav
eitasFi
leName.
c(Useshor
tcutkeyF2t
osav
e)

St
ep2:Compi
leSour
ceCode(
Alt+F9)

Thecompi
l
ati
oni
sthepr
ocessofconv
ert
inghi
gh-
lev
ell
anguagei
nst
ruct
ionsi
ntol
ow-
lev
ell
anguage

i
nst
ruct
ions.Weuset
heshor
tcutkeyAl
t+F9t
ocompi
l
eaCpr
ogr
am i
nTur
boC.

Thecompi
lat
ioni
sthepr
ocessofconv
ert
inghi
gh-
lev
ell
anguagei
nst
ruct
ionsi
ntol
ow-
lev
el

l
anguagei
nst
ruct
ions.

Whenev
erwepr
essAl
t+F9,
thesour
cef
il
eisgoi
ngt
obesubmi
tt
edt
otheCompi
l
er.Onr
ecei
vi
nga

sour
cef
il
e,t
hecompi
l
erf
ir
stchecksf
ort
heEr
ror
s.I
fther
ear
eanyEr
ror
sthencompi
l
err
etur
nsLi
st

ofEr
ror
s,i
fther
ear
enoer
ror
sthent
hesour
cecodei
sconv
ert
edi
ntoobj
ectcodeandst
oresi
tasa

f
il
ewi
th.
objext
ensi
on.Thent
heobj
ectcodei
sgi
vent
otheLi
nker
.TheLi
nkercombi
nesbot
h

t
heobj
ect codeand speci
fi
edheader f
il
ecode and gener
ates anExecut
abl
e f
il
ewi
th

a.
exeext
ensi
on.

St
ep3:Execut
ing/Runni
ngExecut
abl
eFi
le(
Ctr
l+F9)

Af
tercompl
eti
ngcompi
l
ati
onsuccessf
ull
y,anexecut
abl
efi
l
eiscr
eat
edwi
tha.
exeext
ensi
on.The

pr
ocessorcanunder
standt
his.
exef
il
econt
entsot
hati
tcanper
for
mthet
askspeci
fi
edi
nthe

sour
cef
il
e.

Weuseashor
tcutkeyCt
rl+F9t
orunaCpr
ogr
am.Whenev
erwepr
essCt
rl+F9,t
he.
exef
il
eis

submi
tt
edt
otheCPU.Onr
ecei
vi
ng.
exef
il
e,CPUper
for
mst
het
askaccor
dingt
othei
nst
ruct
ion

wr
it
teni
nthef
il
e.Ther
esul
tgener
atedf
rom t
heexecut
ioni
spl
acedi
nawi
ndowcal
l
edUserScr
een.

St
ep4:CheckResul
t(Al
t+F5)

Af
terr
unni
ngt
hepr
ogr
am,t
her
esul
tispl
acedi
ntoUserScr
een.Justweneedt
oopent
heUser

Scr
eent
ocheckt
her
esul
toft
hepr
ogr
am execut
ion.Weuset
heshor
tcutkeyAl
t+F5t
oopent
he
UserScr
eenandcheckt
her
esul
t.

Execut
ionPr
ocessofaCPr
ogr
am

Whenweexecut
eaCpr
ogr
am i
tunder
goeswi
tht
hef
oll
owi
ngpr
ocess…

I
mpor
tantPoi
nts
 Cprogram fi
le(Sour cefil
e)mustsav ewi th.cext
ension.
 Thecompi l
erconv ert
scompl eteprogram atat i
mef r
om high-
levellanguagetol
ow-l
evel
l
anguage.
 I
nputt othe compi l
eris. cfil
e and out putfr
om t he compil
eri s.exefil
e,buti
talso
gener
ates.objfi
lei nthisprocess.
 Thecompilerconv ertsthefil
eonl yi
ftherearenoerrorsi
nthesour cecode.
 CPUplacesther esultinUserScr eenwi ndow.
Comput
erNumberSy
stem:
-

Whenwet ypesomel ett


ersorwords,t
hecomput ertr
anslat
est hem innumbersas
computerscanunderstandonl
ynumbers.Acomput ercanunder standtheposi
ti
onal
numbersy st
em wheretherear
eonlyaf ew sy
mbolscall
eddi gi
tsandt hesesymbol
s
repr
esentdif
fer
entval
uesdependi
ngonthepositi
ontheyoccupyinthenumber.
Thev
alueofeachdi
giti
nanumbercanbedet
ermi
nedusi
ng−
 Thedi
git
 Theposi
ti
onoft
hedi
giti
nthenumber
 Thebaseofthenumbersyst
em (
wher
ethebasei
sdef
inedast
het
otalnumberofdi
git
s
avai
l
abl
einthenumbersy
stem)

Deci
malNumberSy
stem
Thenumbersy st
em thatweusei nourday
-to-
dayl
if
eisthedeci
malnumbersystem.
Decimalnumbersy st
em hasbase10asi tuses10digi
tsfr
om 0t o9.Indecimal
numbersystem,thesuccessiveposi
ti
onstothelef
tofthedecimalpoi
ntrepr
esent
uni
ts,t
ens,hundr
eds,thousands,
andsoon.
Eachposi
ti
onrepresent
saspecifi
cpowerofthebase( 10).Forexample,thedeci
mal
number1234consist
softhedi
git4int
heunit
spositi
on, 3inthetensposit
ion,
2inthe
hundr
edsposi
ti
on,and1i nt
het
housandsposi
tion.I
tsvaluecanbewr i
tt
enas
(1x1000)
+(2x100)
+(3x10)
+(4xl
)
(1x10)
3
+(2x10)
+(3x10)
2
+(
4xl
1
0) 0

1000+200+30+4
1234
Asacomputerpr
ogr
ammeroranITprof
essional
,youshoul
dunder
standt
hef
oll
owi
ng
numbersy
stemswhi
char
efr
equentl
yusedincomputers.

S.
No. NumberSy
stem andDescr
ipt
ion

1 Bi
nar
yNumberSy
stem
Base2.Di
git
sused:
0,1

2 Oct
alNumberSy
stem
Base8.Di
git
sused:
0to7

3 HexaDeci
malNumberSy
stem
Base16.Di
git
sused:
0to9,
Let
ter
sused:
A-F
Bi
nar
yNumberSy
stem
Char
act
eri
sti
csoft
hebi
nar
ynumbersy
stem ar
easf
oll
ows−
 Usest
wodi
git
s,0and1
 Al
socal
l
edasbase2numbersy
stem
 Eachposi
ti
oni
nabi
nar
ynumberr
epr
esent
sa0poweroft
hebase(
2).Exampl
e20
 Lastposi
ti
on in a binar
y numberr epr
esent
s axpower of t
he base (
2).Ex
ampl
e
2xwher
exr epr
esent
sthelastposi
ti
on-1.

Exampl
e
Bi
nar
yNumber
:101012
Cal
cul
ati
ngDeci
mal
Equi
val
ent−

St
ep Bi
nar
yNumber Deci
malNumber

St
ep1 101012 (
(1x24)+(
0x23)+(
1x22)+(
0x21)+(
1x20)
)10

St
ep2 101012 (
16+0+4+0+1)
10

St
ep3 101012 2110

Not
e−101012 i
snor
mal
l
ywr
it
tenas10101.

Oct
alNumberSy
stem
Char
act
eri
sti
csoft
heoct
alnumbersy
stem ar
easf
oll
ows−
 Usesei
ghtdi
git
s,0,
1,
2,
3,
4,
5,
6,
7
 Al
socal
l
edasbase8numbersy
stem
 Eachposi
ti
oni
nanoct
alnumberr
epr
esent
sa0poweroft
hebase(
8).Exampl
e80
 Lastposi
ti
on in an oct
alnumberr epr
esent
s axpowerof t
he base (
8).Ex
ampl
e
8xwher
exr epr
esent
sthel
astposi
ti
on-1

Exampl
e
Oct
alNumber
:125708
Cal
cul
ati
ngDeci
mal
Equi
val
ent−

St
ep Oct
alNumber Deci
malNumber

St
ep1 125708 (
(1x84)+(
2x83)+(
5x82)+(
7x81)+(
0x80)
)10

St
ep2 125708 (
4096+1024+320+56+0)
10
St
ep3 125708 549610

Not
e−125708 i
snor
mal
l
ywr
it
tenas12570.

Hexadeci
malNumberSy
stem
Char
act
eri
sti
csofhexadeci
mal
numbersy
stem ar
easf
oll
ows−
 Uses10di
git
sand6l
ett
ers,
0,1,
2,3,
4,5,
6,7,
8,9,
A,B,
C,D,
E,F
 Let
ter
srepr
esentt
henumber
sst
art
ingf
rom 10.A=10.B=11,
C=12,
D=13,
E=14,
F=15
 Al
socal
l
edasbase16numbersy
stem
 Eachposi
ti
oni
nahexadeci
mal
numberr
epr
esent
sa0poweroft
hebase(
16)
.Exampl
e,160
 Lastposi
ti
oninahexadeci
malnumberr epr
esent
saxpoweroft
hebase(
16)
.Ex
ampl
e
16xwherexrepr
esent
sthel
astposi
ti
on-1

Exampl
e
Hexadeci
mal
Number
:19FDE16
Cal
cul
ati
ngDeci
mal
Equi
val
ent−

St
ep Bi
nar
y Deci
malNumber
Number

St
ep1 19FDE16 (
(1x164)+(
9x163)+(
Fx162)+(
Dx161)+(
Ex160)
)10

St
ep2 19FDE16 (
(1x164)+(
9x163)+(
15x162)+(
13x161)+(
14x160)
)10

St
ep3 19FDE16 (
65536+36864+3840+208+14)
10

St
ep4 19FDE16 10646210

Not
e−19FDE16 i
snor
mal
l
ywr
it
tenas19FDE.
STORI
NGI
NTEGERS:
-

Dataty
pesincref
ert
oanextensi
vesystem usedf ordecl
ari
ngv
ari
abl
esorf
unctionsof
di
ff
erentty
pes.Thetypeofav ari
abl
edet ermineshow muchspacei toccupiesin
st
orageandhowthebi
tpat
ternst
oredisint
erpreted.
Thet
ypesi
nCcanbecl
assi
fi
edasf
oll
ows−

Sr
.No. Ty
pes&Descr
ipt
ion

1
Basi
cTy
pes
Theyar
ear
it
hmet
ict
ypesandar
efur
thercl
assi
fi
edi
nto:(
a)i
ntegert
ypesand(
b)f
loat
ing-
poi
nt
ty
pes.

2
Enumer
atedt
ypes
Theyareagainari
thmeti
ctypesandt heyareusedtodef
inev
ari
abl
est
hatcanonl
yassi
gn
cer
tai
ndiscr
etei
ntegerv
aluesthr
oughoutthepr
ogram.

3
Thet
ypev
oid
Thet
ypespeci
ferv
i oidi
ndi
cat
est
hatnov
aluei
sav
ail
abl
e.

4
Der
ivedt
ypes
Theyincl
ude( a)Poi
ntert
ypes,(
b)Ar
rayt
ypes,(
c)St
ruct
uret
ypes,(
d)Uni
ont
ypesand(
e)
Funct
iontypes.

Thearraytypesandstructur
et y
pesar eref
erredcoll
ect
ivelyastheaggr
egatetypes.
Thetypeofaf uncti
onspecif
iesthetypeofthefuncti
on'
sreturnv
alue.Wewil
lseethe
basi
ct ypesint hefol
lowi
ngsect ion,wher
easot hertypeswillbecover
edi nthe
upcomingchapters.

I
ntegerTy
pes
Thefol
lowi
ngtableprov
idest
hedet
ail
sofst
andar
dint
egert
ypeswi
tht
hei
rst
orage
si
zesandval
ueranges−

Ty
pe St
oragesi
ze Val
uer
ange

char 1by
te -
128t
o127or0t
o255

unsi
gnedchar 1by
te 0t
o255

si
gnedchar 1by
te -
128t
o127

i
nt 2or4by
tes -
32,
768t
o32,
767or-
2,147,
483,
648t
o2,
147,
483,
647
unsi
gnedi
nt 2or4by
tes 0t
o65,
535or0t
o4,
294,
967,
295

shor
t 2by
tes -
32,
768t
o32,
767

unsi
gnedshor
t 2by
tes 0t
o65,
535

l
ong 8by
tes -
9223372036854775808t
o9223372036854775807

unsi
gnedl
ong 8by
tes 0t
o18446744073709551615

Togett heexactsi zeofat ypeorav ari


ableonapar ticul
arplatf
orm,youcanuse
thesizeofoper at
or.Theexpr onssi
essi zeof(t
ype)yi
eldsthest or
agesizeoftheobj
ect
ortypeinby tes.Gi
venbel owisanexampletogetthesizeofv ari
oustypeonamachine
usingdiffer
entconst antdef
inedinli
mit
s.hheaderf
il
e−

STORI
NGREALNUMBERS:
-

Fl
oat
ing-
Poi
ntTy
pes
Thefol
lowi
ngtableprov
idethedetai
lsofst
andar
dfl
oat
ing-
poi
ntt
ypeswi
thst
orage
si
zesandval
uerangesandthei
rpr
ecisi
on−

Ty
pe St
oragesi
ze Val
uer
ange Pr
eci
sion

f
loat 4by
te 1.
2E-
38t
o3.
4E+38 6deci
mal
places

doubl
e 8by
te 2.
3E-
308t
o1.
7E+308 15deci
mal
places

l
ongdoubl
e 10by
te 3.
4E-
4932t
o1.
1E+4932 19deci
mal
places

Theheaderfi
l
efl
oat.
hdefi
nesmacrosthatall
ow youtousetheseval
uesandot
her
det
ail
saboutt
hebi
naryr
epr
esent
ati
onofrealnumbersi
nyourpr
ograms.
I
NTRODUCTI
ONTOCLANGUAGE:
-

Cisageneral
-purpose,
high-l
evel
languagethatwasor i
ginal
l
ydev el
opedbyDenni
sM.
Ri
tchi
ein1972t odevel
opt heUNIXoperat
ingsystem atBellLabs.

The UNI
X operati
ng sy
stem,the C compi
l
er,and essent
ial
l
yallUNI
X appl
icati
on
progr
amshav ebeenwr it
teni
nC.C hasnow becomeawi del
yusedpr
ofessional
l
anguageforv
ariousr
easons−

 Easyt
olear
n

 St
ruct
uredl
anguage

 I
tpr
oducesef
fi
cientpr
ogr
ams

 I
tcanhandl
elow-
lev
elact
ivi
ti
es

 I
tcanbecompi
l
edonav
ari
etyofcomput
erpl
atf
orms

Fact
saboutC
 Cwasi
nvent
edt
owr
it
eanoper
ati
ngsy
stem cal
l
edUNI
X.
 Ci
sasuccessorofBl
anguagewhi
chwasi
ntr
oducedar
oundt
heear
ly1970s.
 Thel
anguagewasf
ormal
i
zedi
n1988byt
heAmer
icanNat
ional
Standar
dInst
it
ute(
ANSI
).
 TheUNI
XOSwast
otal
l
ywr
it
teni
nC.
 TodayCi
sthemostwi
del
yusedandpopul
arSy
stem Pr
ogr
ammi
ngLanguage.
 Mostoft
hest
ate-
of-
the-
artsof
twar
ehav
ebeeni
mpl
ement
edusi
ngC.
 Today
'smostpopul
arLi
nuxOSandRDBMSMy
SQLhav
ebeenwr
it
teni
nC.

WhyuseC?
Cwasiniti
all
yusedf orsystem developmentwork,par
ti
cul
arl
ytheprogramsthatmake-
uptheoperati
ngsy stem.Cwasadopt edasasy st
em dev
elopmentlanguagebecause
i
tproducescodet hatr unsnearlyasf astasthecodewr i
tt
eninassembl ylanguage.
Someexampl esoftheuseofCmi ghtbe−

 Oper
ati
ngSy
stems

 LanguageCompi
l
ers

 Assembl
ers

 Tex
tEdi
tor
s

 Pr
intSpool
ers

 Net
wor
kDr
iver
s

 Moder
nPr
ogr
ams

 Dat
abases
 LanguageI
nter
pret
ers

 Ut
il
it
ies
Feat
uresofCLanguage
Ci
sthewi
del
yusedl
anguage.I
tpr
ovi
desmanyf
eat
urest
hatar
egi
venbel
ow.

1. Si
mpl
e
2. Machi
neI
ndependentorPor
tabl
e
3. Mi
d-l
evel
progr
ammi
ngl
anguage
4. st
ruct
uredpr
ogr
ammi
ngl
anguage
5. Ri
chLi
brar
y
6. Memor
yManagement
7. FastSpeed
8. Poi
nter
s
9. Recur
sion
10.Ext
ensi
ble

CPr
ogr
ams
ACprogr
am canvar
yfr
om 3li
nestomi
l
li
onsofl
i
nesandi
tshoul
dbewr
it
teni
ntoone
ormor
etextf
il
eswit
hext
ensi
on".
c".

C-Pr
ogr
am St
ruct
ure

l
etusl
ookatabar
emi
nimum Cpr
ogr
am st
ruct
ure,

ACpr
ogr
am basi
cal
l
yconsi
stsoft
hef
oll
owi
ngpar
ts−

 Pr
epr
ocessorCommands

 Funct
ions
 Var
iabl
es

 St
atement
s&Expr
essi
ons

 Comment
s

Letust
akeal
ookatt
hev
ari
ouspar
tsoft
heabov
epr
ogr
am −
 Thef
ir
stli
neofthepr
ogram #i
ncl
ude<st
dio.
h>i
saprepr
ocessorcommand,
whi
cht
ell
saC
compil
ert
oincl
udest
dio.
hfil
ebef
oregoi
ngtoact
ual
compil
ation.
 Thenex nei
tl
i ntmai
n()i
sthemai
nfunct
ionwher
ethepr
ogr
am execut
ionbegi
ns.
 Thenextl
i
ne/*..
.*
/wil
lbeignor
edbythecompil
erandi
thasbeenputtoaddaddi
ti
onal
commentsint
heprogr
am.Sosuchl
i
nesarecal
l
edcommentsi
nthepr
ogram.
 Thenextlnepr
i int
f(.
..
)isanotherfunct
ionav
ail
abl
einCwhi
chcausest
hemessage"
Hel
l
o,
Worl
d!"tobedi
spl
ayedont hescreen.
 Thenextl
i
ner
etur
n0;
ter
minat
est
hemai
n()f
unct
ionandr
etur
nst
hev
alue0.

I
dent
if
ier
s
ACident
ifi
erisanameusedt oidenti
fyavar
iabl
e,funct
ion,oranyotheruser
-def
ined
i
tem.Ani
dentif
ierstart
swithalet
terAtoZ,atoz,oranunderscore'
_'fol
l
owedbyzer o
ormorel
ett
ers,underscor
es,anddigi
ts(0t
o9).
Cdoesnotal low punct
uat
ionchar
acterssuchas@,$,and% withini
dent
ifi
ers.Cis
acase-sensi
ti
veprogramming l
anguage.Here ar
e some examples ofacceptabl
e
i
denti
fi
ers−
mohd z ara abc mov
e_namea_123
myname50 _
temp j a23b9 ret
Val

Var
iabl
es
Av ari
ablei snothi
ngbutanamegi ventoast orageareathatourpr ogr
amscan
manipulate.Eachv ari
abl
einC hasaspeci fi
ctype,whichdeterminesthesizeand
l
ayoutoft hevar
iable'
smemor y;t
her angeofval
uest hatcanbest or
edwi t
hint
hat
memor y;andthesetofoperat
ionsthatcanbeappl
iedtothevar
iabl
e.
The nameofa v ari
ablecan be composed oflet
ter
s,digi
ts,and theunderscore
character
.Itmustbeginwitheit
heral et
teroranunderscor
e.Upperandl owercase
l
ettersaredi
sti
nctbecauseCiscase-
sensi
tiv
e.fol
l
owingbasicvar
iabl
et y
pes–
Sr
.No. Ty
pe&Descr
ipt
ion

1
char
Ty
pical
l
yasi
ngl
eoct
et(
oneby
te)
.Thi
sisani
ntegert
ype.

2
i
nt
Themostnat
ural
sizeofi
ntegerf
ort
hemachi
ne.

3
f
loat
Asi
ngl
e-pr
eci
sionf
loat
ingpoi
ntv
alue.

4
doubl
e
Adoubl
e-pr
eci
sionf
loat
ingpoi
ntv
alue.

5
v
oid
Repr
esent
stheabsenceoft
ype.

Var
iabl
eDef
ini
ti
oni
nC
Avar
iabledefi
niti
ontel
lsthecompil
erwher
eandhowmuchst or
agetocr
eatef
orthe
v
ari
able.Avariabl
edefini
ti
onspeci
fi
esadatat
ypeandcont
ainsali
stofoneormore
v
ari
ablesofthattypeasfol
lows−
t
ypev
ari
abl
e_l
i
st;
Her
e,t
ypemustbeav
ali
dCdat
aty
pei
ncl
udi
ngchar
,w_
char
,int
,fl
oat
,doubl
e,bool
,or
anyuser
-def
inedobj
ect
;andv
ari
abl
e_l
istmayconsi
stofoneormor
eident
if
iernames
separ
atedbycommas.Somev
ali
ddecl
arat
ionsar
eshownher
e−

i
nt i,j
,k;
charc, ch;
fl
oatf,salar
y;
doubl
ed;

Thel
i
nei
nti
,j,k;
decl
aresanddef
inest
hev
ari
abl
esi
,j,andk;whi
chi
nst
ructt
he
compi
l
ert
ocr
eat
evar
iabl
esnamedi
,jandkoft
ypei
nt.

Var
iabl
escanbei
nit
ial
i
zed(
assi
gnedani
nit
ialv
alue)i
nthei
rdecl
arat
ion.Thei
nit
ial
i
zer
consi
stsofanequal
signf
oll
owedbyaconst
antexpr
essi
onasf
oll
ows−

t
ypev
ari
abl
e_name=v
alue;
Someexampl
esar
e−
i
ntd=3,f=5; //def
ini
ti
onandi
nit
ial
izi
ngdandf.
charx='
x'
; /
/thevar
iabl
exhasthevalue'
x'
.

Fordefini
ti
onwit
houtanini
ti
ali
zer
:vari
abl
eswit
hstat
icstor
agedurat
ionarei
mpli
cit
ly
i
niti
ali
zedwithNULL(al
lbyt
eshav etheval
ue0);t
hei
nit
ialval
ueofal
lotherv
ari
abl
es
areundefi
ned.

Var
iabl
eDecl
arat
ioni
nC
 Av
ari
abl
edecl
arat
ionpr
ovi
desi
nfor
mat
iont
othecompi
l
ert
hatt
her
eex
ist
sa
v
ari
abl
ewi
tht
hegi
vent
ypeandnamesot
hatt
hecompi
l
ercanpr
oceedf
or
f
urt
hercompi
l
ati
onwi
thoutr
equi
ri
ngt
hecompl
etedet
ail
aboutt
hev
ari
abl
e.

 Av
ari
abl
e def
ini
ti
on hasi
tsmeani
ng att
he t
ime ofcompi
l
ati
on onl
y,t
he
compi
l
erneedsact
ual
var
iabl
edef
ini
ti
onatt
het
imeofl
i
nki
ngt
hepr
ogr
am.
Key
wor
ds
Thefol
lowi
nglistshowsthereser
vedwordsinC.Theser eser
vedwor
dsmaynotbe
usedasconst
antsorvar
iabl
esoranyot
herident
if
iernames.

aut
o el
se l
ong swi
tch

br
eak enum r
egi
ster t
ypedef

case ex
ter
n r
etur
n uni
on

char f
loat shor
t unsi
gned

const f
or si
gned v
oid

cont
inue got
o si
zeof v
olat
il
e

def
aul
t i
f st
ati
c whi
l
e

do i
nt st
ruct _
Packed

doubl
e

Const
ant
s:-

Constantsrefertofi
xedvaluesthattheprogr
am maynotal
terdur
ingi
tsexecut
ion.
Thesefixedvaluesar
ealsocall
edli
ter
als.
Const
ant
scanbeofanyoft hebasi
cdat
aty keani
pesl
i ntegerconst
ant
,af
loat
ing
const
ant
,achar
act
erconst
ant
,orast
ri
ngl
i
ter
al.
Const
antsaretreat
edj ustli
ker
egul
arv
ari
abl
esexceptt
hatt
hei
rval
uescannotbe
modif
iedaf
tert
heirdef
initi
on.
Def
ini
ngConst
ant
s
Ther
ear
etwosi
mpl
eway
sinCt
odef
ineconst
ant
s−
 Usi
ng#def
inepr
epr
ocessor
.
 Usi
ngconstkey
wor
d.

The#def
inePr
epr
ocessor
Gi
venbel
owi
sthef
ormt
ouse#def
inepr
epr
ocessort
odef
ineaconst
ant−
#def
inei
dent
if
ierv
alue
Thef
oll
owi
ngexampl
eexpl
ainsi
tindet
ail

#i
ncl
ude<st
dio.
h>

#def
ineLENGTH10
#def
ineWIDTH 5
#def
ineNEWLINE'\
n'

i
ntmai
n(){
i
ntar
ea;

area=LENGTH*WI DTH;
pri
ntf(
"val
ueofarea:%d"
,ar
ea)
;
pri
ntf(
"%c",NEWLINE)
;
retur
n0;
}
Whent
heabov
ecodei
scompi
l
edandexecut
ed,
itpr
oducest
hef
oll
owi
ngr
esul
t−
v
alueofar
ea:
50

TheconstKey
wor
d
Youcanuseconstpr
efi
xtodecl
areconst
ant
swi
thaspeci
fi
cty
peasf
oll
ows−
constt
ypev
ari
abl
e=v
alue;
Thef
oll
owi
ngexampl
eexpl
ainsi
tindet
ail

#i
ncl
ude<st
dio.
h>

i
ntmain(){
consti
ntLENGTH=10;
consti
ntWI DTH=5;
constcharNEWLINE=' \
n';
i
ntarea;
area=LENGTH*WI DTH;
pri
ntf
("val
ueofarea:%d",ar
ea)
;
pri
ntf
("%c",NEWLINE)
;

ret
urn0;
}
C-ScopeRul
es

 Ascopei
nanypr
ogr
ammi
ngi
sar
egi
onoft
hepr
ogr
am wher
eadef
inedv
ari
abl
e
canhav
eit
sexi
stenceandbey
ondt
hatv
ari
abl
eitcannotbeaccessed.

Ther
ear
ethr
eepl
aceswher
evar
iabl
escanbedecl
aredi
nCpr
ogr
ammi
ngl
anguage−

 I
nsi
deaf
unct
ionorabl
ockwhi
chi
scal
l
edl
ocalv
ari
abl
es.

 Out
sideofal
lfunct
ionswhi
chi
scal
l
edgl
obalv
ari
abl
es.

 I
nthedef
ini
ti
onoff
unct
ionpar
amet
erswhi
char
ecal
l
edf
ormalpar
amet
ers.

LocalVar
iabl
es

 Var
iabl
est
hatar
edecl
aredi
nsi
deaf
unct
ionorbl
ockar
ecal
l
edl
ocal
v
ari
abl
es.Theycan beused onl
ybyst
atement
sthatar
einsi
det
hat
f
unct
ionorbl
ockofcode.

 Local
var
iabl
esar
enotknownt
ofunct
ionsout
sidet
hei
rown.

Thef
oll
owi
ngexampl
eshowshowl
ocal
var
iabl
esar
eused.Her
eal
lthev
ari
abl
es
a,
b,andcar
elocal
tomai
n()f
unct
ion.

#i
ncl
ude<st
dio.
h>

i
ntmai
n(){

/
*localvar
iabl
edecl
arat
ion*
/
i
nta,b;
i
ntc;

/*act
ual
ini
ti
ali
zat
ion*
/
a=10;
b=20;
c=a+b;

pr
int
f("
val
ueofa=%d,
b=%dandc=%d\
n",
a,b,
c);

r
etur
n0;
}

Gl
obal
Var
iabl
es
 Gl
obal
var
iabl
esar
edef
inedout
sideaf
unct
ion,
usual
l
yont
opoft
hepr
ogr
am.
 Gl
obalv
ari
abl
eshol
dthei
rval
uest
hroughoutt
hel
i
fet
imeofy
ourpr
ogr
am and
t
heycanbeaccessedi
nsi
deanyoft
hef
unct
ionsdef
inedf
ort
hepr
ogr
am.

 Agl
obal
var
iabl
ecanbeaccessedbyanyf
unct
ion.

 Thati
s,agl
obalv
ari
abl
eisav
ail
abl
eforuset
hroughouty
ourent
ir
epr
ogr
am
af
teri
tsdecl
arat
ion.

Thef
oll
owi
ngpr
ogr
am showhowgl
obal
var
iabl
esar
eusedi
napr
ogr
am.

Li
veDemo
#i
ncl
ude<st
dio.
h>

/
*global
var
iabl
edecl
arat
ion*
/
i
ntg;

i
ntmai
n(){

/
*localvar
iabl
edecl
arat
ion*
/
i
nta,b;

/*act
ual
ini
ti
ali
zat
ion*
/
a=10;
b=20;
g=a+b;

pri
ntf("
val
ueofa=%d,
b=%dandg=%d\
n",
a,b,
g);
return0;
}

STORAGECLASSES:
-

Astorageclassdefi
nesthescope(v
isi
bil
i
ty)andli
fe-t
imeofvar
iabl
esand/
orfuncti
ons
wi
thinaCPr ogram.Theyprecedethetypethattheymodif
y.Wehav efourdif
fer
ent
st
orageclassesinaCprogram −

 aut
o

 r
egi
ster

 st
ati
c

 ext
ern

Theaut
oSt
orageCl
ass
Theaut
ost
oragecl
assi
sthedef
aul
tst
oragecl
assf
oral
ll
ocal
var
iabl
es.
{
i
ntmount;
aut
ointmont
h;
}
Theexampl
eabovedefi
nestwov ari
abl
eswithi
nthesamest
oragecl
ass.'
aut
o'can
onl
ybeusedwi
thi
nfunct
ions,
i.
e.,
localv
ari
abl
es.
Ther
egi
sterSt
orageCl
ass
Theregist
erst or
agecl
assisusedtodefi
nelocalvari
abl
esthatshoul
dbestoredina
regi
steri
nsteadofRAM.Thismeansthatt
hev ar
iabl
ehasamaxi mum si
zeequalt
othe
regi
stersi
ze( usual
l
yoneword)andcan'
thav
et heunary'
&'operat
orappl
iedt
oit(asi
t
doesnothav eamemor yl
ocati
on)
.
{
regi
steri
ntmi
l
es;
}
Ther egist
ershouldonl ybeusedf orv ar
iabl
esthatrequi
requickaccesssuchas
counters.I
tshoul
dal sobenotedthatdefini
ng'r
egi
ster
'doesnotmeant hatt
hev ari
abl
e
wil
lbest or
edinar egist
er.I
tmeanst hatitMIGHTbestoredinaregi
sterdependingon
hardwareandimplement ati
onrest
ri
ctions.

Thest
ati
cSt
orageCl
ass
Thestati
cstorageclassi
nstructst
hecompi lertokeepal ocalvar
iabl
einexi st
ence
duri
ngthelife-t
imeoftheprogram i
nsteadofcr eati
nganddest r
oyingiteacht i
meit
comesi nt
oandgoesoutofscope.Ther ef
or e,makinglocalvari
ablesstati
cal l
ows
them t
omai ntai
nthei
rval
uesbetweenfuncti
oncal l
s.
Thestat
icmodif
iermayal sobeappl
iedtoglobalvar
iabl
es.Whent hi
sisdone,i
t
causest
hatv
ari
able'
sscopetober
est
ri
ctedt
othefil
einwhichi
tisdecl
ared.
I
nCprogr
ammi ng,whenstat
ici
susedonaglobalvari
abl
e,i
tcausesonl
yonecopyof
t
hatmembert
obeshar edbyall
theobj
ect
sofi
tsclass.

Theext
ernSt
orageCl
ass
Theexternst
oragecl
assisusedtogi
vearef
erenceofaglobalvar
iabl
ethatisv i
sibl
e
toALLt heprogram f
il
es.Wheny ouuse'
exter
n',t
hev ar
iabl
ecannotbei nit
ial
ized
however,i
tpointst
hev ar
iabl
enameatastoragelocat
ionthathasbeenpr evi
ously
def
ined.
Wheny ouhavemul ti
plefil
esandy oudefi
neagl obalv
ari
ableorf unct
ion,whi
chwil
l
alsobeusedi not herfi
les,thenexternwil
lbeusedi nanot herfil
et oprovi
dethe
refer
enceofdefinedv ar
iableorf unct
ion.Justforunder
standing,ext
ernisusedto
declar
eaglobal
variabl
eorf unct
ioninanotherfi
l
e.
Modi
fi
ersorDat
aty
pemodi
fi
ersi
nC:
-

I
nclanguageDataTypeModif
ier
sarekeywor
dsusedtochanget
hepropert
iesofcur
rent
pr
opert
iesofdat
atype.Dat
atypemodi
fier
sarecl
assi
fi
edint
ofol
l
owingtypes.

 l
ong
 shor
t
 unsi
gned
 si
gned

Modi
fier
sarepref
ixedwi
thbasicdat
aty
pestomodi
fy(
eit
heri
ncr
easeordecr
ease)t
he
amountofst
oragespaceal
locatedt
oavar
iabl
e.

Forexample,
storagespacef
orintdat
atypei
s4bytefor32bitpr
ocessor
.Wecanincr
ease
therangebyusi
ngl ongi
ntwhi
chis8by t
e.Wecandecreaset
herangebyusi
ngshorti
nt
whichis2byte.

l
ong:

Thiscanbeusedt oincreasedsizeofthecurr
entdatatypet
o2morebyt
es,
whichcanbe
appli
edonintordoubl edatatypes.Forexampl
eintoccupy2by
teofmemoryi
fweuselong
wit
hintegervar
iabl
et henitoccupy4by teofmemory.

Sy
ntax

1. l
onga;-
->bydef
aul
twhi
chr
epr
esentl
ongi
nt.
shor
t

I
ngenerali
ntdat
aty
peoccupi
esdi
ff
erentmemoryspacesf
oradi
ff
erentoper
ati
ngsy
stem;
t
oall
ocatefi
xedmemor
yspaceshor
tkeywordcanbeused.

Sy
ntax

1. shor
tinta;
-->occupi
es2by
tesofmemor
yspacei
nev
eryoper
ati
ngsy
stem.
unsi
gned

Thi
skey
wor
dcanbeusedt
omaket
heaccept
ingv
aluesofadat
aty
pei
sposi
ti
vedat
aty
pe.

Sy
ntax

1. unsi
gnedi
nta=100;
//r
ight
2. unsi
gnedi
nta=-
100;
//wrong
Si
gned

Thiskey
wor daccept
sbothnegati
veorposi
ti
vev
alueandt
hisi
sdef
aul
tpr
oper
ti
esordat
a
ty
pemodi f
iersforev
erydat
atype.

Exampl
e

1. i
nta=10;/ /r
ight
2. i
nta=-10;//r
ight
3. si
gnedinta=10; //ri
ght
4. si
gnedinta=-10; /
/ri
ght
Ty
peQual
i
fier
s
C–ty
pequalif
ier
s: Thekey
wor
dswhi
char
eusedt
omodi
fyt
hepr
oper
ti
esofav
ari
abl
ear
e
cal
l
edtypequal
ifi
ers.

TYPESOFCTYPEQUALI
FIERS:

Ther
ear
etwot
ypesofqual
i
fier
sav
ail
abl
einCl
anguage.Theyar
e,

1. const
2. vol
ati
l
e

1.CONSTKEYWORD:

 Const
ant
sar
eal
sol
i
kenor
mal
var
iabl
es.But
,onl
ydi
ff
erencei
s,t
hei
rval
ues
can’
tbemodi
fi
edbyt
hepr
ogr
am oncet
heyar
edef
ined.
 Theyr
efert
ofi
xedv
alues.Theyar
eal
socal
l
edasl
i
ter
als.
 Theymaybebel
ongi
ngt
oanyoft
hedat
aty
pe.
 Sy
ntax:
constdat
a_t
ypev
ari
abl
e_name;
(or
)constdat
a_t
ype*
var
iabl
e_name;

2.VOLATI
LEKEYWORD:

 Whenav
ari
abl
eisdef
ineasv
olat
il
e,t
hepr
ogr
am maynotchanget
hev
alueof
t
hev
ari
abl
eexpl
i
cit
ly.
 But
,thesev
ari
abl
eval
uesmi
ghtkeeponchangi
ngwi
thoutanyexpl
i
cit
assi
gnmentbyt
hepr
ogr
am.Theset
ypesofqual
i
fier
sar
ecal
l
edv
olat
il
e.
 Forexampl
e,i
fgl
obal
var
iabl
e’
saddr
essi
spassedt
ocl
ockr
out
ineoft
he
oper
ati
ngsy
stem t
ost
oret
hesy
stem t
ime,
thev
aluei
nthi
saddr
esskeepon
changi
ngwi
thoutanyassi
gnmentbyt
hepr
ogr
am.Thesev
ari
abl
esar
enamedas
v
olat
il
evar
iabl
e.
 Sy
ntax:

v
olat
il
edat
a_t
ypev
ari
abl
e_name;
(or
)vol
ati
l
edat
a_t
ype*
var
iabl
e_name;
EXPRESSI
ONS
Anexpressi
oni sacombinati
onofv ari
ablesconst
antsandoperatorswr i
tt
enaccordi
ngt othe
synt
axofCl anguage.I
nCev eryexpr
essioneval
uatest
oav al
uei.e.,ever
yexpr
essionresul
tsin
some value ofa cert
aintype thatcan be assi
gned t
oav ariable.Some examples ofC
expr
essi
onsar eshowninthetabl
egivenbelow.

Al
gebr
aicExpr on CExpr
essi essi
on

axb–c a*b–c

(
m +n)(
x+y
) (
m +n)*(
x+y
)

(
ab/c) a*b/c

3x2+2x+1 3*
x*x+2*
x+1

(
x/y
)+c x/y+c

Expr
essi
onsar
eev
aluat
edusi
nganassi
gnmentst
atementoft
hef
orm

Var
iabl
e=expr
essi
on;

Vari
abl
ei s any val
i
dC v ari
abl
e name.When t he stat
ementis encounter
ed,the
expr
essioni
sev al
uatedfi
rstandthenr
epl
acesthepreviousval
ueofthevari
ableonthe
l
efthandside.Allvari
abl
esusedi nt
heexpressi
onmustbeassi gnedv al
uesbefore
eval
uat
ionisatt
empted.

Exampl
eofev
aluat
ionst
atement
sar
e

x=a*b–c

y=b/c*a

z=a–b/c+d;
Thef
oll
owi
ngpr
ogr
am i
ll
ust
rat
est
heef
fectofpr
esenceofpar
ent
hesi
sinexpr
essi
ons.

mai
n()
{
f
loata,
b,cx,
y,z;
a=9;
b=12;
c=3;
x=a–b/3+c*2–1;
y=a–b/(
3+c)*(
2–1)
;
z=a–(b/(
3+c)*2)–1;
pr
int
f("
x=%f
",
x);
pr
int
f("
y=%f
",
y);
pr
int
f("
z=%f
",
z);

Out
put
x=10.
00
y=7.
00
z=4.
00
Pr
ecedencei
nAr
it
hmet
icOper
ator
s
Anarit
hmeti
cexpr essi
onwit
houtparent
hesi
swil
lbeevaluat
edf r
om l
efttor
ightusi
ng
ther
ulesofprecedenceofoper
ator
s.Therear
etwodi
stinctpri
ori
tyl
evel
sofari
thmeti
c
oper
atorsi
nC.
Highpr
ior
it
y*/%
Lowpri
ori
ty+-

Rul
esf
oreval
uat
ionofexpr
essi
on

 Fi
rstpar
ent
hesi
zedsubex
pressi
onl
eftt
ori
ghtar
eev
aluat
ed.
I
fpar
ent
hesi
s ar
e nest
ed,t
he ev
aluat
ion begi
ns wi
tht
he i
nner
mostsub
expr
essi
on.

 The pr
ecedence r
ulei
s appl
i
ed i
n det
ermi
ning t
he or
derofappl
i
cat
ion of
oper
ator
sin ev
aluat
ingsubexpr
essi
ons.

 Theassoci
abi
l
ityr
ulei
sappl
i
ed whent
wo ormor
eoper
ator
soft
hesame
pr
ecedencel
evel
appeari
nthesubexpr
essi
on.

 Ar
it
hmet
ic expr
essi
ons ar
e ev
aluat
ed f
rom l
eftt
ori
ghtusi
ng t
he r
ules of
pr
ecedence.

 WhenPar
ent
hesi
sar
eused,t
heexpr
essi
onswi
thi
npar
ent
hesi
sassumehi
ghest
pr
ior
it
y.
Oper
atorpr
ecedenceandAssoci
ati
vi
ty

Eachoper
atori
nChasapr
ecedenceassoci
atedwi
thi
t.Thepr
ecedencei
susedt
o
det
ermi
nehowanexpr
essi
oni
nvol
vi
ngmor
ethanoneoper
atori
sev
aluat
ed.Ther
ear
e
di
sti
nctl
evel
sofpr
ecedenceandanoper
atormaybel
ongt
ooneoft
hesel
evel
s.The
oper
ator
sofhi
gherpr
ecedencear
eev
aluat
edf
ir
st.Theoper
ator
sofsamepr
ecedence
ar
eev
aluat
edf
rom r
ightt
olef
torf
rom l
eftt
ori
ghtdependi
ngont
hel
evel
.Thi
sisknown
asassoci
ati
vi
typr
oper
tyofanoper
ator
.

Thet
abl
egi
venbel
owgi
vest
hepr
ecedenceofeachoper
ator
.

Oper
ati
on
Or
der Cat
egor
y Oper
ator Associ
ati
vi
ty

1 Hi
ghestpr
ecedence () Funct
ioncal
l L?R
[] Lef
ttoRi
ght
?
::
.

2 Unar
y ! Logicalnegation(NOT) R?L
~ Bitwise1’scompl ement Ri
ght-
>Lef
t
+ Unaryplus
- Unarymi nus
++ Preorposti ncrement
-- Preorpostdecr ement
& Address
* Indir
ecti
on
Si
zeof Sizeofoper antinbytes

3 MemberAccess .
* Der
efer
ence L?R
?
* Der
efer
ence

4 Mul
ti
pli
cat
ion * Mul
ti
ply L?R
/ Di
vi
de
% Modul
us

5 Addi
ti
ve + Bi
nar
yPl
us L?R
- Bi
nar
yMi
nus

6 Shi
ft << Shi
ftLeft L?R
>> Shi
ftRight

7 Rel
ati
onal < Lessthan L?R
<= Lessthanorequalt
o
> Great
erthan
>= Great
erthanorequalt
o

8 Equal
i
ty == Equal
to L?R
!
= NotEqual
to

9 Bi
twi
seAAND & Bi
twi
seAND L?R

10 Bi
twi
seXOR ^ Bi
twi
seXOR L?R

11 Bi
twi
seOR | Bi
twi
seOR L?R

12 Logi
cal
AND && Logi
cal
AND L?R

14 Condi
ti
onal ?: Ter
nar
yOper
ator R?L

15 Assi
gnment = Assi
gnment R?L
*= Assi
gnpr oduct
%= Assi
gnremi nder
/= Assi
gnquot i
ent
+= Assi
gnsum
-= Assi
gndifference
&= Assi
gnbitwiseAND
^= Assi
gnbitwiseXOR
|
= Assi
gnbitwiseOR
<<= Assi
gnleftshift
>>= Assi
gnrightshift

16 Comma , Ev
aluat
e L?R
Ty
peconver
sionsorTy
pecast
ing i
nexpr
essi
ons

Typecastingisawayt oconvertav ar
iabl
efrom onedataty
pet oanotherdatatype.
Forexample,ifyouwanttostorea'long'val
ueintoasimplei
ntegert
heny oucant y
pe
cast'
long'to'
int'
.Youcanconv er
tthev al
uesfrom onety
petoanotherexpli
cit
lyusi
ng
thecastoperatorasfol
l
ows−
(
typename)expr
essi
on

Ty
pecast
ingi
soft
wot
ypes:
-

1)I
mplici
tTypeConv
ersi
on
2)Expl
ici
tTy
peConversi
on

1)
Impl
ici
tty
peconv
ersi
on
C permitsmixi
ngofconstantsandv ari
ablesofdiff
erenttypesinanexpressi
on.C
automati
cal
ly conv
ert
s any intermediat
ev al
ues t
ot he propertype so t
hatthe
expressi
oncanbeev aluat
edwi thoutloosinganysignifi
cance.Thi
sautomaticty
pe
conversi
oni
sknowasi mpli
citt
ypeconv er
sion

Duri
ngev aluati
onitadher
estoveryst
ri
ctrul
esandt y
peconver
sion.I
ftheoper
andsare
ofdi
fferenttypesthelowert
ypeisaut
omat i
call
yconver
tedt
othehighert
ypebefor
ethe
oper
ationpr oceeds.Ther
esul
tisofhi
ghertype.

Thef
oll
owi
ngr
ulesappl
ydur
ingev
aluat
ingexpr
essi
ons

All
shortandcharareaut
omati
cal
lyconv
ert
edtointt
hen
1.Ifoneoperandislongdoubl
e,t
heotherwi
llbeconver
tedt
olongdoubl
eandr
esul
t
wil
lbelongdouble.

2.Ifoneoper
andi
sdoubl
e,t
heot
herwi
l
lbeconv
ert
edt
odoubl
eandr
esul
twi
l
lbe
doubl
e.

3.I
foneoper
andi
sfl
oat
,theot
herwi
l
lbeconv
ert
edt
ofl
oatandr
esul
twi
l
lbef
loat
.

4.I
foneoftheoper
andisunsignedlongi
nt,t
heot
herwi
l
lbeconv
ert
edi
ntounsi
gned
l
ongintandr
esul
twi
llbeunsi
gnedlongint
.

5.Ifoneoperandislongi
ntandotheri
sunsignedintthen a.I
funsi
gnedintcanbe
conver
tedtolongi
nt,
thenunsi
gnedi
ntoper
andwi l
lbeconver
tedassuchandtheresul
t
wil
lbelongint
.

.
..
..
b.ElseBothoper
andswi
l
lbeconv
ert
edt
ounsi
gnedl
ongi
ntandt
her
esul
twi
l
lbe
unsi
gnedlongi
nt.

6.I
foneoft
heoper
andi
slongi
nt,t
heot
herwi
l
lbeconv
ert
edt
olongi
ntandt
her
esul
t
wil
lbel
ongi
nt.

7.I
foneoper
andi
sunsi
gnedi
ntt
heot
herwi
l
lbeconv
ert
edt
ounsi
gnedi
ntandt
he
r
esul
twi
l
lbeunsi
gnedi
nt.

2)Expl
ici
tConv
ersi
on
Manytimestheremayar i
seasituati
onwher
ewewanttofor
ceaty
peconv ersi
onina
waythati
sdiffer
entfr
om aut
omat i
cconv
ersi
on.
Consi
derforexamplethecal
cul
ati
onofnumberoff
emal
eandmal
estudent
si naclass

f
emale_student
s
Rat
io= -
---
--
--
--
---
--
male_st
udents

Si
ncei
ffemal
e_st
udent
sandmal
e_st
udent
sar
edecl
aredasi
nteger
s,t
hedeci
malpar
t
wi
l
lber
oundedof
fandi
tsr
ati
owi
l
lrepr
esentawr
ongf
igur
e.Thi
spr
obl
em canbe
sol
vedbyconv
ert
ingl
ocal
l
yoneoft
hev
ari
abl
est
othef
loat
ingpoi
ntasshownbel
ow.

Rat
io=(
fl
oat
)femal
e_st
udent
s/mal
e_st
udent
s

Theoper
atorf
loatconv
ert
sthef
emal
e_st
udent
stof
loat
ingpoi
ntf
ort
hepur
poseof
ev
aluat
ionoft
heexpr
essi
on.Thenusi
ngt
her
uleofaut
omat
icconv
ersi
on,t
hedi
vi
sion
i
sper
for
medbyf
loat
ingpoi
ntmode,
thusr
etai
ningt
hef
ract
ionalpar
toft
her
esul
t.The
pr
ocessofsuchal
ocalconv
ersi
oni
sknownasexpl
i
citconv
ersi
onorcast
ingav
alue.
Thegener
alf
ormi
s(t
ype_
name)expr
essi
on.
#i
ncl
ude<st
dio.
h>

mai
n(){

i
ntsum =17,
count=5;
doubl
emean;

mean=( doubl
e)sum /count
;
pr
int
f("
Valueofmean: %f\
n",
mean)
;
}
Whent
heabov
ecodei
scompi
l
edandexecut
ed,
itpr
oducest
hef
oll
owi
ngr
esul
t−
Val
ueofmean:
3.400000
I
tshouldbenotedherethatthecastoper
atorhasprecedenceoverdi
visi
on,sothe
v
alueofsum isfi
rstconver
ted t
ot y
pedoubleand f
inal
lyitgetsdivi
ded bycount
y
iel
dingadoubl
evalue.
Typeconversi
onscanbei mplici
twhi
chi sper
formedbythecompi l
erautomatical
l
y,or
i
tcanbespecifiedexpli
cit
lyt
hrought
heuseoft hecastoper
ator
.Itisconsider
edgood
progr
ammi ng pract
ice t
o use the castoperatorwhenevertype conversi
ons are
necessar
y.

COMMANDLI
NEARGUMENTS

I
tispossi
bletopasssomev aluesfrom thecommandlinetoyourCprogramswhen
t
heyareexecut
ed.Thesev al
uesar ecall
edcommandl i
neargumentsandmanyt i
mes
t
heyareimport
antforyourprogram especial
l
ywhenyouwanttocontr
olyourpr
ogram
f
rom out
sidei
nsteadofhardcodingthosev al
uesi
nsi
dethecode.
The command li
ne arguments are handl
ed using main(
)f unct
ion arguments
wher
eargcref
erstothenumberofar gumentspassed,andargv
[]isapoi nterar
ray
whi
chpoint
stoeachargumentpassedtothepr
ogram.
Fol
lowi
ngi
sasimpl
eexamplewhichchecksi
fther
eisanyar
gumentsuppl
i
edf
rom t
he
commandl
ineandt
akeact
ionaccor
dingl
y–
#i
ncl
ude<st
dio.
h>

i
ntmai
n(i
ntar
gc,
char*
argv
[]){

i
f(ar
gc==1){
pri
ntf(
"Theargumentsuppl
iedis%s\n",ar
gv[
1])
;
}
el
seif(argc>=2){
pri
ntf(
"Toomanyargument ssuppl
ied.
\n")
;
}
el
se{
pri
ntf(
"Oneargumentexpected.
\n"
);
}
}
MODELQUESTI
ONS
UNI
T–I
1.a)Def
ineComput
er?Dr
awbl
ockdi
agr
am ofcomput
erandexpl
aini
tspar
ts.

b)Expl
ainaboutv
ari
ousl
anguagesi
ncomput
ers.

2.a)Expl
aint
heconv
ersi
onpr
ocessf
rom deci
mal
tobi
nar
ywi
thy
ourownexampl
e.

b)Expl
aint
heconv
ersi
onpr
ocessf
rom bi
nar
ytodeci
mal
wit
hyourownexampl
e.

3.a)Def
inedat
aty
pe?Expl
ainabouti
ntegerandr
eal
dat
aty
pes?

b)Wr
it
eapr
ogr
am t
ounder
standt
heconceptofwor
kingwi
thmul
ti
plev
ari
abl
eswi
th
dat
a
t
ypes?

4.a)Def
inev
ari
abl
e?Expl
ainhowt
odecl
areav
ari
abl
einCandi
tsr
ules?
b)Def
ineConst
ant
?Wr
it
easepar
atepr
ogr
am howt
ouse#def
ineandconstkey
wor
d?

5.Def
ineScope?Wr
it
eapr
ogr
am t
oimpl
ementl
ocal
andgl
obal
var
iabl
es?

6.a)Def
inest
oragecl
ass?Expl
ainv
ari
ousst
oragecl
asseswi
thexampl
es.
b)Wr i
teapr
ogram toi
mpl
ementst
ati
candaut
omat
icst
oragecl
assesandshow t
he
di
ff
erencei
nst
orage.

7.Def
ineexpr
essi
on?Expl
aini
ndet
ailaboutoper
atorpr
ecedenceandassoci
ati
vi
tywi
than
example.

8.Wr
it
eaboutv
ari
ousdat
amodi
fi
ersandt
ypequal
i
fier
sinC?

9.Def
inetypeConversi
on?Writ
easeparat
eprogram t
oimpl
ementi
mpl
i
citandexpl
i
cit
ty
peconver
sionandident
if
ytheconv
ersi
onpr
ocess?

10.Wr
it
eaboutcommandl
i
near
gument
swi
thanexampl
epr
ogr
am.
UNI
T-I
I
SYLLABUS:

BitwiseOper ators:ExactSi zeIntegerTypes,Logical


Bi twi
seOper ator s,
ShiftOperat
ors,
TipsandCommonPr ogr
ammi ngEr ror
s,KeyTer ms,Summar y,PracticeSet.
Selection&Maki ngDeci sions:LogicalDataandOper ator
s, TwoWaySel ecti
on,Multi
way
Selecti
on, Mor eSt andardFunctions,Ti
psandCommonPr ogrammi ngEr r
ors,KeyTerms,
Summar y,
PracticeSet .
Repet i
tion:ConceptofLoop, PretestandPost -t
estLoops, Init
iali
zationandUpdat ing,Event
andCount erCont roll
edLoops, LoopsinC, OtherStatement sRel atedt oLooping,Looping
Applications,Progr ammingExampl eTheCal cul
atorPr ogr
am, TipsandCommon
Programmi ngEr rors,KeyTerms, Summar y,Practi
ceSet .

Logi
calBi
twi
seoper
ator
s
Ast
henamesuggests,
thebi
twi
seoper
ator
soper
ateonbi
ts.Theseoper
ati
onsi
ncl
ude:
1.bi
twi
seAND(&)
2.bi
twi
seOR(|)
3.bi
twi
seX-OR(^)
4.bi
twi
seNOT(~)

Bi
twi
seAND(
&)
Whenweuset hebi twiseANDoper ator,thebiti
nt hefi
rstoper
andisANDed
wit
ht he correspondi ng bi tint he second oper and.I fboth bit
s ar
e 1,the
cor
respondi
ngbi tinther esultis1and0ot herwise.Forexample:
InaCprogram, the&oper atorisusedas
fol
lows.inta=4,b=2, c;
c=a&b;
pri
ntf(
“%d” ,
c); //pr
ints0

100 (
binar
yequi
val
entofdeci
mal
4)
&010 (
binar
yequi
val
entofdeci
mal
2)
000

Bi
twi
seOR(
|)
Whenweuset hebitwiseORoperator,
thebiti
nthefi
rstoper
andisORedwit
h
the corresponding bitint he second oper
and.Ifone orbot h bi
ts ar
e 1,t
he
corr
espondi ngbitintheresulti
s1and0ot herwi
se.Forex
ample:
InaCpr ogram, the|operat
orisusedas
foll
ows.inta=4,b=2,
c;
c=a|b;
print
f(“
%d” ,
c); //
pri
nts6

100 (
binar
yequi
val
entofdeci
mal
4)
|010 (
binar
yequi
val
entofdeci
mal
2)
110
Bi
twi
seXOR(
^)
Whenweuset hebi
twiseXORoper at
or,thebitinthefi
rstoperandisXORedwi t
h
the cor
respondi
ng bitint he second operand.I fboth bit
s ar e di
ff
erent
,the
corr
espondingbi
tintheresul
tis1and0ot herwise.Thetr
uthtableoperat
orisshown
bel
ow:

A B A^
B
0 0 0
0 1 1
1 0 1
1 1 0

Forex
ampl e:
InaCpr ogram,the|operat
orisusedas
fol
lows.inta=4,
b=2,
c;
c=a^b;
pri
ntf(
“%d”,c)
; /
/pri
nts6

100 (
binar
yequi
val
entofdeci
mal
4)
|010 (
binar
yequi
val
entofdeci
mal
2)
110

Bi
twi
seNOT(
~)
One’
scompl
ementoperat
or(Bi
twi
seNOT)isusedtoconverteach1to0and0
o1,
t inthegi
venbi
nar
ypatt
ern.I
tisaunar
yoper
atori
.e.i
ttakesonlyoneoper
and.

Forex
ampl e,I
naCpr ogram,the~oper
atori
susedas
fol
lows.inta=4,c;
c=~a;
pri
ntf(
“%d”,c)
; /
/pri
nts-
5

~100 (
binar
yequi
val
entofdeci
mal
4)101
/*CPr ogr
am todemonst
rat
euseofbi
twi
seoper
ator
s*/
#incl
ude<stdi
o.h>
i
ntmai
n()
{
unsi
gnedchara=5,
b=9;
//a=5(
00000101)
,b=9(
00001001)
pr
int
f("
a=%d,
b=%d\
n",
a,b)
;
pr
int
f("
a&b=%d\
n",
a&b)
;//Ther
esul
tis00000001
pr
int
f("
a|b=%d\
n",
a|b)
;//Ther
esul
tis00001101
pr
int
f("
a^b=%d\
n",
a^b)
;//Ther
esul
tis00001100
pr
int
f("
~a=%d\
n",
a=~a)
;//Ther
esul
tis11111010
r
etur
n0;

a=5,
b=9
a&b=1
a|
b=13
a^
b=12
~a=250

Bi
twi
seshi
ftoper
ator
s

Thebitwi
seshiftoperat
orsar
eusedt omove/
shif
tthebitpatt
ernsei
thert
o
thelef
torri
ghtside.Lef
tandrightar
etwoshi
ftoperat
orsprovi
dedby'C'
whicharerepr
esentedasfoll
ows:

Oper
and<<n(
LeftShi
ft
)
Oper
and>>n(
RightShi
ft
)

Her
e,

 anoper andisanint
egerexpressi
ononwhichwehavetoperf
ormthe
shif
toper at
ion.
 '
n'isthet ot
alnumberofbitposit
ionst
hatwehav
etoshif
tinthei
nteger
expression.

Thelef
tshif
toperat
ionwi l
lshif
tthe'n'
numberofbit
stothelef
tsi
de.The
l
eft
mostbitsintheexpressionwill
bepoppedout,andnbit
switht
hev al
ue0
wil
lbefi
ll
edontherightside.
Therightshi
ftoperati
onwil
lshi
ftt
he'
n'numberofbi
tst
otheri
ghtsi
de.The
ri
ghtmost'n'
bitsintheexpr
essi
onwil
lbepoppedout
,andthev
alue0will
be
fi
l
ledont hel
eftside.

Exampl
e:xisanint
egerexpr
essi
onwi
thdat
a1111.Af
terper
for
mingshi
ft
oper
ati
ontheresul
twil
lbe:

x<<2(lef
tshi
ft
)=1111<<2=0011
x>>2(
ri
ghtshi
ft)=1111>>2=1100

/*CPr ogr
am todemonst
rat
euseofbi
twi
seshi
ftoper
ator
s*/
#incl
ude<stdi
o.h>
i
ntmai
n()
{
unsi
gnedcharb=9;b=9(
00001001)
pr
int
f("b=%d\
n",b)
;
pr
int
f("
b<<1=%d\
n",
b<<1)
;
pr
int
f("
b>>1=%d\
n",
b>>1)
;
r
etur
n0;
}
OUTPUT:
-

b<<1=18
b>>1=4

SELECTI
ONANDDECI
SIONMAKI
NG:
-

LOGI
CALDATAANDOPERATORS:
-

LOGI
CALOPERATORSI
NC:
 Theseoperat
orsareusedtoper
for
mlogicaloper
ati
onsonthegi
venexpr
essi
ons.
 Therear
e3l ogicaloper
ator
sinClanguage.Theyare,l
ogi
calAND(&&),l
ogi
calOR(
||
)
andlogi
calNOT(!)
.

Oper
ator
s Exampl
e/Descr
ipt
ion

(
x>5)&&(
y<5)
&&(
logi
cal
AND) I
tret
urnstr
uewhenbot
hcondi
ti
onsar
etr
ue

(
x>=10)
||
(y>=10)

|
|(l
ogi
cal
OR) I
tret
urnst
ruewhenat
-l
eastoneoft
hecondi
ti
oni
str
ue

!
((
x>5)
&&(
y<5)
)

I
trev
ersest
hest
ateoft
heoper
and“
((x>5)&&(
y<5)
)”

!(
logi
cal
NOT) I
f“(
(x>5)&&(
y<5)
)”i
str
ue,
logi
cal
NOToper
atormakesi
tfal
se

EXAMPLEPROGRAM FORLOGI
CALOPERATORSI
NC:
#i
ncl
ude<st
dio.
h>

i
ntmain()
{
i
ntm=40, n=20;
i
nto=20,p=30;
i
f(m>n&&m ! =0)
{
pri
ntf(
"&&Oper ator: Bot
hcondi t
ionsar
etrue\n"
);
}
i
f(o>p||p!=20)
{
pri
ntf(
"|
| Operat
or: Onlyonecondi t
ioni
strue\
n");
}
i
f(!(
m>n&&m ! =0))
{
pri
ntf(
"!Operator:Bot hcondit
ionsaretr
ue\n"
);
}
el
se
{
pri
ntf(
"!Operator:Bot hcondit
ionsaretr
ue."\
"But,
statusisinvertedasfalse\n")
;
}
}

OUTPUT:
&&Oper
ator:
Bot
hcondi
ti
onsar
etr
ue
|
|Operat
or:Onl
yonecondi
ti
onistrue
!Oper
ator:
Bothcondi
ti
onsaret
rue.But
,st
atusi
sinv
ert
edasf
alse

Deci
sionMaki
ng:
-

Decisi
on making st
ructures requi
rethatthe programmerspeci fi
es one ormor e
condit
ions t
o be ev
aluated ort est
ed bythe pr
ogram,along wi t
h a stat
ementor
stat
ementstobeexecutedi ft
heconditi
oni
sdeterminedtobetrue,andopti
onall
y,ot
her
stat
ementstobeexecutedi ft
heconditi
oni
sdeterminedtobefalse.

TWO-
WAYSELECTI
ON

Whati
sthet
wo-
wayst
atementi
nCpr
ogr
am?

Two-wayst
atement
sar
ethebasi
cdeci
sionst
atement
swhi
chwi
l
lresul
tinei
thert
rueor
fal
se.

Thi
scanbei
mpl
ement
edusi
ngi
f…el
sest
atement
.

Ani
f…el
sestatementi
sapai
redst
atementusedt
osel
ect
ivel
yexecut
ecodebasedon
t
woal
ter
nat
ives.

I
f(
val
i
dex
pressi
on){

I
fexpr
essi
oni
str
ueexecut
eifbl
ock

El
se{

I
fexpr
essi
oni
sfal
seexecut
eel
sebl
ock

Anifstatementcanbef
oll
owedbyanopt
ional
elsest
atement
,whi
chexecut
eswhen
theBooleanexpr
essi
oni
sfal
se.

Sy
ntax
Thesy
ntaxofani
f.
..
elsest
atementi
nCpr
ogr
ammi
ngl
anguagei
s−
i
f(bool
ean_
expr
essi
on){
/*stat
ement(
s)wi
llexecut
eift
hebool
eanexpr
essi
oni
strue*/
}else{
/*stat
ement(
s)wi
llexecut
eift
hebool
eanexpr
essi
oni
sfalse*
/
}
I
ft he Boolean expr
essi
on eval
uates t
otr
ue,t
hen t
hei
fbl
ockwi
l
lbe execut
ed,
ot
herwise,
theelseblockwil
lbeexecut
ed.
Cprogrammi
nglanguageassumesanynon-zer
oandnon-
nul
lval
uesast
rue,
andi
fiti
s
ei
therzer
oornul
l,
theniti
sassumedasfalsevalue.
Fl
owDi
agr
am

Exampl
e
#i
ncl
ude<st
dio.
h>

i
ntmai
n(){

/
*local
var
iabl
edef
ini
ti
on*
/
i
nta=100;

/*checkt hebool
eancondi t
ion* /
i
f(a<20){
/*i
fconditi
onistruethenprintthefoll
owing*/
pri
ntf
("aisl
essthan20\ n");
}else{
/*i
fconditi
onisfalsethenprintthefol
lowing*
/
pri
ntf
("aisnotl
esst han20\n");
}

pr
int
f("
val
ueofai
s:%d\
n",
a);

ret
urn0;
}

Whent
heabov
ecodei
scompi
l
edandexecut
ed,
itpr
oducest
hef
oll
owi
ngr
esul
t−
aisnotl
esst
han20;
val
ueofais:
100
Mul
ti
waySel
ect
ion:
-

I
f.
..
elsei
f.
..
elseSt
atement
Anifst
atementcanbef ol
l
owedbyanoptionalelseif
..
.el
sestat
ement
,whi
chi
sver
y
usef
ult
ot estv
ari
ouscondi
ti
onsusi
ngsi
ngl
eif.
..
elseifst
atement.
Whenusi
ngi
f.
..
elsei
f.
.el
sest
atement
s,t
her
ear
efewpoi
ntst
okeepi
nmi
nd−
 Ani
fcanhav
ezer
ooroneel
se'
sandi
tmustcomeaf
teranyel
sei
f'
s.
 Ani
fcanhav
ezer
otomanyel
sei
f'
sandt
heymustcomebef
oret
heel
se.
 Onceanel
sei
fsucceeds,
noneoft
her
emai
ningel
sei
f'
sorel
se'
swi
l
lbet
est
ed.

FLOW CHART:
-
Sy
ntax
Thesy
ntaxofani
f.
..
elsei
f.
..
elsest
atementi
nCpr
ogr
ammi
ngl
anguagei
s

i
f(bool
ean_expression1){
/*Executeswhent hebooleanex
pressi
on1istrue*
/
}elsei
f(boolean_expressi
on2){
/*Executeswhent hebooleanex
pressi
on2istrue*
/
}elsei
f(boolean_expressi
on3){
/*Executeswhent hebooleanex
pressi
on3istrue*
/
}else{
/*executeswhent henoneoftheabovecondi
ti
onistr
ue*
/
}

Exampl
e

#include<st dio.
h>
intmai n(){
/ *localvariabl
edef i
niti
on* /
inta=100;
/*checkt hebool eancondi t
ion*/
if(a==10){
/*i
fcondi ti
oni struethenpr i
ntthef
oll
owing*
/
pri
ntf
("Valueofai s10\ n");
}el seif
(a==20){
/*i
felsei fcondi t
ionist rue*/
pri
ntf
("Valueofai s20\ n");
}el seif
(a==30){
/*i
felsei fcondi t
ionist rue* /
pri
ntf
("Valueofai s30\ n");
}el se{
/*i
fnoneoft hecondi tionsi str
ue*/
pri
ntf
("Noneoft hev aluesi smatchi
ng\n")
;
}
pri
ntf
("Exactv alueofai s:%d\n",a)
;
return0;
}

Whent
heabov
ecodei
scompi
l
edandexecut
ed,
itpr
oducest
hef
oll
owi
ngr
esul
t−
Noneoftheval
uesismat
chi
ng
Exactv
alueofais:
100
Mul
ti
-waySel
ect
ion:swi
tchst
atement

 Amulti
-waysel
ecti
onstat
ementi susedtoexecut
eatmostONEoft
he
choi
cesofasetofstat
ementspresent
ed.
 Synt
axofthemult
i-
wayselectst
atement:

swi
tch(EXPRESSI
ON)
{
caseCONSTANT1:oneormor
est
atement
s;br
eak;

caseCONSTANT2:oneormor
est
atement
s;br
eak;
..
.

[def
aul
t:oneormor
est
atement
s;]
}

FLOW CHART:
-
Howdoest
heswi
tchst
atementwor
k?

Theexpr oni
essi sev
aluat
edonceandcompar
edwi
tht
hev
aluesofeachcasel
abel
.
 I
fther
eisamat
ch,t
hecor
respondi
ngst
atement
saf
tert
hemat
chi
ngl
abelar
e
execut
ed.Forexampl
e,i
fthev
alueoft
heexpr
essi
oni
sequalt
oconst 2,
ant st
atement
s
af
tercaseconst 2:a
ant r
eexecut
edunt
il eaki
br sencount
ered.
 I
fther
eisnomat
ch,
thedef
aul
tst
atement
sar
eexecut
ed.
I
fwedonotusebr
eak,
all
stat
ement
saf
tert
hemat
chi
ngl
abel
areexecut
ed.
Thedef tc
aul l
ausei
nsi
det
heswi
tchst
atementi
sopt
ional
.

Si
mpl
eCal
cul
ator
1. //Program t ocr eateasi mpl ecalculator
2. #incl
ude<st dio.
h>
3.
4. i
ntmai n(){
5. c haroper ator;
6. doubl en1, n2;
7.
8. pr i
ntf("
Ent eranoper ator(+,-,*,/)
:");
9. s canf("
%c" ,&operator )
;
10. pr i
ntf("
Ent ertwooper ands: ");
11. s canf("
%l f%l f
",&n1,&n2) ;
12.
13. s witch(oper ator)
14. {
15. case' +'
:
16. pri
nt f("
%.1lf+%. 1lf=%. 1l
f",
n1, n2,n1+n2);
17. break;
18.
19. case' -
':
20. pri
nt f("
%.1lf-%. 1lf=%. 1lf
",
n1, n2,n1-n2);
21. break;
22.
23. case' *'
:
24. pri
nt f("
%.1lf*%. 1lf=%. 1lf"
,n1,n2, n1*n2)
;
25. break;
26.
27. case' /'
:
28. pri
nt f("
%.1lf/%. 1lf=%. 1lf"
,n1,n2, n1/n2)
;
29. break;
30.
31. //oper atordoesn' tmat chanycaseconst ant+,
-,*
,/
32. default:
33. pri
nt f("
Error!oper atori
snotcor rect")
;
34. }
35.
36. r et
urn0;
37.}
PROGRAMSUSI
NGDECI
SIONMAKI
NG:
-

Exampl
eofnest
edi
f.
.el
se
#i
ncl ude<st dio.h>
i
ntmai n()
{
i
ntv ar1,var2;
printf("I
nputt hev al
ueofv ar1:
");
scanf ("%d", &var1) ;
printf("I
nputt hev al
ueofv ar2:
");
scanf ("%d",&v ar2);
i
f( var1! =v ar2)
{
printf("var1isnotequal tov ar
2\n")
;
//Nest edi felse
if(var 1>v ar2)
{
pri
ntf(
"var1isgr eat
erthanv
ar2\
n")
;
}
else
{
pri
ntf(
"var2isgr eat
erthanv
ar1\
n")
;
}
}
else
{
printf("var1isequal t
ov ar2\n"
);
}
return0;
}
Out
put
:

I
nputtheval
ueofv ar
1:12
I
nputtheval
ueofv ar
2:21
v
ar1isnotequaltovar2
v
ar2isgreat
erthanvar1

Exampl
eofel
se.
.i
fst
atement
#i
nclude<st dio.h>
i
ntmai n()
{
i
ntv ar1,var2;
pri
ntf("I
nputt hev alueofvar
1:"
);
scanf("%d",&v ar1);
pri
ntf("I
nputt hev alueofvar
2:"
);
scanf("%d",&var2) ;
i
f(var1! =var2)
{
printf(
"var1isnotequaltov
ar2\n"
);
}
el
sei f(var1>v ar2)
{
printf(
"var1isgreat
erthanvar
2\n"
);
}
el
sei f(var2>v ar1)
{
pr
int
f("
var
2isgr
eat
ert
hanv
ar1\
n")
;
}
el
se
{
pri
ntf
("v
ar1i
sequal
tov
ar2\
n")
;
}
ret
urn0;
}
Out
put
:

I
nputtheval
ueofvar
1:12
I
nputtheval
ueofvar
2:21
v
ar1isnotequal
tovar2

Asy
oucanseet
hatonl
ythest
atement
sinsi
det
hebodyof“
if
”ar
eexecut
ed.Thi
s
i
sbecausei
nthi
sst
atementassoonasacondi
ti
oni
ssat
isf
ied,t
hest
atement
s
i
nsi
det
hatbl
ockar
eexecut
edandr
estoft
hebl
ocksar
eignor
ed.

I
mpor
tantPoi
nts:
1.el
seandel
se.
.i
far
eopt
ional
stat
ement
s,apr
ogr
am hav
ingonl
y“i
f”st
atement
woul
drunf
ine.
2.el
seandel
se.
.i
fcannotbeusedwi
thoutt
he“
if
”.
3.Ther
ecanbeanynumberofel
se.
.i
fst
atementi
nai
fel
se.
.i
fbl
ock.
4.I
fnoneoft
hecondi
ti
onsar
emett
hent
hest
atement
sinel
sebl
ockget
s
execut
ed.
5.Justl
i
ker
elat
ional
oper
ator
s,wecanal
sousel
ogi
cal
oper
ator
ssuchasAND
(
&&)
,OR(
||
)andNOT(
!)
.
REPETI
TION:
-

CONCEPTOFLOOPS:
-

LOOPCONTROLSTATEMENTS

Loop:Aloopisdef
inedasabl
ockofst
atement
swhi
char
erepeat
edl
yexecut
edf
orcer
tai
n
numberofti
mes.

BODYOFTHELOOP

CONDI
TION1TRUE

Repr
esent
ati
onOfLoops

Ty
pesofLoops
 Pr
etest-alogi
cal
condi
ti scheckedbef
oni oreeachr
epet
it
iont
odet
ermi
nei
fthe
l
oopshouldter
minat
e.
– whi l
eloop
– f orl
oop
 Post
test-alogi
calcondi
ti scheckedaf
oni tereachr
epet
it
ionf
ort
ermi
nat
ion.
– do- whil
eloop

BODYOF

CONDI
TION LOOP

TRUE TRUE

FALSE

BODYOFTHE CONDI
TION

LOOP

Pr
etestl
oop post
testl
oop
Ter
minat
ingLoops:

 Counter
-cont
rol
l
edl
oops-aloopcontr
oll
edbyacount
ervar
iabl
e,gener
all
ywher
e
thenumberofti
mesthel
oopwill
executei
sknownaheadofti
me.

 Event
-contr
oll
edl
oops-loopswher
eter
minat
iondependsonanev
entr
ather
thanexecut
ingaf
ixednumberofti
mes.

COUNTERCONTROLEDLOOPS EVENTCONTROLEDLOOPS
Event
-Cont
rol
l
edLoops

Count
er-
Cont
rol
l
edLoops •Gener
all
ywi
thwhil
e,do-
whil
eloops
•Can’
tinf
ernumberofrepet
iti
onsfrom
•Generall
ywi
thforloops pr
ogram
•Can gener al
l
y i nfer number of
repet
it
ionsf
rom code.

•Exampl
es: •Exampl es:
–read5numbers –r eaduntili
nputends
–pri
nt7items –r eaduntilanumberencounter
ed
–sortni
tems
–sear chthroughdataunt
ili
tem f
ound

The“
for
”loop:

Thef
orl
oopst
atementcompr
isesof3act
ions.

The3act
ionsar
e

“i
nit
iali
zeexpressi
on”
,“TestCondi
ti
onexpr
essi
on”and“
updat
ion
expression””
 Theexpr
essi
onsar
esepar
atedbySemi
-Col
ons(
;)
.
 Eacht
imet
heupdat
edv
aluei
scheckedbyt
hel
oopi
tsel
f.
Incr
ement/Decrementi
sthenumeri
cal
val
ueaddedor
subtr
act
edtothevari
abl
eineachr
oundoft
heloop.
Sy
ntax:

f
or(
ini
ti
ali
zeexpr
essi
on;
testcondi
ti
on;
updat
ion)

St
atement
-1;

St
atement
-2;
}

(
iThei
) nit
ial
i
zat
ionset
sal
oopt
oani
nit
ial
val
ue.Thi
sst
atementi
sexecut
edonl
yonce.

(
ii
)Thetestcondi
ti
onisarel
ational
expr
essi
onthatdet
erminest
henumberof
i
ter
ati
onsdesir
edoritdet
ermineswhentoexi
tfr
om theloop.

 Thef
orl
oopcont
inuest
oexecut
easl
ongascondi
ti
onal
testi
ssat
isf
ied.

 Whenthecondi
ti
onbecomesfalsethecont
roloft
heprogr
am exi
tsf
rom the
bodyoff
orloopandexecut
esnextstat
ementsaft
ert
hebodyoftheloop.

(i
ii
)Theupdat
ion(
incr
ementordecr
ementoper
ati
ons)deci
deshowt
omakechangesi
n
t
heloop.

 Thebodyoft
hel
oopmaycont
ainei
therasi
ngl
est
atementormul
ti
ple
st
atement
s.


forl
oop”canbespeci
fi
edbydi
ff
erentway
sasshown

Sy
ntaxOut
putRemar
ks

(
i)f
or(
;;) I
nfi
nit
etol
oop Noar
gument
s

(
ii
)for(
a=0;
a<=20;
) I
nfi
nit
eloop “
a‟i
snei
theri
ncr
easednordecr
eased.

(i
i
i)f
or(a=0;a<=10;
a++) Displaysvalue“a‟isi
ncreasedfrom 0t
o10print
f(“
%d”,a)fr
om 0t
o
10curl
ybracesarenotnecessarydefaultscopeofforloopisonest
atementaft
erloop.

(
iv)f
or(
a=10;
a>=0;
a--
)Di
spl
aysv
alue“
a‟i
sdecr
easedf
rom 10t
o0.pr
int
f(„
%d”
,a)
;fr
om 10t
o0

Fl
owchar
t:
Pr
intt
hef
ir
stf
ivenumber
sst
art
ingf
rom onet
oget
herwi
tht
hei
rsquar
es.

#i
ncl
ude<st
dio.
h>

mai
n()

i
nti
;

f
or(
i=1;
i<=5;
i++)

pr
int
f(“
\nNumber
:%di
tsSquar
e:%d”
,i,
i*i
);

Out
put:

Number
:1i
tsSquar
e:1

Number
:2i
tsSquar
e:4

Number
:3i
tsSquar
e:9

Number
:4i
tsSquar
e:16

Number
:5i
tsSquar
e:25

Pr
ogr
am t
odi
spl
ayf
rom 1t
o10usi
ngf
orl
oopandi
=i+1.

#i
ncl
ude<st
dio.
h>

mai
n()

i
nti
;

pr
int
f(“
\nTheNumber
sof1t
o10ar
e:”
);

f
or(
i=1;
i<=10;
i=i
+1)

pr
int
f(“
\t%d”
,i)
;

Out
put:

TheNumber
sof1t
o15ar
e:1 2 3 4 5 6 7 8 9
10
Nest
ed“
for
”loop:

 Wecanal
sousel
oopwi
thi
nloops.

 i
.e.oneforst
atementwi
thi
nanotherf
orst
atementi
sal
l
owedi
nC.(
or„
C‟al
l
ows
multi
pleforl
oopsi
nthenestedf
orms).

 I
nnestedf
orl
oopsoneormor
eforst
atement
sar
eincl
udedi
nthebodyoft
he
l
oop.

 ANSICal
l
owsupt
o15l
evel
sofnest
ing.Somecompi
l
ersper
mitev
enmor
e.

 Twol
oopscanbenest
edasf
oll
ows.

Sy
ntax:

f
or(i
nit
ial
i
ze;
testcondi
ti
on;
updat
ion)/
*out
erl
oop*
/

f
or(
ini
ti
ali
ze;
testcondi
ti
on;
updat
ion)/
*innerl
oop*
/

Bodyofl
oop;

Theout
erl
oopcont
rol
sther
owswhi
l
ethei
nnerl
oopcont
rol
sthecol
umns.

f
or(
row=1;
row<=r
owmax;
++r
ow)

f
or(
col
umn=1;
col
umn<=col
max;
++col
umn)

y=r
ow*col
umn;
pri
ntf
(“%4d”
,y)
;

pr
int
f(“
\n”
);

}
Pr
ogr
am t
oper
for
m subt
ract
ionof2l
oopv
ari
abl
esUsenest
edf
orl
oops.

#i
ncl
ude<st
dio.
h>

v
oidmai
n()

i
nta,
b,sub;

f
or(
a=3;
a>=1;
a--)

f
or(
b=1;
b<=2;
b++)

sub=a–b;

pr
int
f(“
\na=%db=%da-
b=%d\
n”,
a,b,
sub)
;

Out
put
:

a=3b=1a-
b=2

a=3b=2a-
b=1

a=2b=1a-
b=1

a=2b=2a-
b=0

a=1b=1a-
b=0

a=1b=2a-
b=-
1
The“whi
le”l
oop:

Sy
ntax:

I
nit
ial
i
zat
ionExpr
essi
on;

whi
l
e(TestCondi
ti
on)

Bodyoft
hel
oop

Updai
onExpr
essi
on

Fl
owchar
tforwhi
le:

 Thewhi
l
eisanent
ry–cont
rol
l
edl
oopst
atement
.

 Thet
estcondit
ioni
sev
aluat
edandi
fthecondi
ti
oni
str
ue,
thent
hebodyoft
he
l
oopisexecut
ed.

 Theexecut
ionprocessi
srepeat
eduntil
thet
estcondi
ti
onbecomesf
alseandt
he
cont
roli
str
ansferr
edoutoftheloop.

 Onexit
,thepr
ogr
am cont
inueswi
tht
hest
atementi
mmedi
atel
yaf
tert
hebodyof
t
heloop.

 Thebodyoft
hel
oopmayhav
eoneormor
est
atement
s.

 Thebr
acesar
eneededonl
yift
hebodycont
ainst
woormor
est
atement
s.

 I
t‟
sagoodpr
act
icet
ousebr
acesev
eni
fthebodyhasonl
yonest
atement
.
Pr
ogr
am t
opr
int1t
o10usi
ng“
whi
le“st
atement
.

#i
ncl
ude<st
dio.
h>

mai
n()

i
nti
;

pr
int
f(“
\nTheNumber
sof1t
o10ar
e:”
);

i
=1;

whi
l
e(i
<=10)

pr
int
f(“
\t%d”
,i)
;

i
++;

Out
put:

TheNumber
sof1t
o15ar
e:1 2 3 4 5 6 7 8 9
10

Pr
ogr
am t
ocal
cul
atef
act
ori
alofagi
vennumberusewhi
lel
oop.

#i
ncl
ude<st
dio.
h>

mai
n()

l
ongi
ntn,
fact=1;

pr
int
f(“
\nEnt
ert
heNumber
:”
);

scanf
(“%l
d”,
&n)
;

whi
l
e(n>=1)

f
act=f
act
*n;

n--;

pr
int
f(“\
nfact
ori
alofgi
vennumberi
s%d”
,fact
);

Out
put:
Ent
ert
heNumber:
5/*l
ogi
c5*4*3*2*1=120*
/Fact
ori
alofgi
vennumberi
s120

The“do-
whi
le“l
oop:
 I
ndo-
whi
l
e,t
hecondi
ti
oni
scheckedatt
heendoft
hel
oop.

 Thedo-whi
l
eloopwi
l
lexecut
eatl
eastonet
imeev
eni
fthecondi
ti
oni
sfal
se
i
nit
ial
l
y.

 Thedo-
whi
l
eloopexecut
esunt
ilt
hecondi
ti
onbecomesf
alse.
Sy
ntax:

I
nit
ial
i
zat
ionExpr
essi
on;

do

Bodyoft
hel
oop

Updat
ionExpr
essi
on;

}whi
l
e(TestCondi
ti
on)
;

Fl
owchar
tfordo-
whi
le:
MODELQUESTI
ONS
UNI
T–I
I

1.
a)Expl
ainv
ari
ousbi
twi
seoper
ator
sav
ail
abl
einC?

b)Wr
it
eapr
ogr
am t
oimpl
ementv
ari
ousbi
twi
seoper
ator
sinC?

2.
a)Expl
ainaboutl
ogi
cal
dat
aandoper
ator
sinC?

b)
Expl
ainaboutbi
twi
seshi
ftoper
ator
swi
thexampl
es?Wr
it
eapr
ogr
am t
oimpl
ement
bi
twi
se

shi
ftoper
ator
s.

3.Expl
ainaboutt
wosel
ect
ionconcepti
nC?Wr
it
eapr
ogr
am t
ofi
ndt
hel
argestoft
hree
number
s?

4.Expl
aint
heconceptofMul
ti–waysel
ect
ioni
nC?Wr
it
eapr
ogr
am t
oimpl
ementt
he
conceptof

nest
edi
f-
elsest
atement
?

5.Expl
ainaboutswi
tchst
atementi
nCwi
thanexampl
epr
ogr
am?

6.Defi
neLoop?Expl
aint
hedi
ff
erencebet
weenpr
etestand postt
estl
oopswi
than
exampl
efor
each?

7.Expl
ainaboutForl
oop?Wr
it
eapr
ogr
am t
oimpl
ementt
hewor
kingoff
orl
oop?

8.Expl
aini
ndet
ailaboutwhi
l
eloop?Wr
it
eapr
ogr
am t
oimpl
ementt
hewor
kingofwhi
l
e
l
oop?

9.Expl
aint
hedi
ff
erencebet
weenwhi
l
eanddo-
whi
l
eloopwi
thanexampl
epr
ogr
am?

10.a)Expl
aint
hedi
ff
erencebet
weencont
inueandbeakkey
wor
dswi
thanexampl
e?

b)Wr
it
eacal
cul
atorpr
ogr
am?
UNI
T-I
II

SYLLABUS:

UNI TI II
Array s:Concept s,Usi ngAr rayi nC,ArrayAppl ication,TwoDi mensionalArrays,
Mul ti
di mensional Arrays, Programmi ngExampl e–Cal culat
eAv er
ages,TipsandCommon
Progr ammi ngErrors,KeyTer ms, Summar y ,
Pract i
ceSet .
Strings:St ri
ngConcept s, CSt ri
ng,Stri
ngI nput/Out putFunct i
ons,Array
sofSt ri
ngs,Str
ing
Mani pul at
ionFunct ionsSt r
ing/Dat aConv ersion, APr ogrammi ngExampl e–Mor seCode,
Ti
psandCommonPr ogr ammi ngErrors,KeyTer ms, Summar y
,Practi
ceSet .
Enumer at
ed, Str
uctur e,andUni on:TheTy peDef init
ion( Typedef)
,Enumer atedTypes,
Struct ure,
Uni ons,Progr ammi ngAppl i
cation,TipsandCommonPr ogrammi ngErr
ors,Key
Terms, Summar y,PracticeSet .

ARRAY:Arr
ayisnothi
ngbutcol
lect
ionofsi
mi l
ardat
ael
ement
spl
acedi
ncont
iguousmemor
y
l
ocat
ionsandaccessedbythei
rsubscr
iptv
alues

.
1.CPr
ogr
ammi
ngAr
ray
sist
heCol
l
ect
ionofEl
ement
s
2.CPr
ogr
ammi
ngAr
ray
siscol
l
ect
ionoft
heEl
ement
soft
hesamedat
aty
pe.
3.Al
lEl
ement
sar
est
oredi
ntheCont
iguousmemor
y
4. Al
lel
ement
sint
hear
rayar
eaccessedusi
ngt
hesubscr
iptv
ari
abl
e(i
ndex
).

Pi
ctor
ial
repr
esent
ati
onofCPr
ogr
ammi
ngAr
ray
s

Theabov
ear
rayisdecl
aredasi
nta[5]
;
a[
0]=4; a[1]=5; a[
2]=33; a[3]=13; a[
4]=1;
I
ntheabov
efi
gur
e4,
5,33,
13,
1ar
eact
ual
dat
ait
ems.0,
1,2,
3,4ar
eindexv
ari
abl
es.

WhatdoesAr
rayDecl
arat
iont
ell
toCompi
l
er?

1. Ty
peoft
heAr
ray
2. Nameoft
heAr
ray
3. NumberofDi
mensi
on
4. NumberofEl
ement
sinEachDi
mensi
on
Ty
pesofAr
ray

1.Si
ngl
eDi
mensi
onal
Arr
ay/OneDi
mensi
onal
Arr
ay
2.TwoDi
mensi
onal
Arr
ay

Si
ngl
e/OneDi
mensi
onal
Arr
ay:

1. Si
ngl
eorOneDi
mensi
onal
arr
ayi
susedt
orepr
esentandst
oredat
ainal
i
near
f
orm.
2.Ar
rayhav
ingonl
yonesubscr
iptv
ari
abl
eiscal
l
edOne-
Dimensi
onal
arr
ay
3.I
tisal
socal
l
edasSi
ngl
eDi
mensi
onal
Arr
ayorLi
nearAr
ray

Si
ngl
eDi
mensi
onal
Arr
ayDecl
arat
ionandi
nit
ial
i
zat
ion:

Sy
ntaxf
ordecl
arat
ion:
<dat
aty
pe><
arr
ayname>[
siz
e];

Exampl
esf
ordecl
arat
ion:
inti
arr
[3]
;charcar
r[
20]
;fl
oatf
arr
[3]
;

Sy
ntaxf
ori
nit
ial
i
zat
ion:
<dat
aty
pe><ar
rayname>[
size]={
val
1,v
al2,
…,v
aln}
;

Exampl
esf
ori
nit
ial
i
zat
ion:
int
i
arr
[3]={
2,3,
4};

charcar
r[
20]=“
progr
am”
;
f
loatf
arr
[3]={
12.
5,13.
5,14.
5};

1.Exampl
ePr
ogr
am
#i
ncl
ude<st
dio.
h>

v
oidmai
n()

i
ntnum[
]={
2,8,
7,
6,
0};
i
nti
;

f
or(
i=0;
i
<5;
i
++)

pr
int
fſ
”\nAr
rayEl
ementnum [
%d]=%d”
,i
,num[
i]
);

}
Out
put
:

Ar
ray El
ement num[
0] = 2
Ar
ray El
ement num[
1] = 8
Ar
ray El
ement num[
2] = 7
Ar
ray El
ement num[
3] = 6
Ar
rayEl
ementnum[
4]=0

St
ori
ngel
ement
sint
oanar
rayusi
ngscanf
()

Byusi
ngt
hel
oopconceptwecanst
oret
heel
ement
s(v
alues)i
ntoanar
rayas:

Ex:
-intx[
5],
i
;

f
or(
i=0;
i
<5;
i
++)

scanf
(“%d”
,&x[
i]
);

Accessi
ngt
heel
ement
sfr
om anar
ray

Byusi
ngt
hel
oopconceptwecanaccesst
heel
ement
s(v
alues)f
rom anar
rayas:

Ex:
-intx[
5],
i
;

f
or(
i=0;
i
<5;
i
++)

pr
int
f(“
%d\
n”,
x[
i]
);

Exampl
ePr
ogr
am1:
Accessi
ngar
ray

#i
ncl
ude<st
dio.
h>

#i
ncl
ude<coni
o.h>
v
oidmai
n()

{
i
ntar
r[
]={
51,
32,
43,
24,
5,
26}
;

i
nti
;

f
or(
i=0;
i<=5;
i++){

pr
int
f("
\nEl
ementatar
r[
%d]i
s%d"
,i
,
arr
[i
])
;

get
ch(
);

Out
put
:

El
ementatar
r[
0]i
s51El
ement
atar
r[
1]i
s32El
ementatar
r[
2]
i
s43El
ementatar
r[
3]i
s24
El
ementatar
r[
4]i
s5El
ement
atar
r[
5]i
s26

Oper
ati
onswi
thOneDi
mensi
onal
Arr
ay

1. Del
eti
on–I
nvol
vesdel
eti
ngspeci
fi
edel
ement
sfor
m anar
ray
.
2. I
nser
ti
on–Usedt
oinser
tanel
ementataspeci
fi
edposi
ti
oni
nanar
ray
.
3. Sear
chi
ng– Anar
rayel
ementcanbesear
ched
4. Mer
ging–Theel
ement
soft
woar
ray
sar
emer
gedi
ntoasi
ngl
eone.
5. Sor
ti
ng–Ar
rangi
ngel
ement
sinaspeci
fi
cor
derei
theri
nascendi
ngori
n
descendi
ngor
der
.

CPr
ogr
am t
osear
chanel
ementi
nanar
ray
(Li
nearSear
ch)

#i
ncl
ude<st
dio.
h>

v
oidmai
n()

i
nta[
30]
,el
e,num,
i;

pr
int
f("
\nEnt
ernoofel
ement
s:"
);scanf
("%d"
,&num)
;

pr
int
f("
\nEnt
ert
hev
alues:
")
;

f
or(
i=0;
i<num;
i++)

scanf
("%d"
,&a[
i]
);
pr
int
f("
\nEnt
ert
heel
ement
stobesear
ched:
")
;

scanf
("%d"
,&el
e);

i
=0;

whi
l
e(i
<num &&el
e!=a[
i
])

{ i
++; }

i
f(i
<num)

pr
int
f("
Numberf
oundatt
hel
ocat
ion=%d"
,i+1)
;

el
se

{ pr
int
f("
Numbernotf
ound"
);

}
Out
put
:Ent
ernoofel
ement
s:51122334455

Ent
ert
heel
ement
stobesear
ched:
44

Numberf
oundatt
hel
ocat
ion=4

TwoDi
mensi
onal
Arr
ay:

1. Ar
rayhav
ingt
wosubscr
iptv
ari
abl
eiscal
l
edTwo-
Dimensi
onal
arr
ay.
2. TwoDi
mensi
onal
Arr
ayi
sal
socal
l
edasMat
ri
x.

Decl
arat
ion:
Synt
ax:<dat
aty
pe><ar
rayname>[
rowsi
ze]
[col
umnsi
ze]
;

Exampl
e:charname[
50]
[20]
;
I
nit
ial
i
zat
ion:

i
nta[
3][
3]={1,
2,3
5,
6,7

8,
9,0}
;

I
ntheabov
eex
ampl
ewear
edecl
ari
ng2Dar
raywhi
chhas2di
mensi
ons.Fi
rst
di
mensi
onwi
l
lref
ert
her
owand2nddi
mensi
onwi
l
lref
ert
hecol
umn.

TwoDi
mensi
onal
Arr
ays:

1.TwoDi
mensi
onal
Arr
ayr
equi
resTwoSubscr
iptVar
iabl
es
2. TwoDi
mensi
onal
Arr
ayst
orest
hev
aluesi
nthef
orm ofmat
ri
x.
3. OneSubscr
iptVar
iabl
edenot
est
he“
Row”ofamat
ri
x.
4. Anot
herSubscr
iptVar
iabl
edenot
est
he“
Col
umn”ofamat
ri
x.

Decl
arat
ionanduseof2DAr
ray
s:

i
nta[
3][
4];

f
or(
i=0;
i
<row,
i
++)f
or(
j=0;
j
<col
;
j++)

pr
int
f("
%d"
,a[
i]
[j
])
;

}
I
nit
ial
i
zing2DAr
ray

Met
hod1:I
nit
ial
i
zingal
lEl
ement
srowwi
se

Fori
nit
ial
i
zing2DAr
rayweneedt
oassi
gnv
aluest
oeachel
ementofanar
rayusi
ng
t
hebel
owsy
ntax
.
i
nta[
3][
2]={{
1,4}
,{5,
2},
{6,
5}}
;
Exampl
ePr
ogr
am

#i
ncl
ude<st
dio.
h>v
oid
mai
n()

i
nti
,j
;

i
nta[
3][
2]={{1,
4},
{5,
2},
{6,
5}}
;

f
or(
i=0;
i<3;
i++)

f
or(
j=0;
j<2;
j++)

pr
int
f("
%d"
,a[
i]
[j
])
;

pr
int
f("
\n"
);

Met
hod2:Combi
neandI
nit
ial
i
zing2DAr
ray

I
nit
ial
i
zeal
lAr
rayel
ement
sbuti
nit
ial
i
zat
ioni
smuchst
rai
ghtf
orwar
d.Al
lval
uesar
e
assi
gnedsequent
ial
l
yandr
ow-
wise
i
nta[
3][
2]={
1,4,
5,2,
6,5}
;
Exampl
ePr
ogr
am:

#i
ncl
ude<st
dio.
h>i
nt
v
oidmai
n()

i
nti
,j
;

i
nta[
3][
2]={1,
4,5,
2,6,
5};

f
or(
i=0;
i<3;
i++)

f
or(
j=0;
j<2;
j++)

pr
int
f("
%d"
,a[
i]
[j
])
;

pr
int
f("
\n"
);

Out
put
:14

52

65

Met
hod3:SomeEl
ement
scoul
dbei
nit
ial
i
zed

i
nta[
3][
2]={{1}
,{5,
2},
{6}}
;

#i
ncl
ude<st
dio.
h>
v
oidmai
n()

{i
nti
,j
;

i
nta[
3][
2]={{1}
,{5,
2},
{6}
};

f
or(
i=0;
i<3;
i++)

f
or(
j=0;
j<2;
j++)

pr
int
f("
%d"
,a[
i]
[j
])
;

pr
int
f("
\n"
);}

Out
put
:10

6 2
60

St
ori
ngel
ement
sint
oanar
ray

Byusi
ngt
hel
oopconceptwecanst
oret
heel
ement
s(v
alues)i
ntoanar
rayas:

Ex:
-intx[
2][
3],
i;

f
or(
i=0;
i
<2;
i
++)

f
or(
j=0;
j
<3;
j
++)

scanf
(“%d”
,&x[
i]
[j
])
;

Accessi
ngt
heel
ement
sfr
om anar
ray

Byusi
ngt
hel
oopconceptwecanaccesst
heel
ement
s(v
alues)f
rom anar
rayas:

Ex:
-intx[
2][
3],
i;

f
or(
i=0;
i
<2;
i
++)

f
or(
j=0;
j
<3;
j
++)

pr
int
f(“
%d”
,x[
i]
[j
])
;

Accessi
ng2DAr
rayEl
ement
s:

1.Toaccessev
ery2Dar
raywer
equi
res2Subscr
iptv
ari
abl
es.
2.i
–Ref
erst
heRownumber
3.j
–Ref
ersCol
umnNumber
4.a[
1][
0]r
efer
sel
ementbel
ongi
ngt
ofi
rstr
owandzer
othcol
umn
Howi
tWor
ks?

1.ForEv
eryv
alueofr
owSubscr
ipt,
thecol
umnSubscr
ipti
ncr
ement
edf
rom 0t
on-
1
col
umns
2. i
.
e.ForZer
othr
owi
twi
l
lacceptz
erot
h,f
ir
st,
secondcol
umn(
a[0]
[0]
,a[
0][
1],
a[
0][
2])el
ement
s
3.I
nNextI
ter
ati
onRownumberwi
l
lbei
ncr
ement
edby1andt
hecol
umnnumberagai
n
i
nit
ial
i
zedt
o0.
t
h t
h
4.Accessi
ng2-
DAr
ray
:a[
i]
[j
]El
ementFr
om i Rowandj Col
umn

Mul
ti
pli
cat
ioni
spossi
blei
fandonl
yif

i
. No.ofCol
umnsofMat
ri
x1=NoofCol
umnsofMat
ri
x2
. Re
i
i sul
tantMat
ri
xwi
l
lbeofDi
mensi
on–c[
No.ofRowsofMat
1][
No.ofCol
umns
ofMat
2]

APPLI
CATI
ONSONARRAYS:
-

1.ADDI
TIONOFTWOMATRI
CES.

2.MULTI
PLI
CATI
ONOFTWOMATRI
CES.

3.TRANSPOSEOFAGI
VENMATRI
X.

4.SEARCHI
NGANELEMENTI
NAMATRI
X.
STRI
NGS

Astri
ngi sasequenceofcharacterencl
osedwithi
ndoublequotes(
“ ” )but
endswi t
h
\0.Thecompilerput
s\ 0attheendofstri
ngtospeci
fyt
heendofthe
str
ing.
Togetav al
ueofst
ringvar
iablewecanuset hetwodif
fer
entty
pesoffor
mat s.
Usi
ngscanf
()f
unct
ionas: scanf
(“%s”
,st
ri
ngv
ari
abl
e);

Usi
ngget
s()f
unct
ionas: get
s(st
ri
ngv
ari
abl
e);

STRI
NGHANDLI
NG FUNCTI
ONS

Cli
brar
ysupport
salar
genumberofst
ri
nghandl
ingf
unct
ions.Thosef
unct
ionsar
e
st
oredundert
heheaderf
il
est
ri
ng.hi
nthepr
ogram.

Letusseeaboutsomeoft
hest
ri
nghandl
i
ngf
unct
ions.
(
i)st
rl
en(
)funct
ion
st
rl
en()i
susedtoretur
nthel
engthoft
hest
ri
ng,
thatmeanscount
sthe
numberofchar
acter
spresenti
nastri
ng.
Sy
ntax

i
ntegerv
ari
abl
e=st
rl
en(
str
ingv
ari
abl
e);
Exampl
e:

#i
ncl
ude<st
dio.
h>

#i
ncl
ude<coni
o.h>
v
oidmai
n()

{
charst
r[
20]
;i
nt
st
rl
engt
h;
cl
rscr
();

pr
int
f(”
Ent
erSt
ri
ng:
”)
;get
s(st
r);
st
rl
engt
h=st
rl
en(
str
);

pr
int
f(”
Giv
enSt
ri
ngLengt
hIs:
%d”
,st
rl
engt
h);

get
ch(
);

Out
put
:
Ent
erSt
ri
ng
Wel
come

Gi
venSt
ri
ngLengt
hIs:
7

(
ii
)st
rcat
()f
unct
ion
Thest
rcat
()i
susedt
oconcat
enat
etwost
ri
ngs.Thesecondst
ri
ngwi
l
lbe
appendedt
otheendoft
hef
ir
stst
ri
ng.Thi
spr
ocessi
scal
l
edconcat
enat
ion.

Sy
ntax

st
rcat(
Str
ingVar
iabl
e1,
Str
ingVar
iabl
e2)
;
Exampl
e:

#i
ncl
ude<st
dio.
h>

#i
ncl
ude<coni
o.h>
v
oidmai
n()

{
charst
r1[
20]
,
str
2[20]
;cl
rscr
();

pr
int
f(”
Ent
erFi
rstSt
ri
ng:
”)
;
scanf
(”%s”
,st
r1)
;

pr
int
f(”
Ent
erSecondSt
ri
ng:
”)
;
scanf
(”%s”
,st
r2)
;

pr
int
f(”
Concat
enat
ionSt
ri
ngi
s:
%s”
,st
rcat
ſst
r1,
str
2);
get
ch(
);

Out
put
:

Ent
erFi
rstSt
ri
ng
Good

Ent
erSecondSt
ri
ngMor
ning

Concat
enat
ionSt
ri
ngi
s:GoodMor
ning

(
ii
i)st
rcmp(
)funct
ion
st
rcmp(
)funct
ioni
susedt
ocompar
etwost
ri
ngs.st
rcmp(
)funct
iondoesa
casesensi
ti
vecompar
ison bet
ween t
wo st
ri
ngs.Thet
wo st
ri
ngsar
ecompar
ed
char
act
erbychar
act
erunt
ilt
her
eisami
smat
chorendofoneoft
hest
ri
ngsi
s
r
eached(
whi
chev
eroccur
sfi
rst
).I
fthet
wost
ri
ngsar
eident
ical
,st
rcmp()r
etur
nsa
v
aluez
ero.I
fthey
‟renot
,itr
etur
nst
henumer
icdi
ff
erencebet
weent
heASCI
Ival
uesof
t
hef
ir
stnon-
mat
chi
ngpai
rsofchar
act
ers.

Sy
ntax

st
rcmp(
Str
ingVar
iabl
e1,
Str
ingVar
iabl
e2)
;
Exampl
e:

#i
ncl
ude<st
dio.
h>

#i
ncl
ude<coni
o.h>
v
oidmai
n()

charst
r1[
20]
,st
r2[
20]
;i
ntr
es;

cl
rscr
();

pr
int
f(”
Ent
erFi
rstSt
ri
ng:
”)
;
scanf
ſ”
%s”
,st
r1)
;

pr
int
f(”
Ent
erSecondSt
ri
ng:
”)
;
scanf
ſ”
%s”
,st
r2)
;

r
es=st
rcmp(
str
1,st
r2)
;

pr
int
f(”Compar
eSt
ri
ng Resul
tis:
%d”
,r
es)
;

get
ch(
);

Out
put
:

Ent
erFi
rstSt
ri
ngGood

Ent
erSecondSt
ri
ngGood

Compar
eSt
ri
ngResul
tis:
0

(
iv)st
rcmpi
()f
unct
ion:
-

str
cmpi()f
unct
ioni
susedt
ocompar
etwost
ri
ngs.st
rcmpi
()f
unct
ioni
snotcase
sensi
ti
ve.
Sy
ntax

st
rcmpi(
Str
ingVari
abl
e1,
St
ri
ngVari
able2)
;
Exampl
e:

#i
ncl
ude<st
dio.
h>

#i
ncl
ude<coni
o.h>v
oid
mai
n()

{
charst
r1[
20]
,st
r2[
20]
;i
ntr
es;

pr
int
f(”
Ent
erFi
rstSt
ri
ng:
”)
;
scanf
ſ”
%s”
,st
r1)
;

pr
int
f(”
Ent
erSecondSt
ri
ng:
”)
;
scanf
(”%s”
,st
r2)
;

r
es=st
rcmpi
(st
r1,
str
2);

pr
int
f(”
Compar
eSt
ri
ng Resul
tis:
%d”
,r
es)
;

get
ch(
);

Out
put
:

Ent
erFi
rstSt
ri
ng
WELCOME

Ent
erSecondSt
ri
ng
wel
come

Compar
eSt
ri
ngResul
tis:
0

(
v) st
rcpy
()f
unct
ion:

st
rcpy
()f
unct
ioni
susedt
ocopyonest
ri
ngt
oanot
her
.st
rcpy
()f
unct
ioncopyt
he
cont
ent
sofsecondst
ri
ngt
ofi
rstst
ri
ng.

Sy
ntax

st
rcpy
(St
ri
ngVar
iabl
e1,
Str
ingVar
iabl
e2)
;

Exampl
e:

#i
ncl
ude<st
dio.
h>
#i
ncl
ude<coni
o.h>v
oid
mai
n()

{
charst
r1[
20]
,st
r2[
20]
;i
ntr
es;

cl
rscr
();
pr
int
f(”
Ent
erFi
rstSt
ri
ng:
”)
;
scanf
ſ”
%s”
,st
r1)
;

pr
int
f(”
Ent
erSecondSt
ri
ng:
”)
;
scanf
(”%s”
,st
r2)
;

st
rcpy
(st
r1,
str
2);

pr
int
f(”Fi
rstSt
ri
ngi
s:
%s”
,st
r1)
;pr
int
fſ
”SecondSt
ri
ng
i
s:
%s”
,st
r2)
;get
ch(
);

}
Out
put
:

Ent
erFi
rstSt
ri
ngHel
l
o

Ent
erSecondSt
ri
ng
wel
come

Fi
rstSt
ri
ngi
s:wel
comeSecond
St
ri
ngi
s:wel
come

(
vi) st
rrev
()f
unct
ion:
st
rr
ev(
)funct
ioni
susedt
orev
ersechar
act
ersi
nagi
venst
ri
ng.

Sy
ntax st
rrev
(St
ri
ngVar
iabl
e);

Exampl
e:

#i
ncl
ude<st
dio.
h>

v
oidmai
n()

charst
r[
20]
;
pr
int
f(”
Ent
erSt
ri
ng:
”)
;

get
s(st
r);

pr
int
f(”
Rev
erseSt
ri
ng:
%s”
,st
rr
ev(
str
));

Out
put
:

Ent
erSt
ri
ngWELCOME
Rev
erseSt
ri
ng: emocl
ew

(
vii
)st
rupr
()f
unct
ion:
st
rupr
()f
unct
ioni
susedt
oconv
ertal
lchar
act
ersi
nagi
venst
ri
ngf
rom l
ower
caset
oupper
casel
ett
er.

Sy
ntax

st
rupr
(St
ri
ngv
ari
abl
e);

Exampl
e:

#i
ncl
ude<st
dio.
h>

v
oidmai
n()

{
charst
r[
20]
;

pr
int
f(”
Ent
erSt
ri
ng:
”)
;
get
s(st
r);

pr
int
f(“
Upper
caseSt
ri
ng:
%s”
,st
rupr
(st
r))
;

Out
put
:Ent
erSt
ri
ng wel
come

Upper
caseSt
ri
ng: WELCOME
STRUCTURES

Def
ini
ti
on:
-Thecol
l
ect
ionofsi
mil
ar(
or)di
ff
erentdat
a-t
ypei
temsorv
aluesi
scal
l
ed
St
ruct
ure.

St
ruct
ureDecl
arat
ion:
-Speci
fi
est
hest
ruct
uremember
siscal
l
edSt
ruct
ure
declarat i
on.
Syntax: -str
uctst ructureName Example:-
{ st ructSt udentDet ail
s
data-t
y pe member 1; {
data-t
y pe member 2; i
ntr oll
no;
data-t
y pe member 3; charname[ 20];
--
---
--
--
--
-----
---
--
--
---
----
--- floa tpe r
;
--
---
--
--
--
-----
---
--
--
---
----
---
- };
data-t
ype membern;
};
 St ruct
ur eiscr eatedbycr eatedbyusingthekey wordcal l
ed‘ str
uct’.
 St ruct
ur enamei st heuser -
defi
nedanditisal socal ledas‘ structur
et ag'
.
 St ruct
ur edecl arationisst art
swith‘{‘
andclosedwi th‘}’f
ollowedbysemi colon(
;)
.
 Thev ariableswhi char eplacedin-
betweencur ly-br
acesar ecal ledstructures
member s.
Struct ur eDef initi
on: -Cr eati
ngt hestruct
urevariabl eiscalledst ructur
edef i
nition.
Atthet imeofst r
ucturedef i
niti
onmemor yisal
locatedf orstructuremember sbutnotat
thetimeofst ruct uredecl arati
on.
Syntax: - Exampl e:-
structst r
uctur eNamev ar1,v
ar2,..
..
,v
arN;
str uctSt udentDet ail
s x, y;

Accessi
ngst
ruct
ures:
-Accessi
ngst
ruct
uremeansaccessi
ngt
hest
ruct
ure
member s.Thatmeansst or
ingtheval
uesi
ntost
ruct
uremember
sanddispl
ayi
ngthe
val
uesfrom structuremember s.
Thedot(.)operatorisusedforaccessi
ngt
hestr
uctur
emembers.I
tfor
mstheli
nk
betweenstructurevari
ableandit’
smember.
Syntax:
-struct
urev ari
abl
e.member;

Exampl e:-
x.roll
no=1;
str
cpy (x.name,"
har
i"
);
x.
per =78. 23;

Wr it
eaCpr ogram t oexpl
aintheconcept
sofst
ruct
uredecl
arat
ion,
def
ini
ti
onand
accessi ngst ruct uremember s.
#include<st dio.h>
#include<coni o.h>
#include<st ri
ng. h>
structSt udent Det ai
ls
{
introllno;
charname[ 20];
floatper ;
};

v
oidmai
n()
{st r
uctSt udentDetail
sx;
cl
rscr (
);
x.
roll
no=1;
str
cpy (x.
name, "
hari")
;
x.
per =78.23;
pri
ntf("StudentDetail
s:\n"
);
pri
ntf("RollNumber =%dName=%sPer
cent
age=%f
",
x.
rol
l
no,
x.
name,
x.
per
);
getch();
}
Out
put
:-
Rol
lNumber
=1 Name=har
iPer
cent
age=78.
23

St
ruct
ureI
nti
ali
zat
ion:
Valuesareassi
gnedatt heti
meofst r
ucturedef
it
nioniscall
edSt ructureIni
ti
ali
zat
ion.
Syntax:
-str
uctstruct
ureNamev ar
1={l
istofval
ues},
var2={
li
stofv alues},
..
..
,
varn={ l
istofval
ues};
Example:-str
uctStudentDet
ail
sx={1,
”har
i”
,75.
15},
y={2,
”si
va”,
78.25} ;

Wr iteaCpr ogram t oexplai


ntheconcept sStruct
ureIni
ti
ali
zati
on.
#include<st dio.h>
#include<coni o.h>
#include<st ri
ng. h>
structSt udent Details
{
intr oll
no;
charname[ 20];
floatper ;
};
voidmai n()
{st r
uctSt udent Detail
sx={1,
"har
i"
,78.
23};
cl
rscr ()
;
printf("
StudentDet ail
s:\n"
);
printf("
Roll Number =%dName=%sPer centage=%f"
,x.
rol
l
no,x.
name,x.
per
);
get ch()
;
}

Out
put
:-
Rol
lNumber
=1 Name=har
iPer
cent
age=78.
23

Nest
edSt
ruct
ure:-
Nest
edSt
ruct
uremeanst
hest
ruct
urewhi
chcont
ainst
heanot
her
str
ucturevar
iabl
easit’
smember.
Considert
hef ol
lowi
ngexampl
e,
str
uctdate
{
intday;
i
ntmon;
i
nty ear
;
};

str
uctStudent
Det
ail
s
{
introl
l
no;
charname[20]
;
fl
oatper;
st
ructdat
edob;
};
st
ructStudent
Det
ail
s x;

 I
ntheabov eexampl
e,St
udentDetai
l
sstruct
urecontai
nsthedatestr
uctur
e
var
iabl
easit’
smember.I
tiscal
ledNest
edSt r
uctur
es.
 Member soft
henest
edstr
uctur
eisaccessedbyusingthefol
lowingfor
mat.

Ext
ernalst
ruct
urev
ari
abl
e.I
nter
nalst
ruct
urev
ari
abl
e.Member;

Intheabov eexampl eStudentDetai


lsistheExt er
nal st
ruct
ur eanddateist
hei
nter
nal
struct ure.Nowwecanaccesst heinter
nal str
ucturemember sasfoll
ows:
x.dob. day =3;
x.dob. mon=8;
x.dob. year =1998;
Wr iteaCpr ogr am toexpl aint
heconceptofNest edSt r
uctures.
#incl ude<st dio.h>
#incl ude<coni o.h>
#incl ude<st ri
ng. h>
structdat e
{
intday ;
intmon;
i
nty ear;
};
structSt udent Details
{
i
ntr ollno;
charname[ 20];
fl
oatper ;
structdat edob;
};
voi dmai n()
{st r
uctSt udent Detailsx;
clrscr ();
x.rollno=1;
strcpy (x.name, "hari");
x.per =78. 23;
x.dob. day =3;
x.dob. mon=8;
x.dob. year =1998;
printf("StudentDet ails:
\n");
printf("Rol lNumber =%dName=%sPer centage=%f
DOB=%d/ %d/%d" ,
x.
r oll
no,x.name,
x.per,
x.dob.day,
x.dob.mon,x.dob.
year
);
get ch() ;
}
Out
put
:-

StudentDet
ail
s:
RollNumber=1 Name=har
iPer
cent
age=78.
23

ar
ray
sofst
ruct
ures:
-
 Arr
ayofstructuremeansassi gningsi zetot hestruct
urevari
able.
 Consi
derthef oll
owi ngexampl e:
Exampl e:sttuctStudent Det ai
lsx[5];
Syntax:st r
uctSt ructureNamev ar[
size];
 Consi
derthef oll
owi ngexplanat ionforwhati stheneedtoimplementAr r
ayof
str
uct
ures.
Fordisplayingt he5st udentdet ai
lsweneed5st ruct
urev ar
iabl
esas,
stt
uctSt udent Detai
lsx,y ,
z,w,t
;
 Butbyusingthear rayofst ructurewecandi spl
ayt he5studentdetail
susi
ngonl
y
onestr
ucturev ar
iableas,
st
tuctSt
udentDetai
lsx[5];
Wr
it
eaCpr
ogr
am t
oexpl
aint
heconceptofar
rayofst
ruct
ures.
#incl ude<st di
o.h>
#incl ude<coni o.h>
structSt udentDet ai
ls
{
intr ollno;
charname[ 20];
f l
oatper ;
structdat edob;
};
voidmai n()
{inti ;
structSt udent Detail
sx[ 3];
clrscr ();
printf(“Enter3st udentdet ail
s:\
n”);
for(i=0;i<3;i
++)
{scanf (
“%d%s%f ”,&x[i
].
roll
no, x[
i]
.name,&x[
i]
.per
);
}
printf(“3st udentdet ai
ls:\n”);
for(i=0;i<3;i
++)
{printf(“Rollno=%d Name=%s Per centage=%f\n”,
x[
i]
.r
oll
no,
x[
i]
.name,
x[
i]
.per
);
}
get ch( )
;
}
Out put :- Ent er3st udentdet ai
ls:
1 har i 78. 23
2 si va 58. 32
3 sr ee 65. 32
3st udentdet ails:
Rol lno=1 Name=har iPer centage=78.23
Rol lno=2 Name=si va Per centage=58.32
Rol lno=3 Name=sr ee Per cent
age=65.32

Ty
pedef
:-
Typedefini
ti
onal
l
owsust or
enamingtheexi
sti
ngbui
lt
-i
ndata-
typesanduser-
def
ined
data-
types.
Themainpur
poseoft
hetypedefi
sincr
easi
ngthereadabi
li
tyoft
he
pr
ogr
ammi
ngcode.

Sy
ntax:
-ty
pedef dat
a-t
ype newname;

Ex1:
- ty
pedefintci
vi
l;
ci
vil
x,y
,sum;

Ex2:
- st
urctStudent
Det
ail
sci
vi
l;
ci
vi
lx, y
,z;
Wr
it
eaCpr
ogr
am t
oper
for
m sum oft
wonumber
susi
ngt
ypedef
.
#include<stdio.
h>
#include<conio.h>
voidmai n()
{t ypedef intci vi
l;
civilx,
y,sum;
clr
scr ();
x=10;
y=20;
sum=x+y ;
printf(
“sum is:%d”,
sum)
;
get ch();
}
Out
put
:-
sum i
s:30

St
ruct
uresandf
unct
ions
Thef ol l
owi ngar ethethreepossibil
i
tiestot
hest
ruct
uremember
sfr
om cal
l
ingf
unct
ion
tocalledf unction.
1)Passi ngmember sofastructurei
ndivi
dual
ly
2)Passi ngaddr essofast r
ucturevari
ableand
3)Passi ngent irestruct
ureatonce.
considert hef ollowingexample:
structSt udentDet ai
ls
{
introllno;
charname[ 20];
floatper ;
};
structSt udentDet ai
lsx;

1.passingstr
ucturemember sindivi
dually:
-
display(x.
rol
l
no,x.name,x,
per)
;/*cal
li
ngf un*/
voiddisplay(
intr
ollno,
charname[] ,
floatper)/*
cal
l
edf
un*
/
{-
---
--
---
--
--
--
---
--
---
--
--
--
}

2.
passi
ngent
ir
est
ruct
ureatonce:
-

di
spl
ay(
x);
/*cal
l
ingf
unct
ion*
/
v
oiddisplay(
str
uctSt
udent
Det
ail
sx)
/*cal
l
edf
unct
ion*
/
{
--
--
--
--
--
---
-
--
--
--
--
--
--
---
-
-
--
--
--
--
--
--
---
}

3.
passi
ngaddr
essofast
ruct
urev
ari
abl
e:-

di
spl
ay(
&x)
;/
*cal
l
ingf
unct
ion*
/

voiddisplay(st
ructStudent
Detai
l
s* x)
/*call
edfunct
ion*/
{-
---
---
--
---
-
--
--
---
---
---
--
-
}
Wr it
eaCpr ogram forpassi
ngenti
restruct
ureatonce.
#include<stdio.h>
#include<conio.h>
voiddisplay(st
ructStudent
Detai
l
s x);
structStudentDetai
ls
{
introllno;
charname[ 20];
floatper ;
};

voidmai n()_
{
structSt udentDetai
lsx;
cl
r scr(
);
x.
r oll
no=10;
strcpy(x.name,”sai”
);
x.
per =96. 73;
display(x);
get ch(
);
}

voi
ddisplay
(st
ructStudentDetai
l
sx)
{
pri
ntf
(“%d%s%f”,x.
roll
no,x.name,x.
per
);
}

Out
put
:-

10sai
96.
73
UNI
TIV

Poi
nter
s:I
ntroduct
ion,
Poi
nterstopoint
ers,Compat
ibi
li
ty,Lval
ueandRv
alue,
Tipsand
CommonPr ogrammingEr
rors,KeyTerms,Summary,Practi
ceSet
.

Point
erAppli
cat
ions:Arr
ays,
andPoint
ers,Point
erAr
it
hmet i
candArray
s,Memor
y
Al
locati
onFunct
ion,Arr
ayofPoi
nter
s,ProgrammingAppli
cati
on,
TipsandCommon
Progr
ammi ngEr
rors,KeyTer
ms,Summar y,Pr
acti
ceSet.

ProcessorCommands:ProcessorCommands,
TipsandCommonPr
ogr
ammi
ngEr
ror
s,Key
Terms,Summary,
Pract
iceSet.

Poi
nter

Def
ini
ti
on:
-Poi
ntermeanswhi
chi
spoi
nti
ngt
oot
her
.

PointerVari
able:
-Thev
ari
abl
ewhi
chst
orest
headdr
essofanot
herv
ari
abl
eiscal
l
ed
“PointerVar
iabl
e”.

Pointervari
abledeclarat
ion:-
Syntax:-
data-t
ype*var1,*
var2,
..
..
..
..
,
var*
n;
Exampl e:
-int*x,
*y;

Ini
ti
alizati
onofpoi
nterv
ari
abl
e:-
i
nta, b,*x,
*y;
x=&a;
y=&b;
Heret hesymbol‘
&’i
susedfort
hepur
poseofst
ori
ngt
headdr
essi
ntot
hepoi
nter
vari
able.

Poi
nterOper
ator
s:-
(&and*
)

AddressOperator(&)
:-
I
tretur
nstheaddr essofav
ari
abl
e.
Example:
-inta,
* x;
x=&a;

Indir
ection( or
)Di f
fer
encingoperat
or(
*):
-
Itret
urnst hevalueatthataddress.
Exampl e:-inta,b,
*x;
a=10;
x=&a;
b=* x;
so, f
inall
yt hevalueof‘a’
is10aswel l
as‘b’
is10.
Exampl eprogr am:-
Wr iteaCpr ogram tostor
ethev
aluesanddi
spl
ayt
hev
aluesintovar
iabl
eusi
ngpoi
nter
s
concept .
#incl ude<st di
o.h>
#incl ude<coni o.h> Output
voidmai n() a val
ue:10
{ i nta,b,*x; bv al
ue:10
clrscr();
a=10;
x=&a;
b=*x;
printf("av al
ue: %d\n",
a);
printf("bv al
ue: %d",
b);
}

Poi
nter
stopoi
nter
s

Thev ar i
ablewhi chst orestheaddressofpoi
ntervari
ableiscal
l
ed‘
poi
ntertopoi
nter
variabl e’.
Sy ntax: -data-t
ype* *var1,**
var2,
..
..
..
..
,
var**
n;
Exampl e:-int**x,*
*y ;
Consi dert hefoll
owi ngpr ogram t
odispl
aythevari
ablevalueusi
ngpoi
nter
-t
o-poi
nter
concept .
#incl ude<st dio.
h>
#incl ude<coni o.h> Out put
voidmai n() a= 5
{ a= 5
i
nta, *x,**y; a= 5
clr
scr ();
a=5;
x=&a;
y=&x;
printf("a=%d\ n",
a);
printf("a=%d\ n",
*x);
printf("a=%d" ,
**y);
get ch( );
}
Poi
nter
sandf
unct
ionar
gument
s

1.Int hi
st heactual par
amet er
saddressar
epassedtothefor
mal paramet
ers.
2.Themodi fi
cati
onst otheformalpar
ameterswi
ll
automati
cal
lyrefl
ectt
heactual
paramet er.
3.Poi nter
scanr et
ur nthemorethanonevaluef
rom t
hecall
edfuncti
on.
Wr i
teaCpr ogr am forswappi ngoftwonumbersusingpoint
ers.
#incl
ude<stdio.h>
#incl
ude<conio. h>
voidswap( i
nt* x,i
nt*y)
;
voidmain()
{
i
nta,b;
clr
scr()
;
a=10;
b=20;
pri
ntf("Beforeswappinga=%db=%d\
n",
a,b)
;
swap( &a,&b);
getch(); Output:
-
} Befor
eswappi
nga=10b=20
voidswap( int*x,
int*
y) Aft
erswappi
nga=20b=10
{
*x=*x+* y
;
*y=*x-*y;
*x=*x-*y;
pri
ntf("Afterswappi
nga=%db=%d",
*x,
*y);
}

Addr
essar
it
hmet
ic
Thef
oll
owingaretheaddr essari
thmeti
coper
ati
ons.
1)Incr
ement i
ngapoi nter
2)Decrementingapoi nter
3)Addinganintegertopoi nt
erand
4)Subtract
inganintegertopointer
.

Poi
nterv
ari
abl
e Poi
nterv
alue Ar it
hmet i
c Resul
t
oper at
ions
char*
x; 20 x++ 21(
x+1*
sizeof
(char
))
or
x- - 19(
x-1*
sizeof
(char
))
or
x=x+3 23(
x+3*
sizeof
(char
))
or
x=x-3 17(
x-3*
sizeof
(char
))

i
nt*
x; 20 x++ 22(
x+1*
sizeof
(i
nt)
)
or
x- - 18(
x-1*
sizeof
(i
nt)
)
or
x=x+3 26(
x+3*
sizeof
(i
nt)
)
or
x=x-3 14(
x-3*
sizeof
(i
nt)
)

f
loat*
x; 20 x++ 24(
x+1*
sizeof
(fl
oat
))
or
x- - 16(
x-1*
sizeof
(fl
oat
))
or
x=x+3 32(
x+3*
sizeof
(fl
oat
))
or
x=x-3 8(
x-3*
sizeof
(fl
oat
))

doubl
e*x; 30 x++ 38(x+1*
sizeof
(doubl
or e))
x- -
or 22(x-
x=x+3 1*sizeof
(doubl
e))
or
x=x-3 54(
x+3*
sizeof
(doubl
e)
)

6(
x-3*
sizeof
(doubl
e))

Poi
nter
sandAr
ray
s

Thepoi
nter
scanbei
mpl
ement
edi
n1Dand2Dar
ray
sasf
oll
ows:

Ar
ray Wi
thoutpoi
nter
s Wi
thpoi
nter
s

&x[
i] (
x+i
)
1Dar
ray

x[
i] *
(x+i
)

&x[
i]
[j
] *
(x+i
)+j
2Dar
ray

x[
i]
[j
] *
(*(
x+i
)+j
)

Consi
derthefol
lowi
ngexampl
eforst
ori
ngv
aluesi
ntoanar
rayanddi
splayi
ngar
ray
val
ues.
For1Darr
ayis,intA[
5],
i
; and For2Darr
ayi
s,intA[
5][
3],
i,
j
;

Ar
ray Wi
thoutpoi
nter
s Wi
thpoi
nter
s

St
ori
ng f
or(i=0; i
<5; i
++) f
or(i=0; i
<5; i
++)
{ {
scanf (“%d” ,
&A[i
]); scanf (“%d” ,
(A+i)
);
} }
1Dar
ray di
splayi
ng f
or(i=0; i
<5; i
++) f
or(i=0; i
<5; i
++)
{ {
printf(“%d” ,
A[i
])
; printf(“%d” ,
*(A+i
));
} }
Stori
ng f
or(i=0; i
<5; i
++) f
or(i=0; i
<5; i
++)
{ {
for(j=0;j<3;i
++) for(j=0;j<3;i
++)
{ {
scanf (“
%d” ,
&A[i
][
j]
); scanf (“
%d” ,(
*(A+i
)+j
))
;
} }
2Dar
ray } }
di
splayi
ng f
or(i=0; i
<5; i
++) f
or(i=0; i
<5; i
++)
{ {
for(j=0;j<3;i
++) for(j=0;j<3;i
++)
{ {
printf(“%d”,
&A[i]
[j
])
; printf(“%d”,*(
*(A+i
)+j
))
;
} }
} }
Dy
nami
cmemor
ymanagementf
unct
ions

Thememor ywhi chisal locat edatt heti


meofcompi l
ati
oniscalledstati
cmemor y
allocati
on.
Int hi
scaset her eisnochancet or educet hememor ysizeforstori
ngthel esservalues
andnochancet oincreaset hememor ysizet
ost or
ethemor eno.Ofv aluest oactual
size.So, i
nst at i
cmemor yal locat iont hereisachancet ooccurtwopr oblems.
i) Memor ywast ageand
ii
) Memor yshor tage.
Wewi llov er comet heabov et wopr oblemsusi ngDynamicMemor yManagement
Funct i
ons:
1)mal loc()
2)cal loc()
3)r eal loc()and
4)f ree()
1)mal loc() :
-Itallocat est her equest edsizeofbytesandreturnsthepoi ntertothe
fi
r stby te.
Syntax: -int* ptr;
ptr
=( dat a- t
y pe*)mal loc( byt e-size) ;
2)cal loc() :-Itallocat est her equest edblocksofmemor yandr et
urnst hepointert
o
thef irstbl ock.
Syntax: -int* ptr;
ptr
=( dat a- t
y pe*)calloc( n,block- size);
3)r eal loc() :
-Itisusedt or e-all
ocat ethememor ywhichisdy namicallyall
ocatedin
ear lier.
Sy nt ax:-i nt*ptr;
ptr =(dat a- t
ype* )realloc( ptr,new- si
ze);
4)f ree(): -Itisusedt or eleaseabl ockofmemor yas,
free( pt r
) ;

Wr iteraCpr ogram toexpl aintheconceptofDynami cMemor yManagement


funct ions.
#incl ude<st dio.
h>
#incl ude<coni o.h>
#incl ude<mal l
oc.h>
voi dmai n()
{ Output:
int* ptr
,n; enternv al
ue:7
clrscr (
); namei s:Guntur
printf("enternv alue:\n"); enternv al
ue:11
scanf (
"%d" ,
&n) ; namei s:vi
jay
awada
ptr=( char*)mal l
oc(n*sizeof(char))
;
strcpy (ptr,
"guntur")
;
printf("namei s:
%s\ n"
,ptr)
;
printf("enternv alue:\
n" )
;
scanf (
"%d" ,
&n) ;
ptr=( char*)reall
oc(ptr
,n*sizeof(char)
);
strcpy (ptr,
"vij
ayawada" );
printf("namei s:
%s\ n"
,ptr)
;
free(pt r)
;
get ch( )
;
}
Char
act
erpoi
nter
sandf
unct
ions

Theuseofchar act
erpoint
ercanbeexpl
ainedwiththefoll
owingnot at
ions.
Usingar raynot at
ion Usingpoint
ernot at
ions
voidst r
copy (charst
r1[]
,
charstr2[]
) voi
dstrcopy(char*str1,
char*str
2)
{ {
i
nti ; whil
e(*st
r2!=’
\0’)
i
=0; {*st
r1++=*str
2++;
while(str
2[i]
!=’\0’
) }
{st r
1[i]
=str2[i]
; *st
r1=’
\0’;
i++; }
}
str
1[i]
=’\0’;

I
nt heabov eexamplethefuncti
onstrcopy(st
r1,
str
2),thestri
ngfr
om st
r2i
scopi
edint
o
thestri
ngst r
1.
I
npoi nt
erusage, t
hebaseaddr essofstr2andthetargetnameofstri
ngst
r1i
ssuppli
ed
tothestrcopy()funct
ion.
Thelastcellofthestr
ingi
sf i
l
ledwith‘\0’,
toindi
catet heendposi
ti
onofast
ri
ng.

Wr i
teaCpr ogram toexplaintheconceptofchar
act
erpoi
nter
sandf unct
ions.
#include<st di
o.h>
#include<coni o.
h>
voidst r
copy (char*str
2,char*str
1);
voidmai n( )
{ Output
charst r1[10]="
satyam",str
2[10]
; st
ri
ng1data:satyam
clr
scr (
); st
ri
ng2data:satyam
strcopy (str
2,st
r1);
pri
nt f
("stri
ng1dat a:
%s\ n"
,str
1);
pri
nt f
("stri
ng2dat a:
%s" ,
str
2);
getch();
}
voidst rcopy(char*str
2,char*str
1)
{
while(*str1!
='\
0')
{
*str
2++=* st
r1++;
}
*str2='\0';
}
UNI
TV

TextI
nput/Output:Fil
es,
Streams,Standar
dLibraryI
nput/OutputFunct
ions,
For
mat
ti
ng
I
nput/OutputFuncti
ons,Charact
erInput/OutputFunct
ions,
TipsandCommon
Progr
ammingErrors,KeyTerms,Summar y,Pr
acti
ceSet.

Bi
naryInput/Out
put
:Textver
susBi
nar
yStr
eams,StandardLibr
ary
,Funct
ionsf
orFi
les,
Convert
ingFi
leTy
pe,
TipsandCommonProgrammingErrors,
KeyTerms,Summary,
Pract
iceSet.

Functi
ons:Designi
ng,St
ructuredProgr
ams, Funct
ioni
nC, UserDef
inedFunct
ions,I
nter
-
Functi
onCommuni cati
on,StandardFuncti
ons,Passi
ngArraytoFuncti
ons,
PassingPoint
ers
toFuncti
ons,Recursi
on,Passi
nganAr raytoFuncti
on,Ti
psandCommonPr ogramming
Err
ors,KeyTerms,Summar y,Pract
iceSet.

Def
int
ion:
-“Thecol
l
ect
ionofrecor
dsi
scall
edFi
l
e”.
I
nput-
Outputconcept(st
ream)ofaf i
le:-
i) Thei
nputst
ream pr
ov i
dedatatot
heprogram.
ii
) Theout
putstr
eam storest
hedataontothefi
le,suppl
iedbythepr
ogram.
Thefol
lowi
ngdiagr
am expl
ainsthetr
ansf
erofdatabetweendatafi
l
eandinput/
out
put
st
ream.

Oper
ati
onsonFi
les:
-
Thef
ollowi ngar etheoper ati
onswhi char eper f
ormedonf il
es.
1)Nami ngofaf i
le
2)Openi ngandCl osingofaf i
leand
3)Readi ngandWr i
ti
ngofaf il
e.
1)Nami ngofaf i
le:-Thenamei susedf oridenti
fyi
ngthepar
ti
cul
arf
il
e.Thef
il
e
namescanbe,har i
.t
xt,sum.c, stdio.h,etc.Here,

.txt’indicatest hatthef i
l
ei stextfi
le.

.c’ i
ndi cat
est hatt hefil
ei sCpr ogram f il
e

.h’ i
ndi cat
est hatt hefil
ei sheaderf i
le.
2)Openi ng, Closing, ReadingandWr i
tingofaf i
le:
-
Thef unctionfopen()i susedf oropeni ngofaf il
e.
Thef unctionfclose()i susedf orclosingofaf il
e.
Sy ntax: -FILE *f ptr
;
fptr=fopen( “Fi
leName” ,“
Modeofopeni ng”)
;
Ex:-FI LE * fptr;
fptr=f open(“civil
.t
xt”,
”w”);
fclose( fpt
r);
FI
LEI/OFUNCTIONS:-
Fi
l
ei/
ofunct
ionsar
eof2types.Theyare:
1) Un-f
ormatt
edfi
lei
/ofuncti
ons

Char
act
er-
Ori
ent
edf
il
ei/
ofunct
ions-
--
--
-f
put
c()andf
get
c()

St
ri
ng-
Orient
edf
il
ei/
ofunct
ions-
--
--
--
-f
put
s() and f
get
s()
2)For
mat
tedf
il
ei/
ofunct
ions

Mi
xed–Or
ient
edf
il
ei/
ofunct
ions-
--
-f
pri
ntf
() andf
scanf
()

Character-Orientedfilei/of unctions:-
 f putc()i susedt owr iteachar acteront oaf i
le.
fputc(
ch,fptr);
 f getc()i susedt oreadachar acterf rom af i
le.
ch=fgetc(fptr);
Str
ing-Orientedf il
ei/of unctions:-
 Thef unct i
onfput s()isusedf orst oret hestringofdataint
oaf i
l
e.
fputs(
buf f
er,fptr)
;
 Thef unct i
onfget s()isusedt oget tingt hestringofdatafr
om af i
le.
fgets(
buf f
er,size,fptr
);
Mixed–Or ient edfi
lei/of unctions:-
 f pri
ntf() i susedt owr itethedi f
fer entdat atypeelementsontoaf il
e.
fpri
ntf(
fptr,
”cont r
ol st
ring”,argument slist
);
 f scanf()i susedt odi splaythedi ffer entdatat y
peelements(
values)fr
om af
il
e.
fscanf(
fptr,”
cont rolstr
ing”,argument slist
);

Di
ff
erencebet
weenTextf
il
eandBi
nar
yfi
le:
-

 Thefil
ewhichcont
ainst
hetextuali
nfor
mationiscal
ledtextf
il
e.I
t’
sext
ensi
oni
s

.txt

 Thefil
ewhichcont
ainst
heexecutablecodeiscal
ledbinar
yfi
le.I
t’
sext
ensi
oni
s

.exe’
 EOFi ndi
cat
estheEndofafil
e.
Textf
il
e

Sno Command Explanation Sy


ntax
1 Thismodeopensanew
fil
eont hediskf or
w(writ
e) wr i
ti
ng.Ifthef i
leexist, f
ptr
=fopen(
“ci
vi
l.
txt
”,
”w”
);
thenitwill
beov erwritt
en
withoutconf i
rmat i
on.
2 Thismodeopensapr e-
existi
ngfil
ef orreading.
r(
read) Ifthedoesn’texistthen f
ptr
=fopen(
“ci
vi
l.
txt
”,
”r
”);
thecompi l
err et
ur nsa
NULLt othef il
epoi nter.
3 Thismodeopensapr e-
a(append) existi
ngfil
ef or
appendingt hedat aat f
ptr
=fopen(
“ci
vi
l.
txt
”,
”a”
);
theendofaf il
e.
4 w+(wri
te+r
ead) Inthismode, fi
l
ecanbe
wr i
ti
ngandr eadi ng. f
ptr
=fopen(
“ci
vi
l.
txt
”,
”w+”
);
+
5 r (r
ead+writ
e) I nthi
smode, fi
lecanbe
+
readi
ngandwr i
ti
ng. f
ptr
=fopen(
“ci
vi
l.
txt
”,
”r ”)
;
+
6 a( append+rea Inthi
smode, f
il
ecanbe
d) readandrecor
dscanbe fptr
=fopen(
“ci
vi
l.
txt
”,
”a+”);
addedattheendofa
fi
le.

Bi
nar
yInput
/Out
put
:-

Bi
nar
yfi
le

Sno Command Expl


anat
ion Sy
ntax
1 Thismodeopensbinar
y
fi
l
ei nwri
temode.
wb(
wri
te) f
ptr
=fopen(
“ci
vi
l.
txt
”,”
wb”)
;
2 Thismodeopensbi
nar
y
fi
l
ei nr
eadmode.
r
b(r
ead) f
ptr
=fopen(
“ci
vi
l.
txt
”,”
rb”)
;
3 Thismodeopensapr e-
ab( append) exist
ingbinaryfi
lefor
appendingthedat aat f
ptr
=fopen(
“ci
vi
l.
txt
”,”
ab”)
;
theendofaf i
le.
+
4 w b( writ
e+read) Inthismode, binaryf
il
e
canbewr iti
ngand f
ptr
=fopen(
“ci
vi
l.
txt
”,w+”)
” ;
reading.
+
5 r b(read+writ
e) I nthismode, binaryf
il
e
+
canber eadingand f
ptr
=fopen(
“ci
vi
l.
txt
”,”rb”)
;
writi
ng.
+
6 ab( append+rea Inthismode, binaryf
il
e
d) canber eadandr ecor
ds f
ptr
=fopen(
“ci
vi
l.
txt
”,”a+b”
canbeaddedatt heend );
ofaf il
e.

Expl
ainaboutRandom accessf
il
eoper
ati
ons:
-
f
seek() i
i)f
tel
l() i
ii
)rewi
nd()

InRandom accessf i
le,recordsarest
oredregardl
essoftheorderandcanbe
retr
iev
edormodi f
iedr andoml y.I
fareadoperati
onistobeper f
ormedonnth record,
th
thentherei
snoneedt oaccesst hefi
rst(
n-1)recor
ds.Then r ecordcanbe
accesseddir
ectl
y, bywhi chal otoft
imeissaved.
i
) fseek():
-Thi
sfunct i
ongi vesthecurr
entposit
ionofafil
epointerin-
termsofby t
es
from t
hebeginni
ngoft hefil
e.
i
i) ftel
l()
:-Thefunct
ioni susedt omov ethefi
lepoint
ertothedesiredlocat
ionofthe
fi
le.

i
ii
) r
ewi
nd()
:-Thef
unct
ioni
susedt
osett
hef
il
epoi
ntert
othebegi
nni
ngoft
hef
il
e.
1)Wr
it
eaCpr
ogr
am whi
chcopi
esonef
il
etoanot
her
.

#include<st dio. h>


#include<coni o. h>
voidmai n( )
{
FILE* sfptr,
* df ptr;
charch;
clrscr();
sfptr=f open( "msr .txt
",
"r
");
dfptr=f open( " nsr .
txt"
,"
w");
whi l
e( !f
eof (sf pt r
))
{
ch=f get c(sf ptr )
;
fput c(ch,df pt r);
}
fcloseal l(
);
get ch( )
;
}

2)Wr
it
eaCpr
ogr
am t
ocountt
henumberofchar
act
ersandnumberofl
inesi
naf
il
e

#include<st dio.h>
#include<coni o.h>
voidmai n( )
{
FILE* sfpt r
;
charch;
i
ntnoc, now=1, nol
=1;
clrscr();
sfptr=f open( "msr.t
xt"
,"
r"
);
whi le(!feof (
sfpt
r))
{
ch=f get c( sf
ptr)
;
if(ch!=EOF)
noc++;
i
f(ch=='' )
now++;
i
f(ch==' \n')
nol++;
}
printf
( "
No. ofcharacter
s=%dNo.
ofwor
ds=%dNo.
ofl
i
nes=%d"
,now
fclose( sf ptr
);
get ch( )
;
}

Funct
iondef
ini
ti
on
AFunct
ioni
saself
-cont
ainedbl
ockofst
atementthatperf
ormsaspeci
fi
ctaskwhen
cal
l
ed.Af
uncti
onisal
socal
l
edassubpr ogr
am orpr
ocedureorsubrouti
ne.

EveryCProgr
am canbethoughtofasacoll
ecti
onoft
hesefunct
ions.Funct
ionisa
setofinst
ruct
ionstocar
ryoutaparti
culart
ask.Functi
onaft
erit
sexecuti
onr et
urnsasingl
e
val
ue.General
ly,
thef
uncti
onsarecl
assi
fi
edintot
wotypes:

1.Standardfuncti
ons
2.User-def
inedfunct
ions.
TheSt andardf uncti
onsar ealso caled l
l ibrar
yf unct
ions orbui
l
t-i
nfuncti
ons.Allstandard
funct
ions,suchassqr t
(),abs(
),log(
),si
n()etc.areprovi
dedinthel
ibr
aryoffuncti
on.But,most
oftheapplicat
ionsneedot herfuncti
onsthant hoseavail
abl
einthesoft
ware,thoseareknown
asuser-defi
nedf unct
ions.

Adv
ant
agesoff
unct
ions

Sev
eral
adv
ant
agesofdi
vi
dingt
hepr
ogr
am i
ntof
unct
ionsi
ncl
ude:

1.Modulari
ty
2.Reducti
onincoderedundancy
3.Enabl
ingcodereuse
4.Bett
erreadabi
l
ity

Par
tsofaf
unct
ion

Af
unct
ionhast
hef
oll
owi
ngpar
ts:

1.Functi
onprototy
pedeclar
ati
on.
2.Functi
ondefi
niti
on.
3.Functi
oncall
.
4.Actualar
gument sandFor
mal ar
gument
s.
5.Theretur
nstatement.

Funct
ionDecl
arat
ion

Functi
onpr
ototy
pedeclar
ati
onconsistf
unct
ionr
etur
nty
pe,
funct
ionname,
andar
gumentl
i
st.A
funct
ionmustbedecl
aredbefor
eiti
sused

Sy
ntax

r
etur
n_t
ypef
unct
ion_
name(
par
amet
er_
li
st)
;

par
amet
er_
li
st:
typepar
am1,
typepar
am2,
typepar
am3,
etc

Exampl
es

doubl
esqr
t(doubl
e);

i
ntf
unc1(
);

v
oidf
unc2(
charc)
;
Funct
ionDef
ini
ti
onal
soknownasf
unct
ioni
mpl
ement
ati
on,
meanscomposi
ngaf
unct
ion.

Ev
eryFunct
iondef
ini
ti
onconsi
stsoft
woPar
ts:

1.Headerofthefunct
ion,
2.Bodyofthefuncti
on.

Sy
ntax

r
etur
n_t
ypef
unct
ion_
name(
par
amet
er_
li
st)

/
/Funct
ionbody

Thebodyofaf unct
ionconsistsofasetofst
atementsencl
osedwithi
nbraces.The
ret
urnstatementisusedt oretur
nt heresultoft
hecomputati
onsdoneint hecal
ledfuncti
on
and/ortoretur
ntheprogram controlbacktothecal
li
ngfunct
ion.Afunct
ioncanbedef i
nedin
anypartoftheprogr
am textorwit
hinal i
brar
y

Funct
ionheader
Exampl
e:

v
oidwel
come()

pr
int
f(“
hel
l
owor
ld\
n”; f
unct
ionbody

Funct
ionCal
l

Af
unct
ioncal
lhast
hef
oll
owi
ngsy
ntax:

<f
unct
ionname>(
<ar
gumentl
i
st>)
;

Exampl
e:

sum(
a,b)
;
4.
2Ty
pesoff
unct
ions

Funct
ionscanbedi
vi
dedi
nto4cat
egor
ies:

1.Af
unct
ionwi
thnoar
gumentsandnor eturnval
ue
2.Af
unct
ionwi
thnoar
gumentsandar eturnval
ue
3.Af
unct
ionwi
thanar
gumentorargument sandret
urni
ngnov
alue
4.Af
unct
ionwi
thar
gument
sandr et
urningav al
ues

Af
unct
ionwi
thnoar
gument
sandnor
etur
nval
ue

 Call
edfuncti
ondoesnothav eanyarguments
 Notabl
et ogetanyval
uef r
om thecall
ingfuncti
on
 Notret
urninganyval
ue
 Therei
snodat atr
ansferbetweenthecall
i
ngf unct
ionandcal
l
edf
unct
ion.
Exampl
e

#i
ncl
ude<st
dio.
h>

v
oidwel
come(
); /
/funct
ionpr
otot
ypedecl
arat
ion

i
ntmai
n() /
/cal
l
ingf
unct
ion

wel
come(
); /
/funct
ioncal
l

r
etur
n0;

v
oidwel
come(
) /
/cal
l
edf
unct
ion

pr
int
f("
Hist
udent
s..
!"
); f
unct
ionbody

OUTPUT

Hi
student
s..
!

Af
unct
ionwi
thnoar
gument
sandar
etur
nval
ue

 Doesnotgetanyv
aluef
rom t
hecal
li
ngfunct
ion
 Cangi
vear et
urnval
uet
ocall
ingpr
ogram
Exampl
e

#i
ncl
ude<st
dio.
h>

i
ntsend(
);

i
ntmai
n()

i
ntx;

x=send(
)

pr
int
f("
\nYouent
ered:
%d"
,x)
;

r
etur
n0;

i
ntsend(
)

i
ntn;

pr
int
f("
\nEnt
eranumber
:")
;

scanf
("%d"
,&n)
;

r
etur
nn;

OUTPUT

Ent
eranumber
:5

Youent
ered:
5

Af
unct
ionwi
thanar
gument
(s)andr
etur
ningnov
alue

 Af uncti
onhasar gument (
s).
 Acal l
ingf uncti
oncanpassv aluestof unct
ioncall
ed,butcall
ingf
unct
ionnot
receiveanyv al
ue.
 Dat aistransferr
edfrom calli
ngf unct
iont othecall
edfunct
ionbutnodatais
transfer
redfrom thecal
ledfunctiontothecall
ingf
uncti
on.
 Gener al
l
yOut putispri
ntedintheCalledfuncti
on.
Exampl
e

#i
ncl
ude<st
dio.
h>

v
oidadd(
intx,
inty
);

i
ntmai
n()

add(
30,
15)
;

add(
63,
49)
;

add(
952,
321)
;

r
etur
n0;
;

v
oidadd(
inta,
intb)

i
ntr
esul
t;

r
esul
t=a+b;

pr
int
f("
Sum of%dand%di
s%d\
n",
a,
b,
resul
t)
;

OUTPUT

Sum of30and15i
s45

Sum of63and49i
s112

Sum of952and321i
s1273

Af
unct
ionwi
thar
gument
sandr
etur
ningav
alues

 Argumentar epassedbycal li
ngfunct
iontot hecal
ledf
unct
ion
 Calledf uncti
onretur
nv aluetothecal
li
ngf uncti
on
 Datar etur
nedbyt hef uncti
oncanbeusedl at
erinourprogr
am forf
urt
her
calculation
 Most l
yusedi nprogrammi ngbecauseitcant wowaycommunicat
ion
Exampl
e

#i
ncl
ude<st
dio.
h>

i
ntadd(
intx,
inty
);

i
ntmai
n()

i
ntz;
z=add(
952,
321)
;

pr
int
f("
Resul
t%d\
n",
add(
30,
55)
);

pr
int
f("
Resul
t%d\
n",
z);

r
etur
n0;

i
ntadd(
inta,
intb)

i
ntr
esul
t;

r
esul
t=a+b;

r
etur
n(r
esul
t)
;

OUTPUT

Resul
t=85

Resul
t=1273

4.
3Recur
sion

Afunct
iont
hatcallsit
sel
frepet
iti
vel
yisknownasar ecur
siv
ef unct
ion.And,thi
s
techni
queisknownasrecur
sion.Thefunct
ioncall
sit
sel
frepet
it
ivel
yuntilcer
tai
nconditi
onis
sati
sfi
ed.

Ther
ecur
siv
efunct
ionhasf
oll
owi
ngt
ypesofst
atement
s:

1.Astatementorcondit
iontodetermi
nei
fthefunct
ioni
scal
l
ingi
tsel
fagai
n.
2.Afunctioncal
lwhichshouldhavear
gument.
3.Conditi
onalst
atement(i
f-
else)
4.Areturnstat
ement.

Exampl
e:CPr
ogr
am t
hatcal
cul
atesf
act
ori
alofagi
vennumberusi
ngr
ecur
sion

#i
ncl
ude<st
dio.
h>

l
ongi
ntf
act
ori
al(
int
);

i
ntmai
n()

i
ntx;

l
ongi
ntf
;

pr
inf
(“Ent
eranumber
:”
);

scanf
(“%d”
,&x)
;
f
=fact
ori
al(
x);

pr
int
f("
Fact
ori
alof%di
s%l
d\n"
,x,
f)
;

r
etur
n0;

l
ongi
ntf
act
ori
al(
intn)

i
f(
n==0)/
/basecondt
ion

r
etur
n1;

el
se

r
etur
nn*
fact
ori
al(
n-1)
;

OUTPUT

Fact
ori
alof5i
s120
.

I
MPORTANTQUESTI
ONSFROM UNI
T1TOUNI
T5ASPERSYLLABUS

1.
Expl
ainv
ari
ousComput
ingEnv
ironment
s
2.Di
scussaboutdi
ff
erentcomput
erl
anguageswi
thexampl
es
3.
Expl
ainaboutv
ari
abl
esusedi
ncl
angugage
4.
Expl
ainaboutv
ari
ousconst
ant
susedi
ncl
anguage.
5.Expl
ainaboutdi
ff
erentst
oragecl
asseswi
thexampl
es.Di
scusst
hei
rusesandscope.
6.
Expl
ainaboutt
ypequal
i
fier
sincl
anguage
7Expl
ainaboutexpr
essi
onsusedi
ncl
anguage.
8.Wr i
tethei
mpor
tanceofpr
ecedenceandassoci
ati
vi
ty?wr
it
ethet
abl
eforoper
ator
precedence.
9.Whatismeantbyt
ypeconv
ersi
on?Whyi
snecessar
y?Expl
ainabouti
mpl
i
citand
expl
i
cit
t
ypeconv
ersi
onwi
thexampl
es.
10.Expl
ainaboutcommandl
i
near
gument
s
11.Expl
aindi
ff
erentbi
twi
seoper
ator
sav
ail
abl
einCwi
thexampl
es
12.Expl
aindi
ff
erentshi
ftoper
ator
sav
ail
abl
einCwi
thexampl
es

13.Expl
ainr
elat
ional
andl
ogi
cal
oper
ator
swi
thexampl
es
14.Whatar
esel
ect
ionst
atement
s?Whati
sthenecessi
tyofsel
ect
ionst
atement
s?
Expl
ain.
15.Expl
aini
ndet
ail
saboutmul
ti
-waysel
ect
ionst
atement
swi
thexampl
e
16.Whati
smeantbypr
etestandpost
testl
oop?
17.Gi
vet
hedi
ff
erencesbet
weenent
rycont
rol
l
edandexi
tcont
rol
l
edl
oops.
18.Expl
aindi
ff
erentl
oopi
ngst
atement
swi
thsy
ntaxandexampl
es.
19.Whatistheneedofdo-
whi
l
eandwhi
l
eloops?Di
scussaboutt
hei
rusage.
Dist
ingui
shbetweenthem.
20.Wr
it
eaboutnest
edf
orl
oopst
atementwi
thexampl
es.
21.Howdoy
oui
nit
ial
i
zear
ray
sinCpr
ogr
ammi
ng?
22.Expl
ainhowar
ray
sar
epassedasf
unct
ionar
gument
s.Wr
it
eaCpr
ogr
am t
oadd
t
wo2-
dimensi
onal
arr
aysusi
ngt
heconceptofar
ray
sasf
unct
ionar
gument
s.
23.Whatar
etheneedar
ray
sinCpr
ogr
ammi
ng?
24.I
ll
ust
rat
edi
ff
erentway
sofdecl
ari
ngandi
nit
ial
i
zingar
ray
sandst
ri
ngv
ari
abl
es.
25.Expl
ainhowar
ray
sar
epassedasf
unct
ionar
gument
swi
thanexampl
e.
26.Whati
sAr
ray
?Di
scussaboutt
hei
nit
ial
i
zat
ionandaccessi
ngofar
rayel
ement
sin
one
di
mensi
onal
andt
wodi
mensi
onal
arr
ays.
27.Whati
sanar
ray
?Whatar
ethedi
sadv
ant
agesi
nimpl
ement
ingar
ray
sinCl
anguage?
Di
scusspr
obl
emsf
ori
mpl
ement
ingofmul
ti
-di
mensi
onal
arr
aysi
nCl
anguage.
28Whati
sanar
ray
?Howt
oini
ti
ali
ze,
accessi
ng,
andpr
intt
hear
rayel
ement
s?
29.Expl
aindi
ff
erentt
ypesofst
ri
nghandl
i
ngf
unct
ionswi
thexampl
e.
30.Expl
ainf
oll
owi
ngst
ri
nghandl
i
ngf
unct
ionswi
thexampl
e:
(
i)st
rrev
()(
ii
)st
rncmp()(
ii
i)st
rncat
()(
iv)st
rcmpi
(
31.Whati
sst
ri
ng?Expl
ainaboutdecl
arat
ionandi
nit
ial
i
zat
ionofst
ri
ngi
n'C'
.How
st
ri
ngsar
edi
spl
ayedwi
thdi
ff
erentf
ormat
s?Expl
ainwi
thexampl
es.
32Expl
aindi
ff
erentst
ri
ngmani
pul
ati
onf
unct
ionswi
thexampl
e.
33.
Expl
ainaboutt
ypedefi
ncl
anguage.
34.
Expl
ainaboutenumer
ateddat
aty
pesi
ncl
anguage.
35.Expl
aini
ndet
ail
aboutar
rayofst
ruct
ureandpoi
ntert
ost
ruct
urewi
thexampl
e.
36.Expl
ainnest
edst
ruct
ureandsel
fref
erent
ial
str
uct
urewi
thanexampl
e.
37.Whati
stheneedofnest
edst
ruct
ures?Expl
ainwi
thoneexampl
e.
38.Def
ineast
ruct
ure.Descr
ibehowt
odecl
areandi
nit
ial
i
zest
ruct
ureandi
ts
member
swi
thanexampl
e.
39.Expl
ainaboutst
ruct
ureanduni
onwi
thexampl
es.
40.Expl
ainhowt
oaccesst
heel
ement
sofauni
onwi
thanexampl
e.
41.Howt
oini
ti
ali
zeandaccesspoi
nterv
ari
abl
e?Expl
ainpoi
ntert
oaf
unct
ionwi
th
exampl
e.
42.Expl
aint
hear
it
hmet
icoper
ati
onsonpoi
nter
swi
thexampl
e.
43.Whati
sdangl
i
ngpoi
nter
?
44.Expl
aint
hef
oll
owi
ngdy
nami
cmemor
ymanagementf
unct
ionswi
thexampl
e:
(
i)mal
l
oc()(
ii
)cal
l
oc()(
ii
i)r
eal
l
oc()(
iv)f
ree()
45.
Expl
ainaboutLv
alueandRv
aluei
npoi
nter
s.
46.Whatar
ethedi
ff
erentway
sofpassi
ngpar
amet
erst
othef
unct
ion?Expl
ain
47Whati
sty
pecast
ing?Namet
hei
nbui
l
tty
pecast
ingf
unct
ionsav
ail
abl
einC
l
anguage.Whati
sthedi
ff
erencebet
weent
ypecast
ingandt
ypeconv
ersi
on?
49.Howt
oini
ti
ali
zeandaccesspoi
nterv
ari
abl
e?Expl
ainpoi
ntert
oaf
unct
ionwi
th
exampl
e.
50.Di
scussaboutcal
lbyr
efer
enceandcal
lbyv
aluewi
thanexampl
e.
51.
Whati
saf
unct
ion?Whatar
etheusesoff
unct
ions?Di
scusst
her
ulesf
oll
owedt
o
cal
laf
unct
ion.
52.Howt
opassst
ruct
urev
ari
abl
etof
unct
ions?Expl
ainwi
thexampl
e
53.Expl
ainaboutt
heact
ual
argument
sandf
ormal
argumenti
nfunct
ions.Whati
sthe
di
ff
erencebet
weent
hesear
gument
s?Expl
aint
her
ulest
ocal
laf
unct
ioni
namai
n
f
unct
ion.
54.
Expl
ainUserDef
inedFunct
ions,
55Expl
ainPassi
ngAr
rayt
oFunct
ions,
56.
Expl
ainPassi
ngPoi
nter
stoFunct
ions,
57.
Expl
i
anRecur
sion
58.Explai
nthefol
lowi
ngfunct
ionsinf
il
es:
(i
)fseek()(i
i
)ft
ell
()(i
i
i)r
ewind()(i
v)f
open()(
v)f
close()(
vi)f
oef
()
59.Di
sti
ngui
shbet
weenanappendmodeandwr
it
emodei
nfi
l
es.

You might also like