Viva For Cpps (Qna)

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 32

Quest

ionsf
orVi
vaf
orcomput
erpr
ogr
ammi
ngandpr
obl
em sol
vi
ng

1-Whati
sCl
anguage?

Cisami d-
lev
el andprocedural
progr amminglanguage.TheProcedur alpr
ogrammi nglanguage
i
salsoknownast hestr
uctur
edpr ogr amminglanguageisatechniqueinwhichlargeprograms
ar
ebrokendowni nt
osmal l
ermodul es,andeachmodul eusesstructuredcode.Thistechni
que
mini
mizeserrorandmi si
nter
pretati
on.

2-Whyi
sCknownasamot
herl
anguage?

Cisknownasamot herl
anguagebecausemostoft hecompi l
ersandJVMsarewri
tt
eninC
l
anguage.Mostoft
helanguageswhicharedevelopedafterCl
anguagehasborr
owedheavi
l
y
f
rom i
tli
keC++,Pyt
hon,Rust,j
avascr
ipt
,etc.I
tint
roducesnewcoreconcept
sli
kearr
ays,
f
uncti
ons,f
il
ehandl
ingwhichareusedintheselanguages.

3-Whyi
sCcal
l
edami
d-l
evel
progr
ammi
ngl
anguage?

Ciscalledami d-l
evelprogr
amminglanguagebecauseitbi
ndsthel owl
evel
andhigh-l
evel
programmi nglanguage.WecanuseCl anguageasaSy st
em programmingtodevel
opthe
operati
ngsy st
em aswel lasanAppl
i
cationprogr
ammi ngtogeneratemenudri
vencust
omer
dri
venbi l
l
ingsy st
em.

4-Whoi
sthef
ounderofCl
anguage?

Denni
sRi
tchi
e.

5-WhenwasCl
anguagedev
eloped?

Cl
anguagewasdev
elopedi
n1972atbel
ll
abor
ator
iesofAT&T.

6-Whatar
ethef
eat
uresoft
heCl
anguage?

Themai
nfeat
uresofCl
anguagear
egi
venbel
ow:
Si
mple:
Ci sasi
mpl
elanguagebecausei
tfol
l
owst
hest
ruct
uredappr
oach,
i.
e.,
apr
ogr
am i
s
br
okenint
opart
s

Port
able:Cishi
ghl
ypor t
ablemeanst
hatoncet
hepr
ogr
am i
swr
it
tencanber
unonanymachi
ne
wit
hlit
tleornomodif
icati
ons.

MidLev
el:Ci
samid-
lev
elprogramminglanguageasi
tcombi
nest
hel
ow-l
evel
languagewi
th
t
hefeat
uresoft
hehi
gh-
lev
el l
anguage.

St
ruct
ured:
Cisast
ruct
uredl
anguageast
heCpr
ogr
am i
sbr
okeni
ntopar
ts.

FastSpeed:
Clanguagei
sver
yfastasi
tusesapower
ful
setofdat
aty
pesandoper
ator
s.

MemoryManagement :Cprov
idesaninbui
l
tmemor
yfunct
iont
hatsav
est
hememor
yand
i
mprov
estheeffi
ciencyofourprogr
am.

Ext
ensi
ble:
Cisanext
ensi
blel
anguageasi
tcanadoptnewf
eat
uresi
nthef
utur
e.

7-Whati
stheuseofpr
int
f()andscanf
()f
unct
ions?

pri
ntf
():Thepr
int
f()f
unct
ioni
susedt
opr
intt
hei
nteger
,char
act
er,
floatandst
ri
ngv
aluesont
o
thescreen.

Fol
l
owi
ngar
ethef
ormatspeci
fi
er:

%d:
Iti
saf
ormatspeci
fi
erusedt
opr
intani
ntegerv
alue.

%s:
Iti
saf
ormatspeci
fi
erusedt
opr
intast
ri
ng.

%c:
Iti
saf
ormatspeci
fi
erusedt
odi
spl
ayachar
act
erv
alue.

%f
:Iti
saf
ormatspeci
fi
erusedt
odi
spl
ayaf
loat
ingpoi
ntv
alue.

scanf
():
Thescanf
()f
unct
ioni
susedt
otakei
nputf
rom t
heuser
.

8-Whati
sthedi
ff
erencebet
weent
hel
ocal
var
iabl
eandgl
obal
var
iabl
einC?

Fol
l
owi
ngar
ethedi
ff
erencesbet
weenal
ocal
var
iabl
eandgl
obal
var
iabl
e:

Basi
sforcompar
isonLocal
var
iabl
eGl
obal
var
iabl
e
Decl
arat
ion Avari
abl
ewhichisdeclar
edinsi
defunct
ionorbl
ocki
sknownasalocal
var
iabl
e.
Avari
abl
ewhichi
sdecl
aredout
sidefunct
ionorbl
ockisknownasaglobal
var
iabl
e.

ScopeThescopeofav
ariabl
eisavai
l
ablewit
hinaf
unct
ioni
nwhi
cht
heyar
edecl
ared. The
scopeofav
ari
ablei
savai
l
ablethr
oughoutt
heprogr
am.

AccessVar
iabl
escanbeaccessedonl
ybythosestatementsinsi
deafunct
ioni
nwhi
cht
heyar
e
decl
ared. Anystat
ementintheent
ir
eprogram canaccessvari
abl
es.

Li
fe Lifeofav ar
iabl
eiscreat
edwhenthef
uncti
onblocki
sent
eredanddest
roy
edoni
tsexi
t.
Li
feofav
ariabl
eexistsunt
ilt
heprogr
am i
sexecut
ing.

St
orage Var
iabl
esarestor
edi
nast
ackunl
essspeci
fi
ed. Thecompi
l
erdeci
dest
he
st
oragel
ocat
ionofavari
able.

9-Whati
stheuseofast
ati
cvar
iabl
einC?

Fol
l
owi
ngar
etheusesofast
ati
cvar
iabl
e:

Avari
abl
ewhi
chi
sdeclaredasstat
ici
sknownasast
ati
cvar
iabl
e.Thest
ati
cvar
iabl
eret
ainsi
ts
val
uebet
weenmul
ti
plefunct
ioncal
ls.

St
ati
cvari
abl
esar
eusedbecausethescopeoft
hestati
cvar
iabl
eisav
ail
abl
eint
heent
ir
e
pr
ogram.So,
wecanaccessastat
icvari
abl
eanywherei
ntheprogr
am.

Thestat
icvari
abl
eisini
ti
all
yini
ti
ali
zedt
ozer
o.I
fweupdat
ethev
alueofav
ari
abl
e,t
hent
he
updat
edv al
ueisassi
gned.

Thest
ati
cvar
iabl
eisusedasacommonv
aluewhi
chi
sshar
edbyal
lthemet
hods.

Thest
ati
cvar
iabl
eisi
nit
ial
i
zedonl
yoncei
nthememor
yheapt
oreducet
hememor
yusage.

10-Whati
stheuseoft
hef
unct
ioni
nC?

UsesofCf
unct
ionar
e:

Cf
unct
ionsar
eusedt
oav
oidt
her
ewr
it
ingt
hesamecodeagai
nandagai
ninourpr
ogr
am.

Cf
unct
ionscanbecal
l
edanynumberoft
imesf
rom anypl
aceofourpr
ogr
am.

Whenapr
ogr
am i
sdi
vi
dedi
ntof
unct
ions,
thenanypar
tofourpr
ogr
am caneasi
l
ybet
racked.

Cfunct
ionsprov
idether
eusabi
l
ityconcept
,i.e.
,i
tbr
eakst
hebi
gtaski
ntosmal
l
ert
askssot
hat
i
tmakest heCprogr
am moreunderst
andable.
11-Whati
sthedi
ff
erencebet
weencal
lbyv
alueandcal
lbyr
efer
encei
nC?

Fol
l
owi
ngar
ethedi
ff
erencesbet
weenacal
lbyv
alueandcal
lbyr
efer
encear
e:

Cal
lbyv
alue Cal
lbyr
efer
ence

Descr
ipti
on Whenacopyoft
hev
aluei
spassedt
othef
unct
ion,
thent
heor
igi
nal
val
uei
snot
modif
ied. Whenacopyoft
hev
aluei
spassedt
othef
unct
ion,
thent
heor
igi
nal
val
uei
s
modif
ied.

Memorylocat
ion Act
ualar
gument
sandformalar
gument
sarecr
eatedinsepar
atememor
y
l
ocat
ions. Act ual
argument
sandfor
malargument
sarecr
eat
edinthesamememor y
l
ocat
ion.

Saf
etyInthi
scase,
actual
argumentsremai
nsaf
east heycannotbemodi
fi
ed. I
nthi
scase,
act
ualar
gumentsarenotr
eli
abl
e,astheyar
emodif
ied.

Arguments Thecopi esoft


heact
ualargumentsarepassedtothef
ormalargument
s.The
addressesofact
ual
argument
sarepassedtothei
rrespect
ivef
ormalarguments.

Exampl
eofcal
lbyv
alue:

#i
ncl
ude<st
dio.
h>

v
oidchange(
int
,i
nt)
;

i
ntmai
n()

i
nta=10,
b=20;

change(
a,b)
;//
cal
l
ingaf
unct
ionbypassi
ngt
hev
aluesofv
ari
abl
es.

pr
int
f(“
Val
ueofai
s:%d”
,a)
;

pr
int
f(“
\n”
);

pr
int
f(“
Val
ueofbi
s:%d”
,b)
;

r
etur
n0;

v
oidchange(
intx,
i
nty
)
{

x=13;

y
=17;

Out
put
:

Val
ueofai
s:10

Val
ueofbi
s:20

Exampl
eofcal
lbyr
efer
ence:

#i
ncl
ude<st
dio.
h>

v
oidchange(
int
*,
int
*);

i
ntmai
n()

i
nta=10,
b=20;

change(
&a,
&b)
;//cal
l
ingaf
unct
ionbypassi
ngr
efer
encesofv
ari
abl
es.

pr
int
f(“
Val
ueofai
s:%d”
,a)
;

pr
int
f(“
\n”
);

pr
int
f(“
Val
ueofbi
s:%d”
,b)
;

r
etur
n0;

v
oidchange(
int*
x,i
nt*
y)

*
x=13;

*
y=17;
}

Out
put
:

Val
ueofai
s:13

Val
ueofbi
s:17

12-Whati
srecur
sioni
nC?

Whenaf unct
ioncal
l
si t
self
,andthi
sprocessi
sknownasr
ecur
sion.Thef
unct
iont
hatcal
l
s
i
tsel
fisknownasar ecursi
vefunct
ion.

Recur
siv
efunct
ioncomesi
ntwophases:

Wi
ndi
ngphase

Unwi
ndi
ngphase

Windi
ngphase:
Whent
her
ecur
siv
efunct
ioncal
l
sit
sel
f,andt
hisphaseendswhent
hecondi
ti
on
i
sreached.

Unwindi
ngphase:Unwindi
ngphasest
art
swhent
hecondi
ti
oni
sreached,
andt
hecont
rol
ret
urnst
otheorigi
nalcal
l.

Exampl
eofr
ecur
sion

#i
ncl
ude<st
dio.
h>

i
ntcal
cul
ate_
fact
(i
nt)
;

i
ntmai
n()

i
ntn=5,
f;

f
=cal
cul
ate_
fact
(n)
;//cal
l
ingaf
unct
ion
pr
int
f(“
fact
ori
alofanumberi
s%d”
,f
);

r
etur
n0;

i
ntcal
cul
ate_
fact
(i
nta)

i
f(
a==1)

r
etur
n1;

el
se

r
etur
na*
cal
cul
ate_
fact
(a-
1);
//cal
l
ingaf
unct
ionr
ecur
siv
ely
.

Out
put
:

f
act
ori
alofanumberi
s120

13-Whati
sanar
rayi
nC?

AnAr r
ayi
sagr oupofsi
milarty
pesofel
ements.I
thasaconti
guousmemoryl
ocati
on.I
tmakes
thecodeopti
mized,
easytotraver
seandeasytosor
t.Thesi
zeandty
peofarr
ayscannotbe
changedaft
erit
sdeclar
ati
on.

Ar
ray
sar
eoft
wot
ypes:

One-
dimensi
onal
arr
ay:
One-
dimensi
onal
arr
ayi
sanar
rayt
hatst
orest
heel
ement
soneaf
tert
he
anot
her.

Sy
ntax:

dat
a_t
ypear
ray
_name[
size]
;
Mul
ti
dimensi
onal
arr
ay:
Mul
ti
dimensi
onal
arr
ayi
sanar
rayt
hatcont
ainsmor
ethanonear
ray
.

Sy
ntax:

dat
a_t
ypear
ray
_name[
size]
;

Exampl
eofanar
ray
:

#i
ncl
ude<st
dio.
h>

i
ntmai
n()

i
ntar
r[
5]={
1,2,
3,
4,
5};
//anar
rayconsi
stsoff
ivei
ntegerv
alues.

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

pr
int
f(“
%d“
,ar
r[
i]
);

r
etur
n0;

Out
put
:

12345

14-Whati
sapoi
nteri
nC?

Apointerisav ar
iabl
ethatreferstotheaddressofaval
ue.I tmakesthecodeopti
mi zedand
makest heperfor
mancef ast.Whenev eravari
abl
eisdeclaredinsi
deapr ogr
am,t
hent hesyst
em
all
ocat
essomememor ytoav ar
iabl
e.Thememor ycontainssomeaddr essnumber.The
vari
abl
est hatholdthi
saddressnumberi sknownast hepoi nt
ervari
abl
e.

Forexampl
e:
Dat
a_t
ype*
p;

Theabovesynt
axt
ell
sthatpi
sapoi
nterv
ari
abl
ethathol
dst
headdr
essnumberofagi
vendat
a
ty
pevalue.

Exampl
eofpoi
nter

#i
ncl
ude<st
dio.
h>

i
ntmai
n()

i
nt*
p;/
/poi
nteroft
ypei
nteger
.

i
nta=5;

p=&a;

pr
int
f(“
Addr
essv
alueof‘
a’v
ari
abl
eis%u”
,p)
;

r
etur
n0;

Out
put
:

Addr
essv
alueof'
a'v
ari
abl
eis428781252

15-Whati
stheusageoft
hepoi
nteri
nC?

Accessingar
rayelements:
Poi
ntersareusedi
ntrav
ersi
ngthr
oughanar
rayofi
nteger
sand
st
rings.Thestr
ingisanarr
ayofcharact
erswhi
chister
minat
edbyanul
lchar
acter‘
\0’
.

Dynami
cmemor yal
l
ocation:
Poi
nter
sar
eusedi
nal
l
ocat
ionanddeal
l
ocat
ionofmemor
ydur
ing
t
heexecut
ionofaprogr
am.

Cal
lbyRef
erence:
Thepoi
nter
sar
eusedt
opassar
efer
enceofav
ari
abl
etoot
herf
unct
ion.

Dat
aStr
uctur
esl
ikeatree,
graph,l
inkedl
ist,
etc.
:Thepoi
nter
sar
eusedt
oconst
ructdi
ff
erent
dat
ast
ruct
uresl
i
ketree,gr
aph,li
nkedli
st,et
c.

16-Whati
saNULLpoi
nteri
nC?
Apoi
ntert
hatdoesn’
tref
ert
oanyaddressofval
uebutNULLisknownasaNULLpoi
nter
.When
weassi
gna‘0’val
uetoapoi
nterofanyty
pe,t
henitbecomesaNullpoi
nter
.

17-Whati
saf
arpoi
nteri
nC?

Apoi nterwhichcanaccessal
lthe16segments(whol
er esi
dencememor y
)ofRAM i
sknownas
farpointer
.Af arpoi
nteri
sa32-bi
tpoi
ntert
hatobtai
nsinfor
mat i
onout
sidethememoryi
na
givensection.

18-Whati
sdangl
i
ngpoi
nteri
nC?

I
fapoi nteri
spoint
inganymemorylocat
ion,butmeanwhileanot
herpoint
erdelet
esthememory
occupiedbythefir
stpoi
nterwhi
l
ethefir
stpointerst
il
lpoint
stothatmemorylocati
on,t
hef
ir
st
point
erwi l
lbeknownasadangli
ngpointer
.Thisproblem i
sknownasadanglingpoint
er
probl
em.

Dangl
ingpoi
nterari
seswhenanobj
ecti
sdel
etedwi
thoutmodi
fyi
ngt
hev
alueoft
hepoi
nter
.
Thepoint
erpoint
stothedeal
l
ocat
edmemory.

Let
’sseet
hist
hroughanexampl
e.

#i
ncl
ude<st
dio.
h>

v
oidmai
n()

i
nt*
ptr=mal
l
oc(
const
antv
alue)
;//
all
ocat
ingamemor
yspace.

f
ree(
ptr
);/
/pt
rbecomesadangl
i
ngpoi
nter
.

I
nt heaboveexample,i
nit
ial
l
ymemor yisall
ocatedtot
hepointervari
ableptr,
andt hent
he
memor yisdeall
ocat
edfrom thepoi
nterv
ari
able.Now,poi
ntervar
iable,
i.e.
,ptrbecomesa
dangli
ngpointer
.

Howt
oov
ercomet
hepr
obl
em ofadangl
i
ngpoi
nter
Theproblem ofadangli
ngpoint
ercanbeovercomebyassi
gni
ngaNULLv
aluet
othedangl
i
ng
poi
nter.Let
’sunder
standthi
sthroughanexample:

#i
ncl
ude<st
dio.
h>

v
oidmai
n()

i
nt*
ptr=mal
l
oc(
const
antv
alue)
;//
all
ocat
ingamemor
yspace.

f
ree(
ptr
);/
/pt
rbecomesadangl
i
ngpoi
nter
.

pt
r=NULL;
//Now,
ptri
snol
ongeradangl
i
ngpoi
nter
.

I
ntheaboveexample,af
terdeal
locat
ingt
hememoryfr
om apoi
nterv
ari
able,
ptrisassi
gnedt
oa
NULLval
ue.Thismeansthatptrdoesnotpoi
ntt
oanymemoryl
ocati
on.Theref
ore,i
tisno
l
ongeradangl
ingpoint
er.

19-Whati
spoi
ntert
opoi
nteri
nC?

I
ncaseofapoi ntertopointerconcept,onepoint
err ef
erstot headdressofanot
herpoi
nter
.The
poi
ntertopoint
erisachainofpoi nter
s.Generall
y,thepointercontai
nstheaddressofavar
iabl
e.
Thepointert
opointercontainstheaddressofaf i
rstpointer.Let
’sunderst
andthi
sconcept
thr
oughanex ample:

#i
ncl
ude<st
dio.
h>

i
ntmai
n()

i
nta=10;

i
nt*
ptr
,*
*ppt
r;/
/*pt
risapoi
nterand*
*ppt
risadoubl
epoi
nter
.

pt
r=&a;

ppt
r=&pt
r;

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

pr
int
f(“
val
ueof*
ptri
s:%d”
,*
ptr
);

pr
int
f(“
\n”
);

pr
int
f(“
val
ueof*
*ppt
ris:
%d”
,*
*ppt
r);

r
etur
n0;

I
ntheaboveexample,
pptri
sadoublepoi
nterpoi
nti
ngt
otheaddr
essoft
hept
rvar
iabl
eandpt
r
poi
ntst
otheaddressof‘
a’var
iabl
e.

20-Whati
sst
ati
cmemor
yal
l
ocat
ion?

I
ncaseofst
ati
cmemor yal
locat
ion,memoryi
sall
ocat
edatcompi
l
eti
me,
andmemor
ycan’
tbe
i
ncr
easedwhil
eexecut
ingt
heprogram.Iti
susedi
nthear
ray.

Thel
i
fet
imeofav
ari
abl
einst
ati
cmemor
yist
hel
i
fet
imeofapr
ogr
am.

Thest
ati
cmemor
yisal
l
ocat
edusi
ngst
ati
ckey
wor
d.

Thest
ati
cmemor
yisi
mpl
ement
edusi
ngst
acksorheap.

Thepoi
nteri
srequi
redt
oaccesst
hev
ari
abl
epr
esenti
nthest
ati
cmemor
y.

Thest
ati
cmemor
yisf
ast
ert
handy
nami
cmemor
y.

I
nst
ati
cmemor
y,mor
ememor
yspacei
srequi
redt
ost
oret
hev
ari
abl
e.

Forexampl
e:

i
nta[
10]
;

Theabov
eexampl
ecr
eat
esanar
rayofi
ntegert
ype,
andt
hesi
zeofanar
rayi
sfi
xed,
i.
e.,
10.

21-Whati
sdy
nami
cmemor
yal
l
ocat
ion?

I
ncaseofdynami
cmemor yal
l
ocati
on,
memor yi
sall
ocat
edatrunt
imeandmemor
ycanbe
i
ncr
easedwhil
eexecut
ingt
heprogr
am.Iti
susedi
ntheli
nkedl
i
st.

Themal
l
oc(
)orcal
l
oc(
)funct
ioni
srequi
redt
oal
l
ocat
ethememor
yatt
her
unt
ime.

Anal
l
ocat
ionordeal
l
ocat
ionofmemor
yisdoneatt
heexecut
iont
imeofapr
ogr
am.
Nody
nami
cpoi
nter
sar
erequi
redt
oaccesst
hememor
y.

Thedy
nami
cmemor
yisi
mpl
ement
edusi
ngdat
asegment
s.

Lessmemor
yspacei
srequi
redt
ost
oret
hev
ari
abl
e.

Forexampl
e

i
nt*
p=mal
l
oc(
sizeof
(i
nt)
*10)
;

Theabov
eexampl
eal
l
ocat
est
hememor
yatr
unt
ime.

22-Whatf
unct
ionsar
eusedf
ordy
nami
cmemor
yal
l
ocat
ioni
nCl
anguage?

mal
l
oc(
)

Themal
l
oc(
)funct
ioni
susedt
oal
l
ocat
ethememor
ydur
ingt
heexecut
ionoft
hepr
ogr
am.

I
tdoesnoti
nit
ial
i
zet
hememor
ybutcar
ri
est
hegar
bagev
alue.

I
tret
urnsanul
lpoi
nteri
fitcoul
dnotbeabl
etoal
l
ocat
ether
equest
edspace.

Sy
ntax

pt
r=(
cast
-t
ype*
)mal
l
oc(
byt
e-si
ze)/
/al
l
ocat
ingt
hememor
yusi
ngmal
l
oc(
)funct
ion.

cal
l
oc(
)

Thecal
l
oc()i
ssameasmal
l
oc(
)funct
ion,
butt
hedi
ff
erenceonl
yist
hati
tini
ti
ali
zest
hememor
y
wit
hzerov
alue.

Sy
ntax

pt
r=(
cast
-t
ype*
)cal
l
oc(
n,el
ement
-si
ze)
;/
/al
l
ocat
ingt
hememor
yusi
ngcal
l
oc(
)funct
ion.

r
eal
l
oc(
)

Ther
eal
l
oc(
)funct
ioni
susedt
oreal
l
ocat
ethememor
ytot
henewsi
ze.

Ifsuf
fi
cientspacei
snotavai
labl
eint
hememor
y,t
hent
henewbl
ocki
sal
l
ocat
edt
o
accommodat etheexi
sti
ngdata.

Sy
ntax
pt
r=r
eal
l
oc(
ptr
,newsi
ze)
;//updat
ingt
hememor
ysi
zeusi
ngr
eal
l
oc(
)funct
ion.

I
ntheabov
esy
ntax,
ptri
sal
l
ocat
edt
oanewsi
ze.

f
ree(
):
Thef
ree(
)funct
ionr
eleasest
hememor
yal
l
ocat
edbyei
thercal
l
oc(
)ormal
l
oc(
)funct
ion.

Sy
ntax

f
ree(
ptr
);/
/memor
yisr
eleasedusi
ngf
ree(
)funct
ion.

Theabov
esy
ntaxr
eleasest
hememor
yfr
om apoi
nterv
ari
abl
ept
r.

23-Whati
sthedi
ff
erencebet
weenmal
l
oc(
)andcal
l
oc(
)?

cal
l
oc(
) mal
l
oc(
)

Descri
pti
on Themal l
oc(
)funct
ional
locat
esasinglebl
ockofrequest
edmemor
y. The
cal
loc(
)funct
ional
l
ocat
esmulti
plebl
ocksofrequest
edmemor y
.

Ini
ti
ali
zati
on Iti
nit
ial
i
zesthecontentofthememoryt
ozer
o. I
tdoesnoti
nit
ial
i
zet
he
contentofmemory,
soitcarr
iest
hegar bageval
ue.

Numberofar
gument
sItconsi
stsoft
woar
gument
s.I
tconsi
stsofonl
yonear
gument
.

Ret
urnval
ue Itret
urnsapoi
nterpoi
nti
ngt
otheal
l
ocat
edmemor
y. I
tret
urnsapoi
nter
poi
nti
ngtotheal
l
ocatedmemor y
.

24-Whati
sthest
ruct
ure?

Thestr
uctur
eisauser-
defi
neddatat
ypethatall
owsst
ori
ngmul
ti
plet
ypesofdat
ainasi
ngl
e
uni
t.I
toccupi
esthesum ofthememoryofall
members.

Thest
ruct
uremember
scanbeaccessedonl
ythr
oughst
ruct
urev
ari
abl
es.

St
ruct
urevar
iabl
esaccessi
ngt
hesamest
ruct
urebutt
hememor
yal
l
ocat
edf
oreachv
ari
abl
e
wi
llbedi
ff
erent.

Sy
ntaxofst
ruct
ure
st
ructst
ruct
ure_
name

Member
_var
iabl
e1;

Member
_var
iabl
e2

}
[st
ruct
urev
ari
abl
es]
;

Let
’sseeasi
mpl
eexampl
e.

#i
ncl
ude<st
dio.
h>

st
ructst
udent

charname[
10]
; /
/st
ruct
uremember
sdecl
arat
ion.

i
ntage;

}
s1; /
/st
ruct
urev
ari
abl
e

i
ntmai
n()

pr
int
f(“
Ent
ert
hename”
);

scanf
(“%s”
,s1.
name)
;

pr
int
f(“
\n”
);

pr
int
f(“
Ent
ert
heage”
);

scanf
(“%d”
,&s1.
age)
;

pr
int
f(“
\n”
);

pr
int
f(“
Nameandageofast
udent
:%s,
%d”
,s1.
name,
s1.
age)
;

r
etur
n0;
}

Out
put
:

Ent
ert
henameshi
kha

Ent
ert
heage26

Nameandageofast
udent
:shi
kha,
26

25-Whati
sauni
on?

Theunionisauser-defi
neddat
aty
pet hatal
l
owsstor
ingmul
ti
pletypesofdatai
nasi
ngleunit
.
Howev er
,itdoesn’
toccupyt
hesum ofthememoryofall
members.Ithol
dsthememoryofthe
l
argestmemberonl y.

I
nuni
on,wecanaccessonl
yonev
ari
abl
eatat
imeasi
tal
l
ocat
esonecommonspacef
oral
lthe
member
sofaunion.

Sy
ntaxofuni
on

uni
onuni
on_
name

Member
_var
iabl
e1;

Member
_var
iabl
e2;

Member
_var
iabl
en;

}
[uni
onv
ari
abl
es]
;

Let
’sseeasi
mpl
eexampl
e

#i
ncl
ude<st
dio.
h>

uni
ondat
a

{
i
nta; /
/uni
onmember
sdecl
arat
ion.

f
loatb;

charch;

}
;

i
ntmai
n()

uni
ondat
ad; /
/uni
onv
ari
abl
e.

d.
a=3;

d.
b=5.
6;

d.
ch=‘
a’
;

pr
int
f(“
val
ueofai
s%d”
,d.
a);

pr
int
f(“
\n”
);

pr
int
f(“
val
ueofbi
s%f
”,
d.b)
;

pr
int
f(“
\n”
);

pr
int
f(“
val
ueofchi
s%c”
,d.
ch)
;

r
etur
n0;

Out
put
:

v
alueofai
s1085485921

v
alueofbi
s5.
600022

v
alueofchi
sa

I
nt heaboveexampl
e,theval
ueofaandbgetscorr
upted,
andonl
yvar
iabl
echshowst heactual
output
.Thi
sisbecauseall
themembersofaunionsharet
hecommonmemor yspace.Hence,
thevari
abl
echwhosev al
ueiscur
rent
lyupdat
ed.
26-Whati
sanaut
okey
wor
dinC?

InC,everyl
ocalvar
iableofaf unct
ionisknownasanautomati
c( auto)vari
abl
e.Vari
ableswhi
ch
aredeclar
edinsi
dethef uncti
onblockareknownasalocalvar
iable.Thelocalv
ari
ablesareal
so
knownasanaut ovariable.I
tisopti
onaltouseanaut
okey wor
dbef orethedatatypeofa
vari
abl
e.Ifnoval
uei sstoredinthelocal
vari
abl
e,t
henitconsi
stsofagar bageval
ue.

27-Whati
sthepur
poseofspr
int
f()f
unct
ion?

Thespr i
ntf
()standsfor“st
ri
ngpri
nt.
”Thespri
ntf
()f
uncti
ondoesnotpri
ntt
heoutputonthe
consolescreen.Itt
ransf
ersthedat
atothebuff
er.I
tret
urnst
hetot
alnumberofcharact
ers
presentinthestri
ng.

Sy
ntax

i
ntspr
int
f(char*st
r,constchar*f
ormat
,…)
;

Let
’sseeasi
mpl
eexampl
e

#i
ncl
ude<st
dio.
h>

i
ntmai
n()

chara[
20]
;

i
ntn=spr
int
f(a,
“j
avaToi
nt”
);

pr
int
f(“
val
ueofni
s%d”
,n)
;

r
etur
n0;
}

Out
put
:

v
alueofni
s9

28-Canwecompi
l
eapr
ogr
am wi
thoutmai
n()f
unct
ion?
Yes,
wecancompi
l
e,buti
tcan’
tbeexecut
ed.

But,
ifweuse#def
ine,
wecancompi
l
eandr
unaCpr
ogr
am wi
thoutusi
ngt
hemai
n()f
unct
ion.
Forexampl
e:

#i
ncl
ude<st
dio.
h>

#def
inest
artmai
n

v
oidst
art
(){

pr
int
f(“
Hel
l
o”)
;

29-Whati
sat
oken?

TheTokenisanidenti
fi
er.I
tcanbeconst
ant
,key
word,st
ri
ngl
i
ter
al,
etc.At
okeni
sthesmal
l
est
i
ndiv
idual
uniti
napr ogram.Chasthefol
l
owingt
okens:

I
dent
if
ier
s:I
dent
if
ier
sref
ert
othenameoft
hev
ari
abl
es.

Key
wor
ds:
Key
wor
dsar
ethepr
edef
inedwor
dst
hatar
eexpl
ainedbyt
hecompi
l
er.

Const
ant
s:Const
ant
sar
ethef
ixedv
aluest
hatcannotbechangeddur
ingt
heexecut
ionofa
pr
ogram.

Oper
ator
s:Anoper
atori
sasy
mbol
thatper
for
mst
hepar
ti
cul
aroper
ati
on.

Special
charact
ers:
All
thechar
act
ersexceptal
phabet
sanddi
git
sar
etr
eat
edasspeci
al
charact
ers.

30-Whati
scommandl
i
near
gument
?

Thear
gumentpassedtothemai n(
)funct
ionwhi
l
eexecut
ingt
hepr
ogr
am i
sknownas
commandl
ineargument.Forexample:

mai
n(i
ntcount
,char*
args[
])
{

/
/codet
obeexecut
ed
}

31-Whati
stheacr
ony
mforANSI
?

TheANSIstandsfor”AmericanNati
onal
StandardInst
it
ute.”I
tisanorgani
zati
onthatmaint
ains
thebr
oadrangeofdisci
pli
nesincl
udi
ngphotographi
cfil
m, computerl
anguages,dat
aencodi
ng,
mechani
calpart
s,safet
yandmor e.

32-Whati
sthedi
ff
erencebet
weenget
ch(
)andget
che(
)?

Thegetch(
)funct
ionreadsasingl
echar
acterfr
om thekey
boar
d.I
tdoesn’
tuseanybuf
fer
,so
ent
ereddatawil
lnotbedispl
ayedontheoutputscr
een.

Theget
che()f
unctionr
eadsasi
ngl
echar
acterf
rom thekey
wor
d,butdat
aisdi
spl
ayedont
he
out
putscr
een.PressAlt
+f5t
oseet
heenter
edcharacter
.

Let
’sseeasi
mpl
eexampl
e

#i
ncl
ude<st
dio.
h>

#i
ncl
ude<coni
o.h>

i
ntmai
n()

charch;

pr
int
f(“
Ent
erachar
act
er“
);

ch=get
ch(
);/
/taki
nganuseri
nputwi
thoutpr
int
ingt
hev
alue.

pr
int
f(“
\nv
alueofchi
s%c”
,ch)
;

pr
int
f(“
\nEnt
erachar
act
eragai
n“)
;

ch=get
che(
);/
/taki
nganuseri
nputandt
hendi
spl
ayi
ngi
tont
hescr
een.

pr
int
f(“
\nv
alueofchi
s%c”
,ch)
;
r
etur
n0;

charch;

pr
int
f(“
Ent
erachar
act
er“
);

ch=get
ch(
);/
/taki
nganuseri
nputwi
thoutpr
int
ingt
hev
alue.

pr
int
f(“
\nv
alueofchi
s%c”
,ch)
;

pr
int
f(“
\nEnt
erachar
act
eragai
n“)
;

ch=get
che(
);/
/taki
nganuseri
nputandt
hendi
spl
ayi
ngi
tont
hescr
een.

pr
int
f(“
\nv
alueofchi
s%c”
,ch)
;

r
etur
n0;

Out
put
:

Ent
erachar
act
er

v
alueofchi
sa

Ent
erachar
act
eragai
na

v
alueofchi
sa

Int
heabov
eexampl
e,t
hevalueenter
edt
hroughagetch()f
unct
ioni
snotdi
spl
ayedonthe
scr
eenwhi
let
heval
ueent
eredthr
oughagetche(
)funct
ionisdi
spl
ayedont
hescreen.

33-Whati
sthenewl
i
neescapesequence?

Thenewl
i
neescapesequencei
srepr
esent
edby“
\n”
.Iti
nser
tsanewl
i
neont
heout
putscr
een.

34-Whoi
sthemai
ncont
ri
but
ori
ndesi
gni
ngt
heCl
anguageaf
terDenni
sRi
tchi
e?

Br
ainKer
nighan.
35-Whati
sthedi
ff
erencebet
weennear
,farandhugepoi
nter
s?

Av
irt
ual
addr
essi
scomposedoft
hesel
ect
orandof
fset
.

Anearpoi
nterdoesn’
thaveexpli
citsel
ectorwhereasf
ar,
andhugepoint
ershaveexpl
i
cit
sel
ect
or.Wheny ouper
form poi
nterari
thmeticonthef
arpoi
nter
,thesel
ect
orisnotmodi
fied,
buti
ncaseofahugepoi nt
er,i
tcanbemodi f
ied.

Thesear
ethenon-
standar
dkey
wor
dsandi
mpl
ement
ati
onspeci
fi
c.Thesear
eir
rel
evanti
na
modernpl
atf
orm.

36-Whati
sthemaxi
mum l
engt
hofani
dent
if
ier
?

I
tis32char
act
ersi
deal
l
ybuti
mpl
ement
ati
onspeci
fi
c.

37-Whati
sty
pecast
ing?

Thetypecasti
ngisaprocessofconv
ert
ingonedataty
peint
oanot
herisknownastypecast
ing.
Ifwewantt ostor
ethefl
oatingt
ypeval
uetoanintt
ype,t
henwewil
lconver
tthedat
atypeinto
anot
herdatatypeexpli
cit
ly.

Sy
ntax

(
type_
name)expr
essi
on;

38-Whatar
ethef
unct
ionst
oopenandcl
oset
hef
il
einCl
anguage?

Thef
open(
)funct
ioni
susedt
oopenf
il
ewher
easf
close(
)isusedt
ocl
osef
il
e.

39-Canweaccesst
hear
rayusi
ngapoi
nteri
nCl
anguage?

Yes,
byhol
dingt
hebaseaddr
essofar
rayi
ntoapoi
nter
,wecanaccesst
hear
rayusi
ngapoi
nter
.
40-Whati
sani
nfi
nit
eloop?

Al
oopr
unni
ngcont
inuousl
yforani
ndef
ini
tenumberoft
imesi
scal
l
edt
hei
nfi
nit
eloop.

I
nfi
nit
eForLoop:

f
or(
;;
){

/
/codet
obeexecut
ed

I
nfi
nit
eWhi
l
eLoop:

whi
l
e(1)
{

/
/codet
obeexecut
ed

I
nfi
nit
eDo-
Whi
l
eLoop:

do{

/
/codet
obeexecut
ed

}
whi
l
e(1)
;

41-Wr
it
eapr
ogr
am t
opr
int“
hel
l
owor
ld”wi
thoutusi
ngasemi
col
on?

#i
ncl
ude<st
dio.
h>

v
oidmai
n()
{

i
f(
pri
ntf
(“hel
l
owor
ld”
)){
}//I
tpr
int
sthe?
hel
l
owor
ld?ont
hescr
een.

42-Wr
it
eapr
ogr
am t
oswapt
wonumber
swi
thoutusi
ngt
het
hir
dvar
iabl
e?

#i
ncl
ude<st
dio.
h>
#i
ncl
ude<coni
o.h>

mai
n()

i
nta=10,
b=20; /
/decl
arat
ionofv
ari
abl
es.

cl
rscr
(); /
/Itcl
ear
sthescr
een.

pr
int
f(“
Bef
oreswapa=%db=%d”
,a,
b);

a=a+b;
//a=30(
10+20)

b=a-
b;/
/b=10(
30-
20)

a=a-
b;/
/a=20(
30-
10)

pr
int
f(“
\nAf
terswapa=%db=%d”
,a,
b);

get
ch(
);

a=a+b;
//a=30(
10+20)

b=a-
b;/
/b=10(
30-
20)

a=a-
b;/
/a=20(
30-
10)

pr
int
f(“
\nAf
terswapa=%db=%d”
,a,
b);

get
ch(
);

43-Wr
it
eapr
ogr
am t
opr
intFi
bonacci
ser
ieswi
thoutusi
ngr
ecur
sion?

#i
ncl
ude<st
dio.
h>

#i
ncl
ude<coni
o.h>

v
oidmai
n()
{

i
ntn1=0,
n2=1,
n3,
i
,number
;

cl
rscr
();

pr
int
f(“
Ent
ert
henumberofel
ement
s:”
);

scanf
(“%d”
,&number
);

pr
int
f(“
\n%d%d”
,n1,
n2)
;/
/pr
int
ing0and1

f
or(
i=2;
i
<number
;++i
)//
loopst
art
sfr
om 2because0and1ar
eal
readypr
int
ed

n3=n1+n2;

pr
int
f(”%d”
,n3)
;

n1=n2;

n2=n3;

get
ch(
);

f
or(
i=2;
i
<number
;++i
)//
loopst
art
sfr
om 2because0and1ar
eal
readypr
int
ed

n3=n1+n2;

pr
int
f(”%d”
,n3)
;

n1=n2;

n2=n3;

get
ch(
);

}
44-Wr
it
eapr
ogr
am t
opr
intFi
bonacci
ser
iesusi
ngr
ecur
sion?

#i
ncl
ude<st
dio.
h>

#i
ncl
ude<coni
o.h>

v
oidpr
int
Fibonacci
(i
ntn)/
/funct
iont
ocal
cul
atet
hef
ibonacci
ser
iesofagi
vennumber
.

st
ati
cintn1=0,
n2=1,
n3; /
/decl
arat
ionofst
ati
cvar
iabl
es.

i
f(
n>0)
{

n3=n1+n2;

n1=n2;

n2=n3;

pr
int
f(“
%d“
,n3)
;

pr
int
Fibonacci
(n-
1);/
/cal
l
ingt
hef
unct
ionr
ecur
siv
ely
.

v
oidmai
n()
{

i
ntn;

cl
rscr
();

pr
int
f(“
Ent
ert
henumberofel
ement
s:“
);

scanf
(“%d”
,&n)
;

pr
int
f(“
Fibonacci
Ser
ies:
“);

pr
int
f(“
%d%d“
,0,
1);

pr
int
Fibonacci
(n-
2);
//n-
2because2number
sar
eal
readypr
int
ed

get
ch(
);

45-Wr
it
eapr
ogr
am t
ocheckpr
imenumberi
nCPr
ogr
ammi
ng?
#i
ncl
ude<st
dio.
h>

#i
ncl
ude<coni
o.h>

v
oidmai
n()

i
ntn,
i
,m=0,
fl
ag=0; /
/decl
arat
ionofv
ari
abl
es.

cl
rscr
();/
/Itcl
ear
sthescr
een.

pr
int
f(“
Ent
ert
henumbert
ocheckpr
ime:
”)
;

scanf
(“%d”
,&n)
;

m=n/
2;

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

i
f(
n%i
==0)

pr
int
f(“
Numberi
snotpr
ime”
);

f
lag=1;

br
eak; /
/br
eakkey
wor
dusedt
oter
minat
efr
om t
hel
oop.

i
f(
fl
ag==0)

pr
int
f(“
Numberi
spr
ime”
);

get
ch(
);/
/Itr
eadsachar
act
erf
rom t
hekey
wor
d.

46-Wr
it
eapr
ogr
am t
ocheckpal
i
ndr
omenumberi
nCPr
ogr
ammi
ng?

#i
ncl
ude<st
dio.
h>

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

i
ntn,
r,
sum=0,
temp;

cl
rscr
();

pr
int
f(“
ent
ert
henumber
=”)
;

scanf
(“%d”
,&n)
;

t
emp=n;

whi
l
e(n>0)

r
=n%10;

sum=(
sum*
10)
+r;

n=n/
10;

i
f(
temp==sum)

pr
int
f(“
pal
i
ndr
omenumber“
);

el
se

pr
int
f(“
notpal
i
ndr
ome”
);

get
ch(
);

47-Wr
it
eapr
ogr
am t
opr
intf
act
ori
alofgi
vennumberwi
thoutusi
ngr
ecur
sion?

#i
ncl
ude<st
dio.
h>

#i
ncl
ude<coni
o.h>

v
oidmai
n()
{

i
nti
,
fact
=1,
number
;

cl
rscr
();
pr
int
f(“
Ent
eranumber
:“)
;

scanf
(“%d”
,&number
);

f
or(
i=1;
i
<=number
;i
++)
{

f
act
=fact
*i;

pr
int
f(“
Fact
ori
alof%di
s:%d”
,number
,f
act
);

get
ch(
);

f
or(
i=1;
i
<=number
;i
++)
{

f
act
=fact
*i
;

pr
int
f(“
Fact
ori
alof%di
s:%d”
,number
,f
act
);

get
ch(
);

48-Wr
it
eapr
ogr
am t
opr
intf
act
ori
alofgi
vennumberusi
ngr
ecur
sion?

#i
ncl
ude<st
dio.
h>

#i
ncl
ude<coni
o.h>

l
ongf
act
ori
al(
intn) /
/funct
iont
ocal
cul
atet
hef
act
ori
alofagi
vennumber
.

i
f(n==0)

r
etur
n1;

el
se

r
etur
n(n*f
act
ori
al(
n-1)
);/
/cal
l
ingt
hef
unct
ionr
ecur
siv
ely
.

}
v
oidmai
n()

i
ntnumber
;//
decl
arat
ionofv
ari
abl
es.

l
ongf
act
;

cl
rscr
();

pr
int
f(“
Ent
eranumber
:“)
;

scanf
(“%d”
,&number
);

f
act=f
act
ori
al(
number
);/
/cal
l
ingaf
unct
ion.

pr
int
f(“
Fact
ori
alof%di
s%l
d\n”
,number
,fact
);

get
ch(
);/
/Itr
eadsachar
act
erf
rom t
hekey
wor
d.

49-Wr
it
eapr
ogr
am t
ocheckAr
mst
rongnumberi
nC?

#i
ncl
ude<st
dio.
h>

#i
ncl
ude<coni
o.h>

mai
n()

i
ntn,
r,
sum=0,
temp; /
/decl
arat
ionofv
ari
abl
es.

cl
rscr
();
//I
tcl
ear
sthescr
een.

pr
int
f(“
ent
ert
henumber
=”)
;

scanf
(“%d”
,&n)
;

t
emp=n;

whi
l
e(n>0)

r
=n%10;

sum=sum+(
r*r
*r)
;
n=n/
10;

i
f(
temp==sum)

pr
int
f(“
armst
rongnumber“
);

el
se

pr
int
f(“
notar
mst
rongnumber
”);

get
ch(
);/
/Itr
eadsachar
act
erf
rom t
hekey
wor
d.

50-Wr
it
eapr
ogr
am t
orev
erseagi
vennumberi
nC?

#i
ncl
ude<st
dio.
h>

#i
ncl
ude<coni
o.h>

mai
n()

i
ntn,
rev
erse=0,
rem; /
/decl
arat
ionofv
ari
abl
es.

cl
rscr
();
//I
tcl
ear
sthescr
een.

pr
int
f(“
Ent
eranumber
:“)
;

scanf
(“%d”
,&n)
;

whi
l
e(n!
=0)

r
em=n%10;

r
ever
se=r
ever
se*
10+r
em;

n/
=10;

pr
int
f(“
Rev
ersedNumber
:%d”
,r
ever
se)
;

get
ch(
);/
/Itr
eadsachar
act
erf
rom t
hekey
wor
d.
}

You might also like