c-q & a
TRANSCRIPT
8/11/2019 C-Q & A
http://slidepdf.com/reader/full/c-q-a 1/65
C- FAQs
1.
What will be the output of the following code?
void main (){ int i = 0 , a[3 !a[i = i""!p#intf ($%d&,a[i) !'n* +he output fo# the above code would be a ga#bage value. n the tatement a[i= i""! the value of the va#iable i would get aigned fi#t to a[i i.e. a[0 and thenthe value of i would get inc#emented b- 1. ince a[i i.e. a[1 ha not beeninitiali/ed, a[i will have a ga#bage value.
.
Wh- doen2t the following code give the dei#ed #eult?
int = 3000, - = 000 !long int / = 4 - !n* 5e#e the multiplication i ca##ied out between two int and -, and the #eultthat would ove#flow would be t#uncated befo#e being aigned to the va#iable / oft-pe long int. 5oweve#, to get the co##ect output, we hould ue an eplicit cat tofo#ce long a#ithmetic a hown below*
long int / = ( long int ) 4 - !6ote that ( long int )( 4 - ) would not give the dei#ed effect.
3.
Wh- doen2t the following tatement wo#7?
cha# t#[ = &5ello& !t#cat ( t#, 282 ) !n* +he t#ing function t#cat( ) concatenate t#ing and not a cha#acte#. +he baicdiffe#ence between a t#ing and a cha#acte# i that a t#ing i a collection ofcha#acte#, #ep#eented b- an a##a- of cha#acte# whe#ea a cha#acte# i a inglecha#acte#. +o ma7e the above tatement wo#7 w#ite the tatement a hown below*t#cat ( t#, &8& ) !
8/11/2019 C-Q & A
http://slidepdf.com/reader/full/c-q-a 2/65
9.
5ow do 7now how man- element an a##a- can hold?
n* +he amount of memo#- an a##a- can conume depend on the data t-pe of ana##a-. n :; envi#onment, the amount of memo#- an a##a- can conume dependon the cu##ent memo#- model (i.e. +in-, mall, <a#ge, 5uge, etc.). n gene#al ana##a- cannot conume mo#e than 9 7b. >onide# following p#og#am, which howthe maimum numbe# of element an a##a- of t-pe int, float and cha# can have incae of mall memo#- model.main( ){int i[3 !float f[13@3 !cha# [AA3A !'
A.
5ow do w#ite code that #ead data at memo#- location pecified b- egment andoffet?
n* Be pee7b( ) function. +hi function #etu#n b-te() #ead f#om pecific egmentand offet location in memo#-. +he following p#og#am illut#ate ue of thi function.n thi p#og#am f#om C:B memo#- we have #ead cha#acte# and it att#ibute of thefi#t #ow. +he info#mation to#ed in file i then fu#the# #ead and dipla-ed uing pee7() function.
Dinclude Etdio.hFDinclude Edo.hF
main( ){
cha# fa# 4c# = 0G@000000 !H<I 4fp !int offet !cha# ch !
if ( ( fp = fopen ( &c#.dat&, &wb& ) ) == 6B<< ){
p#intf ( &JnBnable to open file& ) !eit( ) !
'
8/11/2019 C-Q & A
http://slidepdf.com/reader/full/c-q-a 3/65
KK #ead and w#ite to filefo# ( offet = 0 ! offet E 10 ! offet"" )fp#intf ( fp, &%c&, pee7b ( c#, offet ) ) !fcloe ( fp ) !
if ( ( fp = fopen ( &c#.dat&, &#b& ) ) == 6B<< )
{
p#intf ( &JnBnable to open file& ) !eit( ) !
'
KK #ead and w#ite to filefo# ( offet = 0 ! offet E 10 ! offet"" ){
fcanf ( fp, &%c&, Lch ) !
p#intf ( &%c&, ch ) !
'
fcloe ( fp ) !
'
.5ow do compa#e cha#acte# data to#ed at two diffe#ent memo#- location?
n* ometime in a p#og#am we #eMui#e to compa#e memo#- #ange containingt#ing. n uch a ituation we can ue function li7e memcmp( ) o# memicmp( ). +hebaic diffe#ence between two function i that memcmp( ) doe a caeenitivecompa#ion whe#ea memicmp( ) igno#e cae of cha#acte#. Hollowing p#og#amillut#ate the ue of both the function.
Dinclude Emem.hF
main( ){cha# 4a##1 = &Nicit& !
cha# 4a## = &7icit6agpu#& !
int c !
c = memcmp ( a##1, a##, i/eof ( a##1 ) ) !
if ( c == 0 )p#intf ( &Jnt#ing a##1 and a## compa#ed uing memcmp a#e identical& ) !
8/11/2019 C-Q & A
http://slidepdf.com/reader/full/c-q-a 4/65
elep#intf ( &Jnt#ing a##1 and a## compa#ed uing memcmp a#e not identical&) !
c = memicmp ( a##1, a##, i/eof ( a##1 ) ) !
if ( c == 0 )p#intf ( &Jnt#ing a##1 and a## compa#ed uing memicmp a#e identical& )!elep#intf ( &Jnt#ing a##1 and a## compa#ed uing memicmp a#e notidentical& ) !'
.Hiedi/e obOect a#e mo#e app#op#iate a compa#ed to va#iable i/e data obOect.
Bing va#iablei/e data obOect ave ve#- little pace. Ca#iable i/e data obOectuuall- have ome ove#head. Panipulation of fiedi/e data obOect i uuall- fate#and eaie#. Be fied i/e when maimum i/e i clea#l- bounded and cloe toave#age. nd ue va#iablei/e data obOect when a few of the data item a#e bigge#than the ave#age i/e. Ho# eample,
cha# 4num[10 = { &;ne&, &+wo&, &+h#ee&, &Hou#&,&Hive&, &i&, &even&, &Iight&, &6ine&, &+en& ' !
ntead of uing the above, ue
cha# num[10[ = { &;ne&, &+wo&, &+h#ee&, &Hou#&,
&Hive&, &i&, &even&, &Iight&, &6ine&, &+en& ' !
+he fi#t fo#m ue va#iablei/e data obOect. t allocate 10 pointe#, which a#epointing to 10 t#ing contant of va#iable i/e. uming each pointe# i of 9 b-te,it #eMui#e Q0 b-te. ;n the othe# hand, the econd fo#m ue fied i/e dataobOect. t allocate 10 a##a- of cha#acte# each. t #eMui#e onl- 0 b-te ofpace. o, the va#iablei/e in thi cae doe not offe# an- advantage ove# fied i/e.@.
+he pawnl( ) function...
:; i a ingle ta7ing ope#ating -tem, thu onl- one p#og#am #un at a time. +hepawnl( ) function p#ovide u with the capabilit- of ta#ting the eecution of onep#og#am f#om within anothe# p#og#am. +he fi#t p#og#am i called the pa#ent p#oceand the econd p#og#am that get called f#om within the fi#t p#og#am i called achild p#oce. ;nce the econd p#og#am ta#t eecution, the fi#t i put on hold untilthe econd p#og#am complete eecution. +he fi#t p#og#am i then #eta#ted. +hefollowing p#og#am demont#ate ue of pawnl( ) function.
8/11/2019 C-Q & A
http://slidepdf.com/reader/full/c-q-a 5/65
K4 Pult.c 4K
int main ( int a#gc, cha#4 a#gv[ ){int a[3, i, #et !if ( a#gc E 3 RR a#gc F 3 )
{p#intf ( &+oo man- o# +oo few a#gument...& ) !eit ( 0 ) !'
fo# ( i = 1 ! i E a#gc ! i"" )a[i = atoi ( a#gv[i ) !#et = a[1 4 a[ !#etu#n #et !'
K4 pawn.c 4K
Dinclude Ep#oce.hFDinclude Etdio.hF
main( ){int val !val = pawnl ( STW+, &>*JJPult.ee&, &3&, &10&,&0&, 6B<< ) !p#intf ( &JnUetu#ned value i* %d&, val ) !'
5e#e, the#e a#e two p#og#am. +he p#og#am 2Pult.ee2 wo#7 a a child p#ocewhe#ea 2pawn.ee2 wo#7 a a pa#ent p#oce. ;n eecution of 2pawn.ee2 itinvo7e 2Pult.ee2 and pae the commandline a#gument to it. 2Pult.ee2 in tu#non eecution, calculate the p#oduct of 10 and 0 and #etu#n the value to val in2pawn.ee2. n ou# call to pawnl( ) function, we have paed pa#amete#, STW+a the mode of eecution, path of 2.ee2 file to #un a child p#oce, total numbe# ofa#gument to be paed to the child p#oce, lit of command line a#gument and6B<<. STW+ will caue ou# application to f#ee/e eecution until the child p#oceha completed it eecution. +hi pa#amete# need to be paed a the defaultpa#amete# if -ou a#e wo#7ing unde# :;. unde# othe# ope#ating -tem thatuppo#t multita7ing, thi pa#amete# can be ST6;W+ o# ST;CIU<V. ST6;W+will caue the pa#ent p#oce to eecute along with the child p#oce, ST;CIU<V willload the child p#oce on top of the pa#ent p#oce in the memo#-.
Q.
#e the following two tatement identical?
cha# t#[ = &Nicit& !cha# 4t# = &Nicit& !n* 6o8 ##a- a#e not pointe#. n a##a- i a ingle, p#eallocated chun7 ofcontiguou element (all of the ame t-pe), fied in i/e and location. pointe# on
8/11/2019 C-Q & A
http://slidepdf.com/reader/full/c-q-a 6/65
the othe# hand, i a #efe#ence to an- data element (of a pa#ticula# t-pe) locatedan-whe#e. pointe# mut be aigned to point to pace allocated elewhe#e, but itcan be #eaigned an- time. +he a##a- decla#ation cha# t#[ ! #eMuet that pacefo# cha#acte# be et aide, to be 7nownb- name t#. n othe# wo#d the#e i a location named t# at which i cha#acte# a#eto#ed. +he pointe# decla#ation cha# 4t# ! on the othe# hand, #eMuet a place that
hold a pointe#, to be 7nown b- the name t#. +hi pointe# can point almot an-whe#eto an- cha#, to an- contiguou a##a- of cha#, o# nowhe#e.10.
the following code f#agment co##ect?
cont int = 10 !int a##[ !n* 6o8 5e#e, the va#iable i fi#t decla#ed a an int o memo#- i #ee#ved fo# it.+hen it i Mualified b- a cont Mualifie#. 5ence, cont Mualified obOect i not a contant
full-. t i an obOect with #ead onl- att#ibute, and in >, an obOect aociated withmemo#- cannot be ued in a##a- dimenion.
11.
5ow do w#ite code to #et#ieve cu##ent date and time f#om the -tem and dipla- ita a t#ing?
n* Be time( ) function to get cu##ent date and time and then ctime( ) function todipla- it a a t#ing. +hi i hown in following code nippet.
Dinclude E-Jt-pe.hF
void main( ){timeTt cu#time !cha# ctm[A0 !
time ( Lcu#time ) ! KK#et#ieve cu##ent time Lto#e in cu#timep#intf ( &Jn>u##ent :ate L +ime* %&, ctime (Lcu#time ) ) !'
1.
5ow do change the t-pe of cu#o# and hide a cu#o#?
n* We can change the cu#o# t-pe b- uing function Tetcu#o#t-pe( ). +hifunction can change the cu#o# t-pe to olid cu#o# and can even hide a cu#o#.Hollowing code how how to change the cu#o# t-pe and hide cu#o#.
8/11/2019 C-Q & A
http://slidepdf.com/reader/full/c-q-a 7/65
Dinclude Econio.hFmain( ){ K4 5ide cu#o# 4K Tetcu#o#t-pe ( T6;>BU;U ) !
K4 >hange cu#o# to a olid cu#o# 4K Tetcu#o#t-pe ( T;<:>BU;U ) !
K4 >hange bac7 to the no#mal cu#o# 4K Tetcu#o#t-pe ( T6;UP<>BU;U ) !'13.
5ow do w#ite code that would get e##o# numbe# and dipla- e##o# meage if an-tanda#d e##o# occu#?
n* Hollowing code demont#ate thi.
Dinclude Etdio.hFDinclude Etdlib.hFDinclude Ee##no.hF
main( ){cha# 4e##mg !H<I 4fp !fp = fopen ( &>*Jfile.tt&, &#& ) !
if ( fp == 6B<< ){e##mg = t#e##o# ( e##no ) !p#intf ( &Jn%&, e##mg ) !''5e#e, we a#e t#-ing to open 2file.tt2 file. 5oweve#, if the file doe not eit, then itwould caue an e##o#. a #eult, a value (in thi cae ) #elated to the e##o#gene#ated would get et in e##no. e##no i an ete#nal int va#iable decla#ed in2tdlib.h2 and alo in 2e##no.h2. 6et, we have called te##o#( ) function which ta7e ane##o# numbe# and #etu#n a pointe# to tanda#d e##o# meage #elated to the givene##o# numbe#.
19.
5ow do w#ite code to get the cu##ent d#ive a well a et the cu##ent d#ive?
n* +he function getdi7( ) #etu#n the d#ive numbe# of cu##ent d#ive. +he d#ivenumbe# 0 indicate 22 a the cu##ent d#ive, 1 a 2G2 and o on. +he etdi7( )function et the cu##ent d#ive. +hi function ta7e one a#gument which i an intege#
8/11/2019 C-Q & A
http://slidepdf.com/reader/full/c-q-a 8/65
indicating the d#ive to be et. Hollowing p#og#am demont#ate ue of both thefunction.
Dinclude Edi#.hF
main( )
{int dno, mad# !
dno = getdi7( ) !p#intf ( &Jn+he cu##ent d#ive i* %cJn&, A " dno) !
mad# = etdi7 ( 3 ) !dno = getdi7( ) !p#intf ( &Jn6ow the cu##ent d#ive i* %cJn&, A "dno ) !'
1A.
+he function memcmp( ) and memicmp( )
+he function memcmp( ) and memicmp( ) compa#e fi#t n b-te of given twobloc7 of memo#- o# t#ing. 5oweve#, memcmp( ) pe#fo#m compa#ion a unignedcha# whe#ea memicmp( ) pe#fo#m compa#ion a cha# but igno#e cae (i.e.uppe# o# lowe# cae). Goth the function #etu#n an intege# value whe#e 0 indicatethat two memo#- buffe# compa#ed a#e identical. f the value #etu#ned i g#eate#than 0 then it indicate that the fi#t buffe# i bigge# than the econd one. +he value
le than 0 indicate that the fi#t buffe# i le than the econd buffe#. +he followingcode nippet demont#ate ue of both
Dinclude Etdio.hFDinclude Emem.hF
main( ){cha# t#1[ = &+hi t#ing contain omecha#acte#& !cha# t#[ = &thi t#ing contain& !int #eult !
#eult = memcmp ( t#1, t#, t#len ( t# ) ) !p#intf ( &JnUeult afte# comap#ing buffe# uingmemcmp( )& ) !how ( #eult ) !
#eult = memicmp ( t#1, t#, t#len ( t# ) ) !p#intf ( &JnUeult afte# comap#ing buffe# uingmemicmp( )& ) !
8/11/2019 C-Q & A
http://slidepdf.com/reader/full/c-q-a 9/65
how ( #eult ) !'
how ( int # ){if ( # == 0 )
p#intf ( &Jn+he buffe# t#1 and t# holdidentical data& ) !if ( # F 0 )p#intf ( &Jn+he buffe# t#1 i bigge# than buffe#t#& ) !if ( # E 0 )p#intf ( &Jn+he buffe# t#1 i le than buffe#t#& ) !'
1.
5ow do w#ite code to find an amount of f#ee di7 pace available on cu##ent d#ive?
n* Be getdf#ee( ) function a hown in follow code.
Dinclude Etdio.hFDinclude Etdlib.hFDinclude Edi#.hFDinclude Edo.hF
main( )
{int d# ! t#uct df#ee di7 !long f#eep !
d# = getdi7( ) !getdf#ee ( d# " 1 , Ldi7 ) !
if ( di7.dfTclu == 0HHHH ){p#intf ( &Jngetdf#ee( ) function failedJn&)!eit ( 1 ) !'
f#eep = ( long ) di7.dfTavail4 ( long ) di7.dfTbec4 ( long ) di7.dfTclu !p#intf ( &Jn+he cu##ent d#ive %c* ha %ld b-teavailable a f#ee paceJn&, 22 " d#, f#eep ) !'
1.
8/11/2019 C-Q & A
http://slidepdf.com/reader/full/c-q-a 10/65
Be of a##a- indice...f we wih to to#e a cha#acte# in a cha# va#iable ch and the cha#acte# to be to#eddepend on the value of anothe# va#iable a- colo# (of t-pe int), then the code wouldbe a hown below*
witch ( colo# )
{cae 0 *ch = 2U2 !b#ea7 !cae 1 *ch = 22 !b#ea7 !cae *ch = 2G2 !b#ea7 !'n place of witchcae we can ma7e ue of the value in colo# a an inde fo# acha#acte# a##a-. 5ow to do thi i hown in following code nippet.
cha# 4t# = &UG2 !cha# ch !int colo# ! KK codech = t#[ colo# !1@.
Hunction ateit( ) #ecevie pa#amete# a the add#e of function of the t-pe void fun( void ). +he function whoe add#e i paed to ateit( ) get called befo#e the
te#mination of p#og#am. f ateit( ) i called fo# mo#e than one function then thefunction a#e called in &fi#t in lat out& o#de#. Vou can ve#if- that f#om the output.
Dinclude Etdio.hFDinclude Etdlib.hFvoid fun1( ){p#intf(&nide fun1Jn&)!'
void fun( ){
p#intf(&nide funJn&)!'main( ){ateit ( fun1 ) ! K4 ome code 4Kateit ( fun ) !p#intf ( &+hi i the lat tatement of
8/11/2019 C-Q & A
http://slidepdf.com/reader/full/c-q-a 11/65
p#og#am?Jn& )!'
1Q.
5ow do w#ite a ue#defined function, which delete each cha#acte# in a t#ing t#1,which matche an- cha#acte# in t#ing t#?
n* +he function i a hown below*
>omp#e ( cha# t#1[, cha# t#[ ){int i, O, 7 !
fo# ( i = 7 = 0 ! t#1[i 8= XJ0Y ! i"" )
{fo# ( O = 0 ! t#[O 8= XJ0Y LL t#[O 8=t#1[i ! O"" )!if ( t#[O == XJ0Y )t#1[7"" = t#1[ !'t#1[7 = XJ0Y'
0.
5ow doe f#ee( ) 7now how man- b-te to f#ee?
n* +he malloc( ) K f#ee( ) implementation #emembe# the i/e of each bloc7allocated and #etu#ned, o it i not necea#- to #emind it of the i/e when f#eeing.1.
What i the ue of #andomi/e( ) and #and( ) function?
n* While gene#ating #andom numbe# in a p#og#am, ometime we #eMui#e tocont#ol the e#ie of numbe# that #andom numbe# gene#ato# c#eate. +he p#oce of aigning the #andom numbe# gene#ato# ta#ting numbe# i called eeding thegene#ato#. +he #andomi/e( ) and #and( ) function a#e ued to eed the #andomnumbe# gene#ato#. +he #andomi/e( ) function ue S>2 cloc7 to p#oduce a #andomeed, whe#ea the #and( ) function allow u to pecif- the #andom numbe#gene#ato#2 ta#ting value.
8/11/2019 C-Q & A
http://slidepdf.com/reader/full/c-q-a 12/65
.
5ow do dete#mine amount of memo#- cu##entl- available fo# allocating?
n* We can ue function co#eleft( ) to get the amount of memo#- available fo#allocation. 5oweve#, thi function doe not give an eact amount of unued memo#-.f, we a#e uing a mall memo#- model, co#eleft( ) #etu#n the amount of unuedmemo#- between the top of the heap and tac7. f we a#e uing a la#ge# model, thifunction #etu#n the amount of memo#- between the highet allocated memo#- andthe end of conventional memo#-. +he function #etu#n amount of memo#- in te#m of b-te.
3.
5ow doe a > p#og#am come to 7now about command line a#gument?
n* When we eecute ou# > p#og#am, ope#ating -tem load the p#og#am intomemo#-. n cae of :;, it fi#t load A b-te into memo#-, called p#og#amegment p#efi. +hi contain file table, envi#onment egment, and command lineinfo#mation. When we compile the > p#og#am the compile# ine#t additional codethat pa#e the command, aigning it to the a#gv a##a-, ma7ing the a#gumenteail- acceible within ou# > p#og#am.
9.
When we open a file, how doe function li7e f#ead( )Kfw#ite( ), etc. get to 7now f#om
whe#e to #ead o# to w#ite the data?
n* When we open a file fo# #eadKw#ite ope#ation uing function li7e fopen( ), it#etu#n a pointe# to the t#uctu#e of t-pe H<I. +hi t#uctu#e to#e the file pointe#called poition pointe#, which 7eep t#ac7 of cu##ent location within the file. ;nopening file fo# #eadKw#ite ope#ation, the file pointe# i et to the ta#t of the file.Iach time we #eadKw#ite a cha#acte#, the poition pointe# advance one cha#acte#. fwe #ead one line of tet at a tep f#om the file, then file pointe# advance to the ta#tof the net line. f the file i opened in append mode, the file pointe# i placed at theve#- end of the file. Bing fee7( ) function we can et the file pointe# to ome othe#place within the file.
A.
+he i/eof( ) function doenYt #etu#n the i/e of the bloc7 of memo#- pointed to b- apointe#. Wh-?
8/11/2019 C-Q & A
http://slidepdf.com/reader/full/c-q-a 13/65
n* +he i/eof( ) ope#ato# doe not 7now that malloc( ) ha been ued to allocate apointe#. i/eof( ) give u the i/e of pointe# itelf. +he#e i no hand- wa- to find outthe i/e of a bloc7 allocated b- malloc( )..
HSTI nd HST;HHZometime while wo#7ing with fa# pointe# we need to b#ea7 a fa# add#e into itegment and offet. n uch ituation we can ue HSTI and HST;HH mac#o.Hollowing p#og#am illut#ate the ue of thee two mac#o.Dinclude Edo.hF
main( ){unigned , o !cha# fa# 4pt# = &5ello8& !
= HSTI ( pt# ) !
o = HST;HH ( pt# ) !p#intf ( &Jn%u %u&, , o ) !'
.
5ow do w#ite a p#og#am to conve#t a t#ing containing numbe# in a headecimalfo#m to it eMuivalent decimal?n* +he following p#og#am demont#ate thi*main( ){cha# t#[ = &0G& !int h, he, i, n !n = 0 ! h = 1 !fo# ( i = 0 ! h == 1 ! i"" ){if ( t#[i F= 202 LL t#[i E= 2Q2 )he = t#[i 202 !ele{if ( t#[i F= 2a2 LL t#[i E= 2f2 )he = t#[i 2a2 " 10 !ele
if ( t#[i F= 22 LL t#[i E= 2H2 )he = t#[i 22 " 10 !eleh = 0 !'if ( h == 1 )n = 1 4 n " he !'p#intf ( &Jn+he decimal eMuivalent of % i %d&,
8/11/2019 C-Q & A
http://slidepdf.com/reader/full/c-q-a 14/65
t#, n ) !'+he output of thi p#og#am would be the decimal eMuivalent of 0G i 11.@.
5ow do w#ite code that #ead the egment #egite# etting?
n* We can ue eg#ead( ) function to #ead egment #egite# etting. +he#e a#efou# egment #egite#code egment, data egment, tac7 egment and et#aegment. ometime when we ue :; and G; e#vice in a p#og#am we need to7now the egment #egite#2 value. n uch a ituation we can ue eg#ead( )function. +he following p#og#am illut#ate the ue of thi function.Dinclude Edo.hFmain( ){t#uct UI !
eg#ead ( L ) !p#intf ( &Jn>* %\ :* %\ * %\ I* %\&,.c,.d, ., .e ) !'Q.
What i envi#onment and how do get envi#onment fo# a pecific ent#-?
n* While wo#7ing in :;, it to#e info#mation in a memo#- #egion calledenvi#onment. n thi #egion we can place configu#ation etting uch a commandpath, -tem p#ompt, etc. ometime in a p#og#am we need to acce theinfo#mation contained in envi#onment. +he function getenv( ) can be ued when wewant to acce envi#onment fo# a pecific ent#-. Hollowing p#og#am demont#ate theue of thi function.Dinclude Etdio.hFDinclude Etdlib.hF
main( ){cha# 4path = 6B<< !
path = getenv ( &S+5& ) !if ( 4path 8= 6B<< )
p#intf ( &JnSath* %&, path ) !elep#intf ( &JnSath i not et& ) !'
30.
8/11/2019 C-Q & A
http://slidepdf.com/reader/full/c-q-a 15/65
5ow do dipla- cu##ent date in the fo#mat given below?
atu#da- ;ctobe# 1, 00n* Hollowing p#og#am illut#ate how we can dipla- date in above given fo#mat.
Dinclude Etdio.hF
Dinclude Etime.hF
main( ){t#uct tm 4cu#time !timeTt dtime !
cha# t#[30 !
time ( Ldtime ) !cu#time = localtime ( Ldtime ) !t#ftime ( t#, 30, &% %G %d, %V&, cu#time ) !
p#intf ( &Jn%&, t# ) !'5e#e we have called time( ) function which #etu#n cu##ent time. +hi time i#etu#ned in te#m of econd, elaped ince 00*00*00 P+, ]anua#- 1, 1Q0. +oet#act the wee7 da-, da- of month, etc. f#om thi value we need to b#ea7 down thevalue to a tm t#uctu#e. +hi i done b- the function localtime( ). +hen we havecalled t#ftime( ) function to fo#mat the time and to#e it in a t#ing t#.
31.
f we have decla#ed an a##a- a global in one file and we a#e uing it in anothe# file
then wh- doen2t the i/eof ope#ato# wo#7 on an ete#n a##a-?
n* n ete#n a##a- i of incomplete t-pe a it doe not contain the i/e. 5ence wecannot ue i/eof ope#ato#, a it cannot get the i/e of the a##a- decla#ed in anothe#file. +o #eolve thi ue an- of one the following two olution*1. n the ame file decla#e one mo#e va#iable that hold the i/e of a##a-. Ho#eample,
a##a-.c
int a##[A !int a##/ = i/eof ( a## ) !
m-p#og.c
ete#n int a##[ !ete#n int a##/ !. :efine a mac#o which can be ued in an a##a-decla#ation. Ho# eample,
m-heade#.h
8/11/2019 C-Q & A
http://slidepdf.com/reader/full/c-q-a 16/65
Ddefine ^ A
a##a-.c
Dinclude &m-heade#.h&int a##[^ !
m-p#og.c
Dinclude &m-heade#.h&ete#n int a##[^ !
3.
5ow do w#ite p#intf( ) o that the width of a field can be pecified at #untime?
n* +hi i hown in following code nippet.
main( ){int w, no !p#intf ( &Inte# numbe# and the width fo# thenumbe# field*& ) !canf ( &%d%d&, Lno, Lw ) !p#intf ( &%4d&, w, no ) !'5e#e, an 242 in the fo#mat pecifie# in p#intf( ) indicate that an int value f#om the
a#gument lit hould be ued fo# the field width.33.
5ow to find the #ow and column dimenion of a given : a##a-?
n* Wheneve# we initiali/e a : a##a- at the ame place whe#e it ha beendecla#ed, it i not necea#- to mention the #ow dimenion of an a##a-. +he #ow andcolumn dimenion of uch an a##a- can be dete#mined p#og#ammaticall- a hownin following p#og#am.
void main( ){int a[[3 = { 0, 1, ,Q,, @,, A, 99,3, 11,1A ' !
8/11/2019 C-Q & A
http://slidepdf.com/reader/full/c-q-a 17/65
int c = i/eof ( a[0 ) K i/eof ( int ) !int # = ( i/eof ( a ) K i/eof ( int ) ) K c !int i, O !
p#intf ( &JnUow* %dJn>ol* %dJn&, #, c ) !fo# ( i = 0 ! i E # ! i"" )
{fo# ( O = 0 ! O E c ! O"" )p#intf ( &%d &, a[i[O ) !p#intf ( &Jn& ) !''39.
+he acce( ) function...
+he acce( ) function chec7 fo# the eitence of a file and alo dete#mine whethe#it can be #ead, w#itten to o# eecuted. +hi function ta7e two a#gument thefilename and an intege# indicating the acce mode. +he value , 9, , and 1 chec7fo# #eadKw#ite, #ead, w#ite and eecute pe#miion of a given file, whe#ea value 0chec7 whethe# the file eit o# not. Hollowing p#og#am demont#ate how we canue acce( ) function to chec7 if a given file eit.
Dinclude Eio.hF
main( ){cha# fname[ !
p#intf ( &JnInte# name of file to open& ) !get ( fname ) !
if ( acce ( fname, 0 ) 8= 0 ){p#intf ( &JnHile doe not eit.& ) !#etu#n !''
3A.
5ow do conve#t a floatingpoint numbe# to a t#ing?
n* Be function gcvt( ) to conve#t a floatingpoint numbe# to a t#ing. Hollowingp#og#am demont#ate the ue of thi function.Dinclude Etdlib.hF
8/11/2019 C-Q & A
http://slidepdf.com/reader/full/c-q-a 18/65
main( ){cha# t#[A !float no !int dg = A ! K4 ignificant digit 4K
no = 19.31 !gcvt ( no, dg, t# ) !p#intf ( &t#ing* %Jn&, t# ) !'
3.What i a tac7 ?
n* +he tac7 i a #egion of memo#- within which ou# p#og#am tempo#a#il- to#edata a the- eecute. Ho# eample, when a p#og#am pae pa#amete# to function,> place the pa#amete# on the tac7. When the function complete, > #emove theitem f#om the tac7. imila#l-, when a function decla#e local va#iable, > to#e the
va#iable2 value on the tac7 du#ing the function2 eecution. :epending on thep#og#am2 ue of function and pa#amete#, the amount of tac7 pace that ap#og#am #eMui#e will diffe#.3.llocating memo#- fo# a 3: a##a-
Dinclude &alloc.h&Ddefine P\\ 3Ddefine P\V 9Ddefine P\^ Amain( )
{int 444p, i, O, 7 !p = ( int 444 ) malloc ( P\\ 4 i/eof ( int 44 ) ) !fo# ( i = 0 ! i E P\\ ! i"" ){p[i = ( int 44 ) malloc ( P\V 4 i/eof ( int 4 ) ) !fo# ( O = 0 ! O E P\V ! O"" )p[i[O = ( int 4 ) malloc ( P\^ 4 i/eof ( int ) ) !'fo# ( 7 = 0 ! 7 E P\^ ! 7"" ){fo# ( i = 0 ! i E P\\ ! i"" ){fo# ( O = 0 ! O E P\V ! O"" ){p[i[O[7 = i " O " 7 !p#intf ( &%d &, p[i[O[7 ) !'p#intf ( &Jn& ) !'p#intf ( &JnJn& ) !'
8/11/2019 C-Q & A
http://slidepdf.com/reader/full/c-q-a 19/65
':ata t#uctu#e5ow to ditinguih between a bina#- t#ee and a t#ee?
n* node in a t#ee can have an- numbe# of b#anche. While a bina#- t#ee i a t#eet#uctu#e in which an- node can have at mot two b#anche. Ho# bina#- t#ee we
ditinguih between the ubt#ee on the left and ubt#ee on the #ight, whe#ea fo#t#ee the o#de# of the ubt#ee i i##elevant.>onide# the following figu#e...
+hi above figu#e how two bina#- t#ee, but thee bina#- t#ee a#e diffe#ent. +hefi#t ha an empt- #ight ubt#ee while the econd ha an empt- left ubt#ee. f theabove a#e #ega#ded a t#ee (not the bina#- t#ee), then the- a#e ame depite thefact that the- a#e d#awn diffe#entl-. lo, an empt- bina#- t#ee can eit, but the#e ino t#ee having /e#o [email protected] do ue the function ldep( ) in a p#og#am?
n* +he math function ldep( ) i ued while olving the comple mathematicaleMuation. +hi function ta7e two a#gument, a double value and an int #epectivel-.+he o#de# in which ldep( ) function pe#fo#m calculation i ( n 4 pow ( , ep ) )whe#e n i the double value and ep i the intege#. +he following p#og#amdemont#ate the ue of thi function.
Dinclude Etdio.hFDinclude Emath.hF
void main( ){double an !double n = 9 !
an = ldep ( n, ) !p#intf ( &Jn+he ldep value i * %lfJn&, an ) !'5e#e, ldep( ) function would get epanded a ( 9 4 4 ), and the output would bethe ldep value i * 1.000000
3Q.
>an we get the mantia and eponent fo#m of a given numbe#?
n* +he function f#ep( ) plit the given numbe# into a mantia and eponentfo#m. +he function ta7e two a#gument, the numbe# to be conve#ted a a doublevalue and an int to to#e the eponent fo#m. +he function #etu#n the mantia pa#ta a double value. Hollowing eample demont#ate the ue of thi function.
Dinclude Emath.hFDinclude Etdio.hF
8/11/2019 C-Q & A
http://slidepdf.com/reader/full/c-q-a 20/65
void main( ){double mantia, numbe# !int eponent !
numbe# = @.0 !
mantia = f#ep ( numbe#, Leponent ) !
p#intf ( &+he numbe# %lf i &, numbe# ) !p#intf ( &%lf time two to the &, mantia ) !p#intf ( &powe# of %dJn&, eponent ) !
#etu#n 0 !'90.
5ow do w#ite code that eecute ce#tain function onl- at p#og#am te#mination?n* Be ateit( ) function a hown in following p#og#am.
Dinclude Etdlib.hFmain( ){int ch !void fun ( void ) !ateit ( fun ) ! KK code'void fun( void ){p#intf ( &Jn+e#minate p#og#am......& ) !getch( ) !'91.What a#e memo#- model?n* +he compile# ue a memo#- model to dete#mine how much memo#- iallocated to the p#og#am. +he S> divide memo#- into bloc7 called egment of i/e9 NG. Buall-, p#og#am ue one egment fo# code and a econd egment fo# data. memo#- model define the numbe# of egment the compile# can ue fo# each. ti impo#tant to 7now which memo#- model can be ued fo# a p#og#am. f we ue
w#ong memo#- model, the p#og#am might not have enough memo#- to eecute. +hep#oblem can be olved uing la#ge# memo#- model. 5oweve#, la#ge# the memo#-model, lowe# i -ou# p#og#am eecution. o we mut chooe the mallet memo#-model that atifie ou# p#og#am need. Pot of the compile# uppo#t memo#-model li7e tin-, mall, medium, compact, la#ge and huge.
9.5ow doe > compile# to#e element in a multidimenional a##a-?
8/11/2019 C-Q & A
http://slidepdf.com/reader/full/c-q-a 21/65
n* +he compile# map multidimenional a##a- in two wa-Uow maOo# o#de# and>olumn o#de#. When the compile# place element in column of an a##a- fi#t then iti called columnmaOo# o#de#. When the compile# place element in #ow of an a##a-fi#t then it i called #owmaOo# o#de#. > compile# to#e multidimenional a##a- in#owmaOo# o#de#. Ho# eample, if the#e i a multidimenional a##a- a[[3, thenacco#ding #owmaOo# o#de#, the element would get to#ed in memo#- following
o#de#*a[0[0, a[0[1, a[0[, a[1[0, a[1[1, a[1[93.
f the #eult of an Tep#eion ha to be to#ed to one of two va#iable, depending ona condition, can we ue conditional ope#ato# a hown below?
( ( i E 10 ) ? O * 7 ) = l 4 " p !n* 6o8 +he above tatement i invalid. We cannot ue the conditional ope#ato# inthi fahion. +he conditional ope#ato# li7e mot ope#ato#, -ield a value, and wecannot aign the value of an Tep#eion to a value. 5oweve#, we can ueconditional ope#ato# a hown in following code nippet.
main( ){int i, O, 7, l !i = A ! O = 10 ! 7 = 1, l = 1 !4 ( ( i E 10 ) ? LO * L7 ) = l 4 " 19 !p#intf ( &i = %d O = %d 7 = %d l = %d&, i, O, 7, l ) !'
+he output of the above p#og#am would be a given below*i = A O = 1 7 = 1 l = 1
99.
5ow can find the da- of the wee7 of a given date?
n* +he following code nippet how how to get the da- of wee7 f#om the givendate.
da-ofwee7 ( int --, int mm, int dd ){ K4Ponda- = 1 and unda- = 0 4K
K4 month numbe# F= 1 and E= 1, -- F 1A o# o 4Ktatic int a##[ = { 0, 3, , A, 0, 3, A, 1, 9, , , 9 ' !-- = -- mm E 3 !#etu#n ( -- " -- K 9 -- K 100 " -- K 900 " a##[ mm 1 " dd ) % !'
void main( ){p#intf ( &JnJnJn:a- of wee7 * %d &, da-ofwee7 ( 00, A, 1@ ) ) !
8/11/2019 C-Q & A
http://slidepdf.com/reader/full/c-q-a 22/65
'
9A.
What2 the diffe#ence between thee two decla#ation?
t#uct t#1 { ... ' !t-pedef t#uct { ... ' t# !n * +he fi#t fo#m decla#e a t#uctu#e tag whe#ea the econd decla#e a t-pedef.+he main diffe#ence i that the econd decla#ation i of a lightl- mo#e abt#act t-pe it ue# don2t necea#il- 7now that it i a t#uctu#e, and the 7e-wo#d t#uct i notued when decla#ing intance of it.
9.
5ow do p#int the content of envi#onment va#iable?
n*. +he following p#og#am how how to achieve thi*main( int a#gc, cha# 4a#gv[ , cha# 4env[ ){int i = 0 !cl#c#( ) !while ( env[ i )
p#intf ( &Jn%&, env[ i"" ) !'
main( ) ha the thi#d command line a#gument env, which i an a##a- of pointe# tothe t#ing. Iach pointe# point to an envi#onment va#iable f#om the lit ofenvi#onment va#iable.9.div( )...
+he function div( ) divide two intege# and #etu#n the Muotient and #emainde#. +hifunction ta7e two intege# value a a#gument! divide fi#t intege# with the econdone and #etu#n the anwe# of diviion of t-pe divTt. +he data t-pe divTt i at#uctu#e that contain two long int, namel- Muot and #em, which to#e Muotient and#emainde# of diviion #epectivel-. +he following eample how the ue of div( )function.
Dinclude Etdlib.hFvoid main( )
8/11/2019 C-Q & A
http://slidepdf.com/reader/full/c-q-a 23/65
{divTt #e !
#e = div ( 3, A ) !p#intf ( &Jn+he Muotient = %d and #emainde# = %d &, #e.Muot, #e.#em ) !'
[email protected] would the econd and the thi#d p#intf( ) output the following p#og#am?
main( ){cha# 4t#[ = {&ood Po#ning&&ood Ivening&&ood fte#noon&' !p#intf ( &JnHi#t t#ing = %&, t#[0 ) !
p#intf ( &Jnecond t#ing = %&, t#[1 ) !p#intf ( &Jn+hi#d t#ing = %&, t#[ ) !'n* Ho# the above given p#og#am, we epect the output a ood Ivening and oodfte#noon, fo# the econd and thi#d p#intf( ). 5oweve#, the output would be a hownbelow.
Hi#t t#ing = ood Po#ningood Iveningood fte#noonecond t#ing = ( null )+hi#d t#ing =
What i miing in the above given code nippet i a comma epa#ato# which houldepa#ate the t#ing ood Po#ning, ood Ivening and ood fte#noon. ;n addingcomma, we would get the output a hown below.
Hi#t t#ing = ood Po#ningecond t#ing = ood Ivening+hi#d t#ing = ood fte#noon9Q.
5ow do ue canf( ) to #ead the date in the fo#m 2ddmm--2 ?n* +he#e a#e two wa- to #ead the date in the fo#m of 2ddmm--2 one poiblewa- i...
int dd, mm, -- !cha# ch ! K4 fo# cha# 22 4Kp#intf ( &JnInte# the date in the fo#m of ddmm-- * & ) !canf( &%d%c%d%c%d&, Ldd, Lch, Lmm, Lch, L-- ) !
nd anothe# bet wa- i to ue upp#eion cha#acte# 4 a...
8/11/2019 C-Q & A
http://slidepdf.com/reader/full/c-q-a 24/65
int dd, mm, -- !canf( &%d%4c%d%4c%d&, Ldd, Lmm, L-- ) !
+he upp#eion cha#acte# 4 upp#ee the input #ead f#om the tanda#d inputbuffe# fo# the aigned cont#ol cha#acte#.
A0.
5ow do p#int a floatingpoint numbe# with highe# p#eciion a- 3.39A@39 withonl- p#eciion up to two decimal place?
n* +hi can be achieved th#ough the ue of upp#eion cha# 242 in the fo#matt#ing of p#intf( ) a hown in the following p#og#am.main( ){int i = !float f = 3.39A@39 !
p#intf ( &%.4f&, i, f ) !'+he output of the above p#og#am would be 3.3A.
A1.#e the ep#eion 4pt#"" and ""4pt# ame?
n* 6o. 4pt#"" inc#ement the pointe# and not the value pointed b- it, whe#ea ""4pt# inc#ement the value being pointed to b- pt#.
A.t#pb#7( )
+he function t#pb#7( ) ta7e two t#ing a pa#amete#. t can the fi#t t#ing, tofind, the fi#t occu##ence of an- cha#acte# appea#ing in the econd t#ing. +hefunction #etu#n a pointe# to the fi#t occu##ence of the cha#acte# it found in the fi#tt#ing. +he following p#og#am demont#ate the ue of t#ing function t#pb#7( ).
Dinclude Et#ing.hFmain( ){cha# 4t#1 = &5ello8& !cha# 4t# = &Gette#& !cha# 4p !p = t#pb#7 ( t#1, t# ) !
if ( p )p#intf ( &+he fi#t cha#acte# found in t#1 i %c&, 4p ) !ele
8/11/2019 C-Q & A
http://slidepdf.com/reader/full/c-q-a 25/65
p#intf ( &+he cha#acte# not found& ) !'+he output of the above p#og#am would be the fi#t cha#acte# found in t#1 i e
A3.
>an we conve#t an unigned long intege# value to a t#ing?
n* +he function ultoa( ) can be ued to conve#t an unigned long intege# value to at#ing. +hi function ta7e th#ee a#gument, fi#t the value that i to be conve#ted,econd the bae add#e of the buffe# in which the conve#ted numbe# ha to beto#ed (with a t#ing te#minating null cha#acte# 2J02) and the lat a#gument pecifiethe bae to be ued in conve#ting the value. Hollowing eample demont#ate the ueof thi function.
Dinclude Etdlib.hFvoid main( ){
unigned long ul = 339A31< !cha# t#[A !
ultoa ( ul, t#, 10 ) !p#intf ( &t# = % unigned long = %luJn&, t#, ul ) !'A9.ceil( ) and floo#( )
+he math function ceil( ) ta7e a double value a an a#gument. +hi function find
the mallet poible intege# to which the given numbe# can be #ounded up.imila#l-, floo#( ) being a math function, ta7e a double value a an a#gument and#etu#n the la#get poible intege# to which the given double value can be #oundeddown. +he following p#og#am demont#ate the ue of both the function.
Dinclude Emath.hFvoid main( ){double no = 193.31 !double down, up !
down = floo# ( no ) !up = ceil ( no ) !
p#intf ( &+he o#iginal numbe# %.AlfJn&, no ) !p#intf ( &+he numbe# #ounded down %.AlfJn&, down ) !p#intf ( &+he numbe# #ounded up %.AlfJn&, up ) !'
+he output of thi p#og#am would be,+he o#iginal numbe# 193.31
8/11/2019 C-Q & A
http://slidepdf.com/reader/full/c-q-a 26/65
+he numbe# #ounded down 193.00000+he numbe# #ounded up [email protected].
5ow do ue function ecvt( ) in a p#og#am?
n* +he function ecvt( ) conve#t a floatingpoint value to a null te#minated t#ing.+hi function ta7e fou# a#gument, uch a, the value to be conve#ted to t#ing, thenumbe# of digit to be conve#ted to t#ing, and two intege# pointe#. +he twointege#pointe# to#e the poition of the decimal point (#elative to the t#ing) and the ign of the numbe#, #epectivel-. f the value in a va#iable, ued to to#e ign i 0, then thenumbe# i poitive and, if it i non/e#o, then the numbe# i negative. +he function#etu#n a pointe# to the t#ing containing digit. Hollowing p#og#am demont#ate theue of thi function.
Dinclude Etdlib.hF
main( ){cha# 4t# !double val !int dec, ign !int ndig = 9 !
val = !t# = ecvt ( val, ndig, Ldec, Lign ) !p#intf ( &t#ing = % dec = %d ign = %dJn&, t#, dec, ign ) !
val = 39A. !ndig = @ !t# = ecvt ( val, ndig, Ldec, Lign ) !p#intf ( &t#ing = % dec = %d ign = %dJn&, t#, dec, ign ) !
KK numbe# with a cientific notationval = 3.A91eA !ndig = A !t# = ecvt ( val, ndig, Ldec, Lign ) !p#intf ( &t#ing = % dec = %d ign = %dJn&, t#, dec, ign ) !'
+he output of thi p#og#am would be
t#ing = 00 dec = ign = 0t#ing = 39A000 dec = 3 ign = 1t#ing = 3A9 dec = ign = 0
A.
8/11/2019 C-Q & A
http://slidepdf.com/reader/full/c-q-a 27/65
5ow to #un :U command p#og#ammaticall-?
n* We can ue the -tem( ) function to eecute the :U command along with itoption. Hollowing p#og#am how how thi can be achieved*
KK m-di#.c
main ( int a#gc, cha# 4a#gv[ ){cha# t#[30 !
if ( a#gc E )eit ( 0 ) !
p#intf ( t#, &di# % %&, a#gv[1, a#gv[ ) !-tem ( t# ) !'
f we #un the eecutable file of thi p#og#am at command p#ompt paing the
command line a#gument a follow*
F m-di# abc.c K
+hi will ea#ch the file 2abc.c2 in the cu##ent di#ecto#-.A.
uppoe have a t#uctu#e having field name, age, ala#- and have paed add#eof age to a function fun( ). 5ow can acce the othe# membe# of the t#uctu#e uingthe add#e of age?
n*t#uct emp{cha# name[0 !int age !float ala#- !' !main( ){t#uct emp e !p#intf ( &JnInte# name* & ) !canf ( &%&, e.name ) !p#intf ( &JnInte# age* & ) !
canf ( &%d&, Le.age ) !p#intf ( &JnInte# ala#-* & ) !canf ( &%f&, Le.ala#- ) !fun ( Le.age ) !'fun ( int 4p ){t#uct emp 4M !int offet !
8/11/2019 C-Q & A
http://slidepdf.com/reader/full/c-q-a 28/65
offet = ( cha# 4 ) ( L ( ( t#uct emp 4 ) 0 ) F age ) ( cha# 4 ) ( (t#uct emp4 ) 0 ) !M = ( t#uct emp 4 ) ( ( cha# 4 ) p offet ) !p#intf ( &Jnname* %&, M F name ) !p#intf ( &Jnage* %d&, M F age ) !p#intf ( &Jnala#-* %f&, M F ala#- ) !
'
A@.
5ow to #et#ict the p#og#am2 output to a pecific c#een #egion?
n* > function window( ) can be ued to #et#ict the c#een output to a pecific#egion. +he window( ) function define a tetmode window. +he pa#amete# paedto thi function define the uppe#left and lowe##ight co#ne# of the #egion withinwhich -ou want the output. n the following p#og#am, the t#ing 25ello82 get p#intedwithin the pecified #egion. +o p#int the t#ing we mut ue cp#intf( ) function whichp#int di#ectl- on the tetmode window.
Dinclude Econio.hFmain( ){int i, O !
window ( 0, @, 0, 1 ) !fo# ( i = 0 ! i E @ ! i"" )fo# ( O = 0 ! O E 10 ! O"" )cp#intf ( &5ello8& ) !'
AQ.
ometime -ou need to p#ompt the ue# fo# a pawo#d. When the ue# t-pe in thepawo#d, the cha#acte# the ue# ente# hould not appea# on the c#een. tanda#d lib#a#- function getpa( ) can be ued to pe#fo#m uch function. Paimumnumbe# of cha#acte# that can be ente#ed a pawo#d i @.
main( ){cha# 4pwd !
pwd = getpa ( &Inte# Sawo#d& ) !
if ( t#cmp ( pwd, &o#gcit-& ) )p#intf ( &JnSawo#d % i inco##ect&, pwd ) !elep#intf ( &Jn>o##ect Sawo#d& ) !'0.
8/11/2019 C-Q & A
http://slidepdf.com/reader/full/c-q-a 29/65
5ow to obtain the cu##ent d#ive th#ough > ?
n* We can ue the function Tgetd#ive( ) to obtain the cu##ent d#ive. +he Tgetd#ive() function ue :; function 0\1Q to get the cu##ent d#ive numbe#
Dinclude Edi#ect.hF
main( ){int di7 !di7 = Tgetd#ive( ) " 22 1 !p#intf ( &+he cu##ent d#ive i* %cJn&, di7 ) !'1.
5ow come the output fo# both the p#og#am i diffe#ent when the logic i ame?
main( ){int i, O !
fo# ( i = 1, O = 1 ! i E= A, O E= 100 ! i"", O"" ){goto- ( 1, 1, ) !p#intf ( &%d %d&, i, O ) !''
main( ){
int i, O !
fo# ( i =1, O = 1! O E= 100, i E= A! i"", O"" ){goto- ( 1, 1 ) !p#intf ( &%d %d&, i, O ) !''
;utput F A AIven if logic of both the p#og#am i ame the output of the fi#t p#og#am come outto be 100, 100, but of the econd p#og#am it i A, A. +he comma ope#ato# pla- avital #ole inide the fo# loop. t alwa- conide# the value of the latet va#iable. o,
at the time of teting the condition in fo# loop, the value of O will be conide#ed in thefi#t p#og#am and value of i in the econd..>an we get the and - coo#dinate of the cu##ent cu#o# poition ?
n * +he function whe#e( ) and whe#e-( ) #etu#n the coo#dinate and -coo#dinate of the cu##ent cu#o# poition #epectivel-. Goth the function #etu#n an
8/11/2019 C-Q & A
http://slidepdf.com/reader/full/c-q-a 30/65
intege# value. +he value #etu#ned b- whe#e( ) i the ho#i/ontal poition of cu#o#and the value #etu#ned b- whe#e-( ) i the ve#tical poition of the cu#o#. Hollowingp#og#am how how to ue the whe#e( ) and whe#e-( ) function.
Dinclude Econio.hFmain( )
{p#intf ( &]utJn +oJn +etJn Whe#eJn the cu#o#Jn goe& ) !
p#intf ( &>u##ent location i \* %d V* %dJn&, whe#e( ), whe#e-( ) ) !'
3.5ow do p#og#ammaticall- delete line in the tet window?
n* While w#iting p#og#am that pe#fo#m c#eenbaed K;, -ou ma- want todeletethe cu##ent line2 content, moving one line up, all of the output that follow. n uchcae a function called delline( ) can be ued. Hollowing code nippet illut#ate theue of function delline( ).
Dinclude Econio.hFmain( ){int i !cl#c#( ) !
fo# ( i = 0! i E= 3! i"" )p#intf ( &<ine %dJ#Jn&, i ) !
p#intf ( &S#e a 7e- to continue * & ) !getch( ) !
goto- ( , ) !
fo# ( i = ! i E= 1! i"" )delline( ) !
getch( ) !'9.
5ow do get the time elaped between two function call ?
n* +he function difftime( ) find the diffe#ence between two time. t calculate theelaped time in econd and #etu#n the diffe#ence between two time a a doublevalue.
Dinclude Etime.hFDinclude Etdio.hFDinclude Edo.hF
8/11/2019 C-Q & A
http://slidepdf.com/reader/full/c-q-a 31/65
main( ){int a[ = { , 39, A, @, 11, 33, , 11, 9A, Q, ' !int !timeTt t1, t ! KK timeTt define the value ued fo# time function
= i/eof ( a ) K !t1 = time ( 6B<< ) !elTo#t ( a, ) ! KK o#t a##a- b- election o#tbubTo#t ( a, ) ! KK o#t a##a- b- bubble o#t methodt = time ( 6B<< ) !p#intf ( &Jn+he diffe#ence between two function call i %f&, difftime (t, t1 ) ) !'
n the above p#og#am we have called difftime( ) function that #etu#n the timeelaped f#om t1 to t.
A.
5ow do ue wab( ) in m- p#og#am ?
n* +he function wab( ) wap the adOacent b-te of memo#-. t copie the b-tef#om ou#ce t#ing to the ta#get t#ing, p#ovided that the numbe# of cha#acte# in theou#ce t#ing i even. While cop-ing, it wap the b-te which a#e then aigned tothe ta#get t#ing.
Dinclude Etdlib.hFDinclude Etdio.hFDinclude Et#ing.hF
main ( ){cha# 4t#1 = &h eell nial not eh e a ohe# & !cha# 4t# !cl#c#( ) !wab ( t#1, t#, t#len ( t#1 ) ) !p#intf ( &+he ta#get t#ing i * %Jn&, t# ) ! KK output he ellnail on the ea ho#egetch( ) !'
.
+u#bo > p#ovide va#iou command line compile# option which we can ue th#ough+>>. +he compile# option include * dipla-ing pecific wa#ning meage, gene#ating@0@ ha#dwa#e int#uction, uing a filename fo# gene#ating aembl- code, etc.ntead of compile# option being eecuted at command line we can ue theecompile# option in ou# p#og#am. +hi can be achieved uing Dp#agma option. We
8/11/2019 C-Q & A
http://slidepdf.com/reader/full/c-q-a 32/65
can ue va#iou flag with Dp#agma option to ue the compile# option. ll theeflag a#e available in tu#bo >2 online help..
have an a##a- decla#ed in file 2H1.>2 a,int a[ = { 1, , 3, 9, A, ' !and ued in the file 2H.>2 a,ete#n int a[ !
n the file H.>, wh- i/eof doen2t wo#7 on the a##a- a[ ?
n* n ete#n a##a- of unpecified i/e i an incomplete t-pe. Vou cannot appl-i/eof to it, becaue i/eof ope#ate du#ing compile time and it i unable to lea#n thei/e of an a##a- that i defined in anothe# file. Vou have th#ee wa- to #eolve thip#oblem*1. n file 2H1.>2 define a,
int a[ = { 1, , 3, 9, A, ' !int i/eTa = i/eof ( a ) !and in file H.> decla#e a,ete#n int a[ !ete#n int i/eTa !
. n file 2H1.52 define,
Ddefine UUT^ n file H1.> decla#e a,Dinclude &H1.5&int a[ UUT^ !and in file H.> decla#e a,
Dinclude &H1.5&ete#n int a[ UUT^ !3. n file 2H1.>2 define a,int a[ = { 1, , 3, 9, A, , 1 ' !
and in file 2H.>2 decla#e a,
ete#n int a[ !5e#e the element 1 i ued a a entinel value, o the code canunde#tand the end without an- eplicit i/e.
@.
5ow to delete a line f#om tet dipla-ed on the c#een?
n* ometime, peciall- when we a#e c#eating a tet edito# li7e p#og#am we ma-wih to allow ue# to delete a line. We can do o b- uing two function namel-cl#eol( ) and delline( ). +he cl#eol( ) function delete the line f#om the cu##ent cu#o#poition to the end of line. +he delline() function delete the enti#e line at the cu##entcu#o# poition andmove up the following line. Hollowing p#og#am how how to ue thee function.
8/11/2019 C-Q & A
http://slidepdf.com/reader/full/c-q-a 33/65
Dinclude Econio.hF
main( ){int i !
fo# ( i = 1 ! i E= 0 ! i"" )p#intf ( &+hi i <ine %dJn&, i ) !
getch( ) !goto- ( 1, ) !cl#eol( ) !
getch( ) !goto- ( 1, 1 ) !delline( ) !
getch( ) !
'Q.5ow do p#og#ammaticall- ine#t line in the tet window?
n* We can ine#t a blan7 line in the tet window uing the inline( ) function. +hifunction ine#t line at cu##ent cu#o# poition. While doing o, it hift down the linethat a#e below the newl- ine#ted line.
Dinclude Econio.hFvoid main( )
{p#intf ( &+he little nail wa lowl- moving up. he wantedJ#Jn& ) !p#intf ( &to #each the top of the t#ee. t wa chill-J#Jn& ) !p#intf ( &winte# eaon. Pot of the animal we#e #eting inJ#Jn& ) !p#intf ( &thei# net a the#e wa a heav- now fall.J#Jn& ) !p#intf ( &J#JnS#e an- 7e- to continue*& ) !
goto- ( 10, ) !getch( ) !inline( ) !getch( ) !'
0.
What will be the output of the following p#og#am?
main( ){unigned int num !
8/11/2019 C-Q & A
http://slidepdf.com/reader/full/c-q-a 34/65
int i !
p#intf ( &JnInte# an- numbe#& ) !canf ( &%u&, Lnum ) !
fo# ( i = 0 ! i E 1 ! i"" )
p#intf ( &%d&, ( num EE i L 1 EE 1A ) ? 1 * 0 ) !'
n* +he output of thi p#og#am i the bina#- eMuivalent of the given numbe#. Wehave ued bitwie ope#ato# to get the bina#- numbe#.1.#aphic
Guilding Poue >u#o#...
n tet mode the moue cu#o# appea# a a bloc7, whe#ea in g#aphic mode itappea# a an a##ow. f we wih we can change the g#aphic cu#o# to an- othe#hape the wa- Window doe. +he moue cu#o# in g#aphic mode occupie a 1 b-1 piel bo. G- highlighting o# dehighlighting ome of the piel in thi bo we canget the dei#ed hape. Ho# eample, the following bitpatte#n can be ued togene#ate the cu#o# which loo7 li7e an hou#gla.1111111111111111 00000000000000001000000000000001 00000000000000001111111111111111 00000000000000001000000000000001 00000000000000000100000000000010 10000000000000010010000000000100 11000000000000110000100000010000 1111000000001111
0000001001000000 11111100001111110000001001000000 11111100001111110000100000010000 11110000000011110010000000000100 11000000000000110100000000000010 10000000000000011000000000000001 00000000000000001111111111111111 00000000000000001000000000000001 00000000000000001111111111111111 0000000000000000Poue pointe# bitmap c#een Pa7 the one2 in the moue pointe# bitmap indicatethat the piel would be d#awn whe#ea the /e#o indicate that the piel would tande#aed. t i impo#tant to note that the moue pointe# bit patte#n i 3 b-te long.5oweve#, while actuall- w#iting a p#og#am to change the pointe# hape we need a 9b-te bitmap. +hi p#oviion i made to enu#e that when the cu#o# #eache apoition on the c#een whe#e omething i al#ead- w#itten o# d#awn onl- that po#tionhould get ove#w#itten which i to be occupied b- the moue cu#o#. ;f the 9 b-tethe fi#t 3 b-te contain a bit ma7 which i fi#t 6:ed with the c#een image, andthen the econd 3 b-te bit ma7 i \;Ued with the c#een image.
+he following p#og#am change the moue cu#o# in g#aphic mode to #eemble anhou# gla.
8/11/2019 C-Q & A
http://slidepdf.com/reader/full/c-q-a 35/65
D include &g#aphic.h&D include &do.h&
union UI i, o !t#uct UI !
int cu#o#[3 ={ K4 5ou#gla c#een ma7 4K00000, 00000, 00000, 00000,0@001, 0c003, 0f00f, 0fc3f,0fc3f, 0f00f, 0c003, 0@001,00000, 00000, 00000, 00000, K4 +he moue pointe# bitmap 4K0ffff, 0@001, 0ffff, 0@001,0900, 0009, 0100@, 0090,0090, 00@10, 0009, 0900,0@001, 0ffff, 0@001, 0ffff,
' !main( ){int gd = :I+I>+, gm !initg#aph ( Lgd, Lgm, &c*JJtcJJbgi& ) !if ( initmoue( ) == 1 ){cloeg#aph( ) !p#intf ( &Jn Poue not intalled8& ) !eit( ) !'goto- ( 10, 1 ) ! p#intf ( &S#e an- 7e- to eit...& ) !changecu#o# ( cu#o# ) ! howmouept#( ) !
getch( ) !'
initmoue( ){i..a = 0 ! int@ ( 033, Li, Lo ) !#etu#n ( o..a == 0 ? 1 * 0 ) !'howmouept#( ){i..a = 1 ! int@ ( 033, Li, Lo ) !'
changecu#o# ( int 4hape ){i..a = Q ! K4 e#vice numbe# 4Ki..b = 0 ! K4 actual cu#o# poition f#om left 4Ki..c = 0 ! K4 actual cu#o# poition f#om top 4Ki..d = ( unigned ) hape ! K4 offet add#e of pointe# image4Keg#ead ( L ) !.e = .d ! K4 egment add#e of pointe# 4K
8/11/2019 C-Q & A
http://slidepdf.com/reader/full/c-q-a 36/65
int@ ( 033, Li, Li, L ) !'
.
+owe# ;f 5anoi
uppoe the#e a#e th#ee peg labeled , G and >. Hou# di7 a#e placed on peg .+he bottommot di7 i la#get, and di7 go on dec#eaing in i/e with the topmotdi7 being mallet. +he obOective of the game i to move the di7 f#om peg topeg >, uing peg G a an auilia#- peg. +he #ule of the game a#e a follow*
;nl- one di7 ma- be moved at a time, and it mut be the top di7 on one of thepeg. la#ge# di7 hould neve# be placed on the top of a malle# di7. uppoe wea#e to w#ite a p#og#am to p#int out the eMuence in which the di7 hould be moveduch that all di7 on peg a#e finall- t#anfe##ed to peg >. 5e#e it i...main( ){
int n = 9 !move ( n, 22, 2G2, 2>2 ) !'
move ( n, p, ap, ep )int n !cha# p, ap, ep !{if ( n == 1 )p#intf ( &Jn Pove f#om %c to %c &, p, ep ) !ele{move ( n 1, p, ep, ap ) !move ( 1, p, 2 2, ep ) !move ( n 1, ap, p, ep ) !''nd he#e i the output...
Pove f#om to GPove f#om to >Pove f#om G to >Pove f#om to GPove f#om > to Pove f#om > to G
Pove f#om to GPove f#om to >Pove f#om G to >Pove f#om G to Pove f#om > to Pove f#om G to >Pove f#om to GPove f#om to >Pove f#om G to >
8/11/2019 C-Q & A
http://slidepdf.com/reader/full/c-q-a 37/65
+hi p#oblem i the famou +owe# of 5anoi p#oblem, whe#ein th#ee peg a#e to beemplo-ed fo# t#anfe##ing the di7 with the given c#ite#ia. 5e#e2 how we go aboutit. We have th#ee peg* the ta#ting peg, p, the auilia#- peg ap, and the endingpeg, ep, whe#e the di7 mut finall- be. Hi#t, uing the ending peg a an auilia#-o# uppo#ting peg, we t#anfe# all but the lat di7 to ap. 6et the lat di7 i movedf#om p to ep. 6ow, uing p a the uppo#ting peg, all the di7 a#e moved f#om ap
to ep. XY, G and > denote the th#ee peg. +he #ecu#ive function move( ) i calledwith diffe#ent combination of thee peg a ta#ting, auilia#- and ending peg.
3.
What would be the output of following p#og#am?
t#uct -nta{int i !
float g !cha# c !'main( ){p#intf ( & won2t give -ou an- e##o#& ) !'
n* +he above p#og#am would get compiled uccefull- and on eecution it wouldp#int the meage given in p#intf(). What t#i7e in the above code nippet i thet#uctu#e -nta which i decla#ed but not te#minated with the tatement te#minato#,the emicolon. +he compile# would not give an- e##o# meage fo# it, a it aumethat main( ) function have a #etu#n t-pe of t#uct -nta and hence woulduccefull- compile and eecute the p#og#am.9.
5ow to get the memo#- i/e ?
n* >onide# the following p#og#am
Dinclude Etdio.hFDinclude Ebio.hFmain( )
{int memi/e!memi/e = biomemo#-( ) !p#intf ( &UP i/e = %dNJn&,memi/e ) !#etu#n 0 !'
+he function biomemo#- ue G; inte##upt 01 to #etu#n the i/e of memo#-.
8/11/2019 C-Q & A
http://slidepdf.com/reader/full/c-q-a 38/65
A.
Hloat Ho#mat5ow doe > compile# to#e float value ?
n* n >, the float value a#e to#ed in a mantia and eponent fo#m. While w#itinga numbe# we pecif- the eponent pa#t in the fo#m of bae 10. Gut, in cae of >compile#, the eponent fo# float i to#ed in the fo#m of bae . ;bvioul-, becaue,compute# to#e the numbe# in bina#- fo#m. +he > compile# follow an IIItanda#d to to#e a float. +he III fo#mat ep#ee a floatingpoint numbe# in abina#- fo#m 7nown a _no#mali/ed2 fo#m. 6o#mali/ation involve adOuting theeponent o that the &bina#- point& (the bina#- analog of the decimal point) in themantia alwa- lie to the #ight of mot ignificant non/e#o digit. n bina#-#ep#eentation, thi mean that the mot ignificant digit of the mantia i alwa- a1. +hi p#ope#t- of the no#mali/ed #ep#eentation i eploited b- the III fo#matwhen to#ing the mantia. <et u conide# an eample of gene#ating the no#mali/edfo#m of a floating point numbe#. uppoe we want to #ep#eent the decimal numbe#A.3A. t bina#- eMuivalent can be obtained a hown below* R A.3A = 0.A0 0R.A0 = 1.A00 1 R 1.A00 = 1.000 1R R 1 0RR 0 1W#iting #emainde# in #eve#e w#iting whole pa#t in the ame o#de# we get 101o#de# in which the- a#e obtained we get 011 thu the bina#- eMuivalent of A.3A
would be 101.011. +he no#mali/ed fo#m of thi bina#- numbe# i obtained b-adOuting the eponent until the decimal point i to the #ight of mot ignificant 1. nthi cae the #eult i 1.01011 . +he III fo#mat fo# floating point to#age ue aign bit, a mantia and an eponent fo# #ep#eenting the powe# of . +he ign bitdenote the ign of the numbe#* a 0 #ep#eent a poitive value and a 1 denote anegative value. +he mantia i #ep#eented in bina#-. >onve#ting the floatingpointnumbe# to it no#mali/ed fo#m #eult in a mantia whoe mot ignificant digit ialwa- 1. +he III fo#mat ta7e advantage of thi b- not to#ing thi bit at all. +heeponent i an intege# to#ed in unigned bina#- fo#mat afte# adding a poitiveintege# bia. +hi enu#e that the to#ed eponent i alwa- poitive. +he value ofthe bia i 1 fo# float and 103 fo# double. +hu, 1.01011 i #ep#eented ahown below*
R 0 R 100 0000 1 R 010 1100 0000 0000 0000 0000 R ign bit eponent mantia to#ed in no#mali/ed fo#m obtained afte# adding a bia1 to eponent
:ata t#uctu#e
Which i the bet o#ting method?
8/11/2019 C-Q & A
http://slidepdf.com/reader/full/c-q-a 39/65
n* +he#e i no o#ting method that i unive#all- upe#io# to all othe#. +hep#og#amme# mut ca#efull- eamine the p#oblem and the dei#ed #eult befo#edeciding the pa#ticula# o#ting method. ome of the o#ting method a#e givenbelow*Gubble o#t * When a file containing #eco#d i to be o#ted then Gubble o#t i thebet o#ting method when o#ting b- add#e i ued.
Go#t * t can be #ecommended if the input to the file i 7nown to be nea#l- o#ted.
Peano#t * t can be #ecommended onl- fo# input 7nown to be ve#- nea#l- o#ted.
`uic7 o#t * n the vi#tual memo#- envi#onment, whe#e page of data a#e contantl-being wapped bac7 and fo#th between ete#nal and inte#nal to#age. n p#acticalituation, Muic7 o#t i often the fatet available becaue of it low ove#head and itave#age behavio#.
5eap o#t * ene#all- ued fo# o#ting of complete bina#- t#ee. imple ine#tion o#tand t#aight election o#t * Goth a#e mo#e efficient than bubble o#t. election o#t
i #ecommended fo# mall file when #eco#d a#e la#ge and fo# #eve#e ituationine#tion o#t i #ecommended. +he heap o#t and Muic7 o#t a#e both mo#e efficientthan ine#tion o# election fo# la#ge numbe# of data.
hell o#t * t i #ecommended fo# mode#atel- i/ed file of eve#al hund#edelement.
Uadi o#t * t i #eaonabl- efficient if the numbe# of digit in the 7e- i not toola#ge.
.
>alculating Wated G-te ;n :i7
When a file get to#ed on the di7, at a time :; allocate one clute# fo# it. clute# i nothing but a g#oup of ecto#. 5oweve#, ince all file i/e cannot beepected to be a multiple of A1 b-te, when a file get to#ed often pa#t of theclute# #emain unoccupied. +hi pace goe wate unle the file i/e g#ow tooccup- thee wated b-te. +hefollowing p#og#am find out how much pace i wated fo# all file in all thedi#ecto#ie of the cu##ent d#ive.
Dinclude Edi#.hFDinclude Edo.hFDinclude Etdio.hFDinclude Et#ing.hFDinclude Etdlib.hFunigned b-teTpe#Tclute# !unigned long watedTb-te !unigned long numTfile = 0 !main( ){int pt# = 0, flag = 0, fi#t = 0 !
8/11/2019 C-Q & A
http://slidepdf.com/reader/full/c-q-a 40/65
t#uct ffbl7 f[A0 !t#uct df#ee f#ee ! K4 get clute# info#mation and calculate b-te pe# clute# 4Kgetdf#ee ( 0, Lf#ee ) !b-teTpe#Tclute# = f#ee.dfTbec 4 f#ee.dfTclu !chdi# ( &JJ& ) !
K4 chec7 out file in #oot di#ecto#- fi#t 4KcalTwate( ) ! K4 loop until all di#ecto#ie canned 4Kwhile ( pt# 8= 1 ){ K4 hould do a findfi#t o# a findnet? 4Kif ( fi#t == 0 )flag = findfi#t ( &4.4&, Lf[pt#, HT:UI> ) !eleflag = findnet ( Lf[pt# ) !while ( flag == 0 ){ K4 ma7e u#e it a di#ecto#- and 7ip ove# . L .. ent#ie 4K
if ( f[pt#.ffTatt#ib == HT:UI> LL f[pt#.ffTname[0 8= 2.2 ){flag = chdi# ( f[pt#.ffTname ) ! K4 t#- changing di#ecto#ie 4Kif ( flag == 0 ) K4 did change di# wo#7? 4K{calTwate( ) !fi#t = 0 ! K4 et fo# findfi#t on net pa 4Kb#ea7 !''flag = findnet ( Lf[pt# ) ! K4 ea#ch fo# mo#e di# 4K'
if ( flag 8= 0 RR pt# == 9Q ) K4 didn2t find an- mo#e di# 4K{pt# !chdi# ( &..& ) ! K4 go bac7 one level 4Kfi#t = 1 ! K4 et to findnet on net pa 4K'elept#"" !'p#intf ( &+he#e a#e %lu b-te wated in %lu file.Jn&, watedTb-te,numTfile ) !'calTwate( )
{int flag = 0 !long fullTclute# !t#uct ffbl7 ff ! K4 loo7 fo# all file t-pe 4Kflag = findfi#t ( &4.4&, Lff, HTU:;6<V R HT5::I6 R HTV+IP R HTU>5) !while ( flag == 0 ){
8/11/2019 C-Q & A
http://slidepdf.com/reader/full/c-q-a 41/65
numTfile"" !fullTclute# = ff.ffTfi/e K b-teTpe#Tclute# 4 b-teTpe#Tclute# !watedTb-te "= b-teTpe#Tclute# ( ff.ffTfi/e fullTclute# ) !flag = findnet ( Lff ) !''
:ata t#uctu#e
Solih 6otation
+he method of w#iting all ope#ato# eithe# befo#e thei# ope#ation, o# afte# them, icalled Solih notation, in hono# of it dicove#e#, the Solih mathematician ]an<u7aiewic/. When the ope#ato# a#e w#itten befo#e thei# ope#and, it i called thep#efi fo#m. When the ope#ato# come afte# thei# ope#and. t i called the potfifo#m, o#, ometime #eve#e Solih fo#m o# uffi fo#m. n thi contet, it icutoma#- to ue the coined ph#ae infi fo#m to denote the uual cutom of w#itingbina#- ope#ato# between thei# ope#and. Ho# eample, the ep#eion " G
become "G in p#efi fo#m and G" in potfi fo#m. n the ep#eion " G >,the multiplication i done fi#t, o we conve#t it fi#t, obtaining fi#t " ( G> ) andthen G>" in potfi fo#m. +he p#efi fo#m of thi ep#eion i " G>. +he p#efiand potfi fo#m a#e not #elated b- ta7ing mi##o# image o# othe# uch implet#anfo#mation. lo all pa#enthee have been omitted in the Solih fo#m.
.+he <ongOmp nd etOmp
+he > p#og#amming language doe not let -ou net function. Vou cannot w#ite afunction definition inide anothe# function definition, a in*
int fun1( ){int fun() K4 uch neting of function i not allowed 4K{.....''Gecaue of thi #et#iction it i not poible to hide function name inide a hie#a#ch-. a #eult all the function that -ou decla#e within a p#og#am a#e viible to eachothe#. +hi of cou#e i not a maOo# d#awbac7 ince one can limit viibilit- b-g#ouping function within epa#ate > ou#ce file that belong to diffe#ent logical unit
of the p#og#am. > doe, howeve#, uffe# in anothe# wa- becaue of thi deigndeciion. t p#ovide no ea- wa- to t#anfe# cont#ol out of a function ecept b-#etu#ning to the ep#eion that called the function. Ho# the vat maOo#it- of functioncall, that i a dei#able limitation. Vou want the dicipline of neted function calland #etu#n to help -ou unde#tand flow of cont#ol th#ough a p#og#am. 6eve#thele,on ome occaion that dicipline i too #et#ictive. +he p#og#am i ometime eaie#to w#ite, and to unde#tand, if -ou can Oump out of one o# mo#e function invocationat a ingle t#o7e. Vou want to b-pa the no#mal function #etu#n and t#anfe#cont#ol to omewhe#e in an ea#lie# function invocation.
8/11/2019 C-Q & A
http://slidepdf.com/reader/full/c-q-a 42/65
Ho# eample, -ou ma- want to #etu#n to eecute ome code fo# e##o# #ecove#- nomatte# whe#e an e##o# i detected in -ou# application. +he etOmp and the longOmpfunction p#ovide the tool to accomplih thi. +he etOmp function ave the &tate&o# the &contet& of the p#oce and the longOmp ue the aved contet to #eve#t to ap#eviou point in the p#og#am. What i the contet of the p#oce? n gene#al, thecontet of a p#oce #efe# to info#mation that enable -ou to #econt#uct eactl- the
wa- the p#oce i at a pa#ticula# point in it flow of eecution. n > p#og#am the#elevant info#mation include Muantitie uch a value of S, , H<, >, S, GS,:, I, and : #egite#.
+o ave thi info#mation +u#bo > ue the following t#uctu#e, which i defined, in theheade# file 2etOmp.h2.t-pedef t#uct{unigned OTp !unigned OT !unigned OTflag !unigned OTc !unigned OTip !unigned OTbp !unigned OTdi !unigned OTe !unigned OTi !unigned OTd !' OmpTbuf[1 !+hi i a -temdependent data t-pe becaue diffe#ent -tem might #eMui#ediffe#ent amount of info#mation to captu#e the contet of a p#oce. n +u#bo >, OmpTbuf i impl- an a##a- of ten b-te intege#. +o unde#tand the mechanic ofetOmp and longOmp, loo7 at the following codef#agment.Dinclude &etOmp.h&
OmpTbuf buf !main( ){if ( etOmp ( buf ) == 0 )p#oce( ) !elehandleTe##o#( ) ! K4 eecuted when longOmp i called 4K'p#oce( ){int flag = 0 ! K4 ome p#oceing i done he#e 4K K4 if an e##o# occu# du#ing p#oceing flag i et up 4K
if ( flag )longOmp ( buf, 1 ) !'
Bpon ent#- to etOmp the tac7 contain the add#e of the buffe# buf and theadd#e of the if tatement in the main function, to which etOmp will #etu#n. +heetOmp function copie thi #etu#n add#e a well a the cu##ent value of #egite#,S, , H<, GS, :, I, and :, into the buffe# buf. +hen etOmp #etu#n with a/e#o. n thi cae, the if tatement i atified and the p#oce( ) function i called. f
8/11/2019 C-Q & A
http://slidepdf.com/reader/full/c-q-a 43/65
omething goe w#ong in p#oce( ) (indicated b- the flag va#iable), we call longOmpwith two a#gument* the fi#t i the buffe# that contain the contet to which we will#etu#n. When the tac7 #eve#t bac7 to thi aved tate, and the #etu#n tatement inlongOmp i eecuted, it will be a if we we#e #etu#ning f#om the call to etOmp, whicho#iginall- aved the buffe# buf. +he econd a#gument to longOmp pecifie the #etu#nvalue to be ued du#ing thi #etu#n. t hould be othe# than /e#o o that in the if
tatement we can tell whethe# the #etu#n i induced b- a longOmp.
+he etOmpKlongOmp combination enable -ou to Oump unconditionall- f#om one >function to anothe# without uing the conventional #etu#n tatement. Ientiall-,etOmp ma#7 the detination of the Oump and longOmp i a nonlocal goto thateecute the Oump.:ata t#uctu#e
>ompa#ion +#ee...
+he compa#ion t#ee alo called deciion t#ee o# ea#ch t#ee of an algo#ithm, iobtained b- t#acing th#ough the action of the algo#ithm, #ep#eenting each
compa#ion of 7e- b- a ve#te of the t#ee (which we d#aw a a ci#cle). nide theci#cle we put the inde of the 7e- againt which we a#e compa#ing the ta#get 7e-.G#anche (line) d#awn down f#om the ci#cle #ep#eent the poible outcome of thecompa#ion and a#e labeled acco#dingl-. When the algo#ithm te#minate, we puteithe# H (fo# failu#e) o# the location whe#e the ta#get i found at the end of theapp#op#iate b#anch, which we call a leaf, and d#aw a a Mua#e. <eave a#e aloometime called end ve#tice o# ete#nal ve#tice of the t#ee. +he #emaining ve#ticea#e called the inte#nal ve#tice of the t#ee. +he compa#ion t#ee fo# eMuential ea#chi epeciall- imple.@.
uppoe we have a floatingpoint numbe# with highe# p#eciion a- 1.19@@and we wih it to be p#inted with onl- p#eciion up to two decimal place. 5ow can do thi?
n. +hi can achieved th#ough the ue of upp#eion cha# 242 in the fo#mat t#ing of p#intf( ) which i hown in the following p#og#am.main( ){int p = !float n = 1.19@@ !p#intf ( &%.4f&,p, n ) !'
Q.pawning
ll p#og#am that we eecute f#om :; p#ompt can be thought of a child#en of>;PP6:.>;P. +hu, the p#og#am that we eecute i a child p#oce, whe#ea>;PP6:.>;P #unning in memo#- i it pa#ent. +he p#oce of a pa#ent p#ocegiving bi#th to a child p#oce i 7nown a 2pawning2. f the pawned p#og#am o
8/11/2019 C-Q & A
http://slidepdf.com/reader/full/c-q-a 44/65
dei#e, it ma- in tu#n pawn child#en of it own, which then eecute and #etu#ncont#ol to thei# pa#ent. Who i the pa#ent of >;PP6:.>;P? >;PP6:.>;P itelf.We can t#ace the anceto# of ou# p#og#am uing the field Sa#ent S#oce : (S:)p#eent at offet 01 in the S#og#am egment S#efi (SS). +o t#ace thi ancet#-ou# p#og#am hould fi#t locate it SS, et#act the pa#ent p#oce : f#om it andthen ue thi to find SS of the pa#ent. +hi p#oce can be #epeated till we #each
>;PP6:.>;P (p#oce : of >;PP6:.>;P i it own SS), the fathe# of allp#ocee. 5e#e i a p#og#am which achieve thi...
K4 SW6.> 4KDinclude &do.h&
unigned oldpp, newpp, fa# 4ebTeg, i !cha# fa# 4ebTpt# !
main( ){oldpp = Tpp !
while ( 1 ){p#intf ( &Jn& ) !p#intname ( oldpp ) !p#intf ( & pawned b- & ) !
newpp = 4 ( ( unigned fa# 4 ) PNTHS ( oldpp, 01 ) ) !
if ( 4 ( ( unigned 4 ) PNTHS ( newpp, 01 ) ) == newpp )b#ea7 !ele
oldpp = newpp !
p#intname ( newpp ) !'
p#intf ( &%0 (%09\)&, &>;PP6:.>;P&, newpp ) !'
p#intname ( unigned lpp ){cha# d#ive[A, di#[@, name[13, et[A !
ebTeg = ( unigned fa# 4 ) PNTHS ( lpp, 0> ) !ebTpt# = PNTHS ( 4ebTeg, 0 ) !
i = 0 !while ( 1 ){if ( ebTpt#[i == 0 ){if ( ebTpt#[i " 1 == 0 LL ebTpt#[i " == 1 )
8/11/2019 C-Q & A
http://slidepdf.com/reader/full/c-q-a 45/65
{i "= 9 !b#ea7 !''i"" !
'
fnplit ( ebTpt# " i, d#ive, di#, name, et ) !t#cat ( name, et ) !p#intf ( &%0 (%09\)&, name, oldpp ) !'
;n #unning the p#og#am f#om within +> the output obtained i hown below.SW6.I\I (A@Q) pawned b- +>.I\I (0) +>.I\I (0) pawned b->;PP6:.>;P (0AG@). +he p#og#am impl- copie it own p#oce : in theva#iable oldpp and then ue it to et#act it own filename f#om it envi#onmentbloc7. +hi i done b- the function p#intname( ). +he value in oldpp i then ued to
#et#ieve the pa#ent2 S: in newpp. H#om the#e the p#og#am loop #epo#ting thevalue of oldpp, newpp and the co##eponding file name until the p#og#am #eache>;PP6:.>;P.
+he p#intname( ) function fi#t locate the envi#onment bloc7 of the p#og#am andthen et#act the file name f#om the envi#onment bloc7. +he fnplit( ) function habeen ued to eliminate the path p#eent p#io# to the file name. :o not #un thep#og#am f#om command line ince it would give -ou onl- one level of ancet#-.
:ata t#uctu#e
>hooing the data t#uctu#e to be ued fo# info#mation #et#ieval. Ho# p#oblem ofinfo#mation #et#ieval, conide# the i/e, numbe#, and location of the #eco#d alongwith the t-pe and t#uctu#e of the 7e- while chooing the data t#uctu#e to beued. Ho# mall #eco#d, highpeed inte#nal memo#- will be ued, and bina#- ea#cht#ee will li7el- p#ove adeMuate. Ho# info#mation #et#ieval f#om di7 file, methodemplo-ing multiwa- b#anching, uch a t#ee, Gt#ee , and hah table, will uuall-be upe#io#. +#ie a#e pa#ticula#l- uited to application whe#e the 7e- a#e t#uctu#eda a eMuence of -mbol and whe#e the et of 7e- i #elativel- dene in the et ofall poible 7e-. Ho# othe# application, method that t#eat the 7e- a a ingle unitwill often p#ove upe#io#. Gt#ee, togethe# with va#iou gene#ali/ation andetenion, can be uefull- applied to man- p#oblem conce#ned with ete#nalinfo#mation #et#ieval.
@0.
Ca#iabl- :imenioned ##a-
While dealing with cientific o# Inginee#ing p#oblem one i often #eMui#ed to ma7eue of multidimenioned a##a-. 5oweve#, when it come to paing multidimenionala##a- to a function > i found wanting. +hi i becaue the > compile# want to7now the i/e of all but the fi#t dimenion of an- a##a- paed to a function. Ho#intance, we can define a function compute ( int n, float [ ), but not compute ( intn, [[).
8/11/2019 C-Q & A
http://slidepdf.com/reader/full/c-q-a 46/65
+hu, > can deal with va#iabl- dimenioned 1: a##a-, but when an a##a- ha mo#ethan one dimenion, the > compile# ha to 7now the i/e of the lat dimenionep#eed a a contant. +hi p#oblem ha long been #ecogni/ed, and ome of theolution that a#e often ued a#e*
:ecla#e the a##a- in the function to be big enough to tac7le all poible ituation.
+hi can lead to a watage of lot of p#eciou memo#- in mot cae. nothe# olutioni to cont#uct multipledimenion a##a- a an a##a- of pointe#. Ho# eample, amat#i (: a##a-) of float can be decla#ed a a 1: a##a- of float pointe#, witheach element pointing to an a##a- of float. +he p#oblem with thi method i that thecalling function ha to define all a##a- in thi fahion. +hi mean that an- othe#computation done on the a##a- mut ta7e thi pecial t#uctu#e into account.
nothe# ea- olution, though eldom ued, eit. +hi i baed on the followingmethod*
Sa the a##a- to the function a though it i a pointe# to an a##a- of float (o# theapp#op#iate data t-pe), no matte# how man- dimenion the a##a- actuall- ha,
along with the dimenion of the a##a-. Uefe#ence individual a##a- element aoffet f#om thi pointe#.W#ite -ou# algo#ithm o that a##a- element a#e acceed in to#age o#de#. +hefollowing p#og#am fo# multipl-ing two mat#ice illut#ate thip#ocedu#e.D define P 3D define 6 D define S 9
float a[P[6, b[6[S, c[P[S !void mulmat ( int, int, int, float4, float4, float4 ) !
main( ){int i, O !fo# ( i = 0 ! i E P ! i"" )fo# ( O = 0 ! O E 6 ! O"" )a[i[O = i " O !
fo# ( i = 0 ! i E 6 ! i"" )fo# ( O = 0 ! O E S ! O"" )b[i[O = i " O !
mulmat ( P, 6, S, a, b, c ) !fo# ( i = 0 ! i E P ! i"" )
{p#intf ( &Jn& ) !fo# ( O = 0 ! O E S ! O"" )p#intf ( &%fJt&, c[i[O ) !''
void mulmat ( int m, int n, int p, float 4a, float 4b, float 4c ){
8/11/2019 C-Q & A
http://slidepdf.com/reader/full/c-q-a 47/65
float 4pt#tob, 4pt#toc !int i, O, 7, nc !
K4 et all element of mat#i c to 0 4Kfo# ( i = 0 ! i E m 4 p ! i"" )4( c " i ) = 0 !
fo# ( i = 0 ! i E m ! i"" ){pt#tob = b !fo# ( 7 = 0 ! 7 E n ! 7"" ){pt#toc = c !
fo# ( O = 0 ! O E p ! O"" )4pt#toc"" "= 4a 4 4pt#tob"" !a"" !'
c "= p !''
We 7now that > to#e a##a- element in a #owmaOo# o#de#. 5ence to enu#e thatthe element a#e acceed in the to#age o#de# the above p#og#am ue a va#iationof the no#mal mat#imultiplication p#ocedu#e. +he peudo code fo# thi i givenbelow*fo# i = 1 to mfo# O = 1 to pc[i[O = 0endfo# 7 = 1 to n
fo# O = 1 to pc[i[O = c[i[O " a[i[7 4 b[7[Oendendend@1.
Wh- i it not poible to can t#ing f#om 7e-boa#d in cae of a##a- of pointe# tot#ing?
n* When an a##a- i decla#ed, dimenion of a##a- hould be pecified o thatcompile# can allocate memo#- fo# the a##a-. When a##a- of pointe# to t#ing idecla#ed it element would contain ga#bage add#ee. +hee add#ee would bepaed to canf( ). o t#ing can be #eceived but the- would get to#ed at un7ownlocation. +hi i [email protected] ##a-
8/11/2019 C-Q & A
http://slidepdf.com/reader/full/c-q-a 48/65
f in a p#og#am a va#iable i to ta7e onl- two value 1 and 0, we #eall- need onl- aingle bit to to#e it. imila#l-, if a va#iable i to ta7e value f#om 0 to 3, then twobit a#e ufficient to to#e thee value. nd if a va#iable i to ta7e value f#om 0th#ough , then th#ee bit will be enough, and o on. Wh- wate an enti#e intege#when one o# two o# th#ee bit will do? Gecaue the#e a#en2t an- one bit o# two bit o#th#ee bit data t-pe available in >. 5oweve#, when the#e a#e eve#al va#iable whoe
maimum value a#e mall enough to pac7 into a ingle memo#- location, we canue _bit field2 to to#e eve#al value in a ingle intege#. Git field a#e dicued inmot tanda#d > tet. +he- a#e uuall- ued when we want to to#e ao#tedinfo#mation which can be accommodated in 1, , 3 bit etc.
Ho# eample, the following data about an emplo-ee can be eail- to#ed uing bitfield.
male o# femaleingle, ma##ied, divo#ced o# widowedhave one of the eight diffe#ent hobbiecan chooe f#om an- of the fifteen diffe#ent cheme p#opoed b- the compan- to
pu#ue hiKhe# hobb-.
+hi mean we need one bit to to#e gende#, two to to#e ma#ital tatu, th#ee fo#hobb-, and fou# fo# cheme (with one value ued fo# thoe who a#e not dei#ou ofavailing an- of the cheme). We need ten bit altogethe#, which mean we can pac7all thi info#mation into a ingle intege#, ince an intege# i 1 bit long.
t time we ma- need to to#e eve#al +#ue o# Hale tatue. n uch cae inteadof uing bit field uing an a##a- of bit would be mo#e enible. ;n thi a##a- wema- be #eMui#ed to pe#fo#m the following ope#ation*
et a bit (ma7e it 1).>lea# a bit (ma7e it 0).+et the tatu of a bit in the a##a-.Ueach the app#op#iate bit lot in the a##a-.ene#ate a bit ma7 fo# etting and clea#ing a bit.We can implement thee ope#ation uing mac#o given below*
Ddefine >5U^I @Ddefine PN ( - ) ( 1 EE - % >5U^I )Ddefine G+<;+ ( - ) ( - K >5U^I )Ddefine I+ ( , - ) ( [G+<;+( - ) R= PN( - ) )Ddefine ><IU ( , - ) ( [G+<;+( - ) L= PN( - ) )Ddefine +I+ ( , - ) ( [G+<;+( - ) L PN( - ) )Ddefine 6BP<;+ ( n ) ( ( n " >5U^I 1) K >5U^I )
Bing thee mac#o we can decla#e an a##a- of A0 bit be a-ing,
cha# a##[6BP<;+(A0) !+o et the 0th bit we can a-,
I+(a##, 0 ) !nd if we a#e to tet the tatu of 90th bit we ma- a-,
8/11/2019 C-Q & A
http://slidepdf.com/reader/full/c-q-a 49/65
if ( +I+ ( a##, 90 ) )Bing bit a##a- often #eult into aving a lot of p#eciou memo#-. Ho# eample, thefollowing p#og#am which implement the ieve of I#atothene fo# gene#ating p#imenumbe# malle# than 100 #eMui#e onl- 13 b-te. 5ad we implemented the amelogic uing an a##a- of intege# we would have #eMui#ed an a##a- of 100 intege#,that i 00 b-te.
Dinclude Etdio.hFDinclude Et#ing.hF
Ddefine P\ 100
main( ){cha# a##[6BP<;+( P\ ) !int i, O !
memet ( a##, 0, 6BP<;+( P\ ) ) !fo# ( i = ! i E P\ ! i"" )
{if ( 8+I+ ( a##, i ) ){p#intf ( &Jn%d&, i ) !fo# ( O = i " i ! O E P\ ! O "= i )I+ ( a##, O ) !'''@3.nfo#mation 5iding in >
+hough > language doen2t full- uppo#t encapulation a >"" doe, the#e i aimple techniMue th#ough which we can implement encapulation in >. +he techniMuethat achieve thi i modula# p#og#amming in >. Podula# p#og#amming #eMui#e alittle et#a wo#7 f#om the p#og#amme#, but pa- fo# itelf du#ing maintenance. +ounde#tand thi techniMue let u ta7e the eample of the popula# tac7 datat#uctu#e. +he#e a#e man- method of implementing a tac7 (a##a-, lin7ed lit, etc.).nfo#mation hiding teache that ue# hould be able to puh and pop the tac72element without 7nowing about the tac72 implementation. benefit of thi o#t ofinfo#mation hiding i that ue# don2t have to change thei# code even if theimplementation detail change.
>onide# the following cena#io*
+o be able to app#eciate the benefit of modula# p#og#amming and the#eb-info#mation hiding, would fi#t how a t#aditional implementation of the tac7 datat#uctu#e uing pointe# and a lin7ed lit of t#uctu#e. +he main( ) function call thepuh( ) and pop( ) function.
Dinclude Ealloc.hFt-pedef int element !
8/11/2019 C-Q & A
http://slidepdf.com/reader/full/c-q-a 50/65
void initiali/eTtac7 ( t#uct node 44 ) !void puh ( t#uct node 44, element ) !element pop ( t#uct node 4 ) !int iempt- ( t#uct node 4 ) !t#uct node{
element data !t#uct node 4net !' !void main( ){t#uct node 4top !element num !initiali/eTtac7 ( Ltop ) !puh ( Ltop, 10 ) !puh ( Ltop, 0 ) !puh ( Ltop, 30 ) !if ( iempt- ( top ) )p#intf ( &Jntac7 i empt-& ) !
ele{num = pop ( top ) !p#intf ( &Jn Sopped %d&, num ) !''void initiali/eTtac7 ( t#uct node 44p ){4p = 6B<< !'void puh ( t#uct node 44p, element n ){
t#uct node 4# !# = ( t#uct node 4) malloc ( i/eof ( t#uct node ) ) !# F data = n !if ( 4p == 6B<< )# F net = 6B<< !ele# F net = 4p !4p = # !'element pop ( t#uct node 4p ){element n !t#uct node 4# !
n = p F data !# = p !p = p F net !f#ee ( # ) !#etu#n ( n ) !'int iempt- ( t#uct node 4p ){if ( p == 6B<< )
8/11/2019 C-Q & A
http://slidepdf.com/reader/full/c-q-a 51/65
#etu#n ( 1 ) !ele#etu#n ( 0 ) !'6otice how the pecific implementation of the data t#uctu#e i t#ewn th#oughoutmain( ). main( ) mut ee the definition of the t#uctu#e node to ue the puh( ),
pop( ), and othe# tac7 function. +hu the implementation i not hidden, but imied with the abt#act ope#ation.
:ata t#uctu#e
Uadi o#t
+hi o#ting techniMue i baed on the value of the actual digit in the poitional#ep#eentation of the numbe# being o#ted. Bing the decimal bae, fo# eample,whe#e the #adi i 10, the numbe# can be pa#titioned into ten g#oup on the o#te#.Ho# eample, to o#t a collection of numbe# whe#e each numbe# i a fou#digitnumbe#, then, ll the numbe# a#e fi#t o#ted acco#ding to the the digit at unit2place.n the econd pa, the numbe# a#e o#ted acco#ding to the digit at tenth place. nthe thi#d pa, the numbe# a#e o#ted acco#ding to the digit at hund#edth place. nthe fo#th and lat pa, the numbe# a#e o#ted acco#ding to the digit at thouandthplace.:u#ing each pa, each numbe# i ta7en in the o#de# in which it appea# in pa#titionf#om unit2 place onwa#d. When thee action have been pe#fo#med fo# each digit,ta#ting with the leat ignificant and ending with mot ignificant, the numbe# a#eo#ted. +hi o#ting method i called the #adi o#t.
<et u ta7e anothe# eample. uppoe we have a lit of name. +o o#t thee nameuing #adi o#t method we will have to claif- them into g#oup +he lit i fi#to#ted on the fi#t lette# of each name, i.e. the name a#e a##anged in clae,
whe#e the fi#t cla conit of thoe name that begin with alphabet 22, the econdcla conit of thoe name that begin with alphabet 2G2 and o on. :u#ing theecond pa each cla i alphabeti/ed acco#ding to the econd lette# of the name,and o on.
@9.
Iception 5andling in >
>onide# the following p#og#am*
Dinclude Emath.hF
void main( ){float i !i = pow ( , 3 ) !p#intf ( &%f&, i ) !'
int mathe## ( t#uct eception 4a ){
8/11/2019 C-Q & A
http://slidepdf.com/reader/full/c-q-a 52/65
if ( a F t-pe == :;P6 ){if ( 8t#cmp ( a F name, &pow& ) ){a F #etval = pow ( ( a F a#g1 ), a F a#g ) !#etu#n 1 !
''#etu#n 0 !'
f we pa a negative value in pow( ) function a #un time e##o# occu#. f we wih toget the p#ope# output even afte# paing a negative value in the pow( ) function wemut handle the #un time e##o#. Ho# thi, we can define a function mathe##( ) which idecla#ed in the 2math.h2 file. n thi function we can detect the #untime e##o# andw#ite ou# code to co##ect the e##o#. +he element of the eception t#uctu#e #eceivethe function name and a#gument of the function cauing the eception.
:ata t#uctu#e
C< +#ee
Ho# ideal ea#ching in a bina#- ea#ch t#ee, the height of the left and #ight ubt#eeof an- node hould be eMual. Gut, due to #andom ine#tion and deletion pe#fo#medon a bina#- ea#ch t#ee, it often tu#n out to be fa# f#om ideal. cloe app#oimationto an ideal bina#- ea#ch t#ee i achievable if it can be enu#ed that the diffe#encebetween the height of the left and the #ight ub t#ee of an- node in the t#ee i atmot one. bina#- ea#ch t#ee in which the diffe#ence of height of the #ight and leftubt#ee of an- node i le than o# eMual to one i 7nown a an C< t#ee. C< t#eei alo called a Galanced +#ee. +he name &C< +#ee& i de#ived f#om the name of itinvento# who a#e delonCeil7ii and <andi. node in an C< t#ee have a new field
to to#e the &balance facto#& of a node which denote the diffe#ence of heightbetween the left and the #ight ubt#ee of the t#ee #ooted at that node. nd it canaume one of theth#ee poible value {1,0,1'.@A.
BniMue combination fo# a given numbe#
5ow do w#ite a p#og#am which can gene#ate all poible combination of numbe#f#om 1 to one le than the given numbe# ?main( ){long tep, fval, btp, cnt1 !int num, unit, bo[[13, cnt, cnt3, cnt9 !p#intf ( &Inte# 6umbe# & ) !canf ( &%d&, Lnum ) !num = num E 1 ? 1 * num F 1 ? 1 * num !fo# ( tep = 1, cnt1 = ! cnt1 E= num ! tep 4= cnt1"" ) !fo# ( cnt1 = 1 ! cnt1 E= tep ! cnt1"" ){
8/11/2019 C-Q & A
http://slidepdf.com/reader/full/c-q-a 53/65
fo# ( cnt = 1 ! cnt E= num ! cnt"" )bo[0[cnt = cnt !fo# ( fval = tep, btp = cnt1, cnt = 1 ! cnt E= num ! cnt"" ){if ( btp == 0 ){
cnt9=num !while ( bo[0[cnt9 == 0 )cnt9 !'ele{fval K= num cnt " 1 !unit = ( btp " fval 1 ) K fval !btp %= fval !fo# ( cnt9 = 0, cnt3 = 1 ! cnt3 E= unit ! cnt3"" )while ( bo[0[""cnt9 == 0 ) !'bo[1[cnt = bo[0[cnt9 !
bo[0[cnt9 = 0 !'p#intf ( &JneM.6o.%ld*&, cnt1 ) !fo# ( cnt = 1 ! cnt E= num ! cnt"" )p#intf ( & %d&, bo[1[cnt ) !''+hi p#og#am compute the total numbe# of tep. Gut intead of ente#ing into theloop of the fi#t and lat combination to be gene#ated it ue a loop of 1 to numbe# of combination. Ho# eample, in cae of input being A the numbe# of poiblecombination would be facto#ial A, i.e. 10. +he p#og#am uffe# f#om the limitationthat it cannot gene#ate combination fo# input be-ond 1 ince a long int cannot
handle the #eulting combination.
:ata t#uctu#e
5ahing...
5ahing o# hah add#eing i a ea#ching techniMue. Buall-, ea#ch of an element ica##ied out via a eMuence of compa#ion. 5ahing diffe# f#om thi a it iindependent of the numbe# of element n in the collection of data. 5e#e, the add#eo# location of an element i obtained b- computing ome a#ithmetic function.5ahing i uuall- ued in file management. +he gene#al idea i of uing the 7e- todete#mine the add#e of a #eco#d. Ho# thi, a function fun( ) i applied to each 7e-,called the hah function. ome of the popula# hah function a#e* 2:iviion2 method,
2PidMua#e2 method, and 2Holding2 method. +wo #eco#d cannot occup- the amepoition. uch a ituation i called a hah colliion o# a hah clah. +he#e a#e twobaic method of dealing with a hah clah. +he fi#t techniMue, called #ehahing,involve uing econda#- hah function on the hah 7e- of the item. +he #ehahfunction i applied ucceivel- until an empt- poition i found whe#e the item canbe ine#ted. f the hah poition of the item i found to be occupied du#ing a ea#ch,the #ehah function i again ued to locate the item. +he econd techniMue, calledchaining, build a lin7ed lit of all item whoe 7e- hah to the ame value. :u#ingea#ch, thi ho#t lin7ed lit i t#ave#ed eMuentiall- fo# the dei#ed 7e-. +hi
8/11/2019 C-Q & A
http://slidepdf.com/reader/full/c-q-a 54/65
techniMue involve adding an et#a lin7 field to each table poition.
@.
+he following p#og#am demont#ate how to get input f#om the ue# in g#aphicmode, echoed in the cu##ent colo# and font i/e and font t-le.
Ddefine ;6 1Ddefine ;HH 0Dinclude Eg#aphic.hFmain( ){cha# namet#ing[@0, aget#ing[@0 !int age, gd = :I+I>+, gm !initg#aph ( Lgd, Lgm, &c*JJtcJJbgi& ) !etb7colo# ( G<BI ) !
etcolo# ( VI<<;W ) !ettett-le ( ;+5>TH;6+, 5;U^T:U, 0 ) !moveto ( 0, 0 ) !outtet ( &Inte# -ou# name* & ) !get#t#ing ( namet#ing ) !moveto ( 0, get-( ) " tetheight ( && ) ) !outtet ( &6ame* & ) !outtet ( namet#ing ) !moveto ( 0, get-( ) " tetheight ( && ) ) !outtet ( &S#e 7e- to eit8 & ) !getch( ) !cloeg#aph( ) !#eto#ec#tmode( ) !
'get#t#ing ( cha# 4inputt#ing ){int t#ingnde = 0, old>olo# !cha# ch, outt#ing[ ! K4 Cal will to#e the c#een poition fo# each cha# 4Kint Cal[AA !outt#ing[1 = 0 !Cal[0 = get( ) !do{cu#o# ( ;6 ) !ch = getch( ) !cu#o# ( ;HH ) !if ( ch == 0 ) K4 avoid dealing with all pecial 7e- 4Kgetch( ) !ele{if ( ch == @ ) K4 bac7pace 4K{old>olo# = getcolo#( ) !t#ingnde !
8/11/2019 C-Q & A
http://slidepdf.com/reader/full/c-q-a 55/65
if ( t#ingnde E 0 )t#ingnde = 0 ! K4 move to ( old ho#/ poition, cu##ent ve#t poition ) 4Kmoveto ( Cal[t#ingnde, get-( ) ) !etcolo# ( getb7colo#( ) ) !outt#ing[0 = inputt#ing[t#ingnde !
outtet ( outt#ing ) !moveto ( Cal [t#ingnde, get-( ) ) !etcolo# ( old>olo# ) !'ele{inputt#ing[t#ingnde = ch !outt#ing[0 = ch !outtet ( outt#ing ) !""t#ingnde !Cal[t#ingnde = get( ) !''
' while ( ch 8= 13 LL ch 8= 10 ) !inputt#ing[t#ingnde = 0 !'cu#o# ( int on ){int cu#\, old>olo# ! K4 we2ll ue an unde#co#e a a cu#o# 4Kcha# uGa#t#[ = { 2T2, 0 ' !if ( 8on ){old>olo# = getcolo#( ) !etcolo# ( getb7colo#( ) ) !
' K4 ave ho#i/ontal poition befo#e d#awing cu#o# 4Kcu#\ = get( ) !outtet ( uGa#t# ) !moveto ( cu#\, get-( ) ) ! K4 if we changed the colo# to e#ae cu#o#, change it bac7 4Kif ( 8on )etcolo# ( old>olo# ) !'+he function get#t#ing( ) echoe g#aphicall- the ue# input and to#e it in a buffe#,and the function cu#o#( ) handle the cu#o# poition.
-tem Btilit-
What i ga#bage collection?
n* uppoe ome memo#- pace become #euable becaue a node i #eleaedf#om a lin7ed lit. 5ence, we want the pace to be available fo# futu#e ue. ;ne wa-to b#ing thi about i to immediatel- #eine#t the pace into the f#eeto#age lit.5oweve#, thi method ma- be too timeconuming fo# the ope#ating -tem. +heope#ating -tem ma- pe#iodicall- collect all the deleted pace onto the f#eeto#agelit. +he techniMue that doe thi collection i called a#bage >ollection. a#bage
8/11/2019 C-Q & A
http://slidepdf.com/reader/full/c-q-a 56/65
>ollection uuall- ta7e place in two tep* Hi#t the a#bage >ollecto# #un th#oughall lit, tagging whoe cell a#e cu##entl- in ue, and then it #un th#ough thememo#-, collecting all untagged pace onto the f#eeto#age lit. +he a#bage>ollection ma- ta7e place when the#e i onl- ome minimum amount of pace o# nopace at all left in the f#eeto#age lit, o# when the >SB i idle and ha time to dothe collection. ene#all- pea7ing, the a#bage >ollection i inviible to the
p#og#amme#.
@.
5ow do get the time elaped between two function call ?
n* +he function difftime( ) find the diffe#ence between two time. t calculate theelaped time in econd and #etu#n the diffe#ence between two time a a doublevalue.
Dinclude Etime.hFDinclude Etdio.hF
Dinclude Edo.hF
main( ){int a[ = { , 39, A, @, 11, 33, , 11, 9A, Q, ' !int !timeTt t1, t ! KK timeTt define the value ued fo# time function
= i/eof ( a ) K !t1 = time ( 6B<< ) !elTo#t ( a, ) ! KK o#t a##a- b- election o#tbubTo#t ( a, ) ! KK o#t a##a- b- bubble o#t methodt = time ( 6B<< ) !
p#intf ( &Jn+he diffe#ence between two function call i %f&, difftime (t, t1 ) ) !'
n the above p#og#am we have called difftime( ) function that #etu#n the timeelaped f#om t1 to t.@@.
ene#almain( )
{cha# 4 ! = fun ( 1@, ) !p#intf ( &Jn%&, ) !'fun ( unigned int num, int bae ){tatic cha# buff[33 !cha# 4pt# !
8/11/2019 C-Q & A
http://slidepdf.com/reader/full/c-q-a 57/65
pt# = Lbuff [ i/eof ( buff ) 1 !4pt# = 2J02 !do{4pt# = &0139A@Qabcdef&[ num % bae !num K= bae !
' while ( num 8= 0 ) !#etu#n pt# !'+he above p#og#am would conve#t the numbe# 1@ to the bae . Vou can conve#t anumbe# to a headecimal o# octal fo#m b- paing the numbe# and the bae, to thefunction fun( ).
:ata t#uctu#e
What i a p#io#it- Mueue?
n* we 7now in a tac7, the latet element i deleted and in a Mueue the oldetelement i deleted. t ma- be #eMui#ed to delete an element with the highet p#io#it-in the given et of value and not onl- the oldet o# the newet one. data t#uctu#ethat uppo#t efficient ine#tion of a new element and deletion of element with thehighet p#io#it- i 7nown a p#io#it- Mueue. +he#e a#e two t-pe of p#io#it- Mueue*an acending p#io#it- Mueue i a collection of item into which item can be ine#teda#bit#a#il- and f#om which onl- the mallet item can be #emoved. decendingo#de# p#io#it- Mueue i imila# but allow onl- the la#get item to be deleted.@Q.
What i the diffe#ence between cont cha# 4p, cha# cont 4p, and cha#4 cont p ?
2cont cha# 4p2 and 2cha# cont 4p2 a#e the ame, i.e. p point to a contant cha#acte#.;n the othe# hand, 2cha#4 cont p2 mean p i a contant pointe# pointing to acha#acte# which mean we cannot change the pointe# p but we can change thecha#acte# which p i pointing to.Q0.
What2 the diffe#ence between a null pointe#, a 6B<< mac#o, the > 6B< cha#acte#and a null t#ing?
n* null pointe# i a pointe# which doen2t point an-whe#e. 6B<< mac#o i ued
to #ep#eent the null pointe# in ou#ce code. t ha a value 0 aociated with it. +he> 6B< cha#acte# ha all it bit a 0 but doen2t have an- #elationhip with thenull pointe#. +he null t#ing i Out anothe# name fo# an empt- t#ing &&.
-tem Btilit-
pa#e Pat#i...
pa#e mat#i i one whe#e mot of it element a#e /e#o. +he#e i no p#ecie
8/11/2019 C-Q & A
http://slidepdf.com/reader/full/c-q-a 58/65
definition a to 7now whethe# a mat#i i pa#ed o# not, but it i a concept which weall can #ecogni/e intuitivel-. +he natu#al method of #ep#eenting mat#ice in memo#-a twodimenional a##a- ma- not be uitable fo# pa#e mat#ice. +hat i one ma-ave pace b- to#ing onl- thoe ent#ie which ma- be non/e#o. f thi i done, thenthe mat#i ma- be thought of a an o#de#ed lit of non/e#o element onl-.nfo#mation about a non/e#o element ha th#ee pa#t*
an intege# #ep#eenting it #ow,an intege# #ep#eenting it column andthe data aociated with thi element.
+hat i, each element of a mat#i i uniMuel- cha#acte#i/ed b- it #ow and columnpoition, a- i, O. We might to#e that mat#i a a lit of 3tuple of the fo#m (i, O,data), a hown below,
lthough the non/e#o element ma- be to#ed in the a##a- in an- o#de#, 7eepingthem o#de#ed in ome fahion ma- be advantageou fo# fu#the# p#oceing. 6otethat above a##a- i a##anged in inc#eaing o#de# of the #ow numbe# of non/e#oelement. Po#eove#, fo# element in the ame #ow numbe#, the a##a- i a##anged in
o#de# of inc#eaing column numbe#.Q1.
Sointe#
What doe the e##o# &6ull Sointe# ignment& mean and what caue thi e##o#?
n* +he 6ull Sointe# ignment e##o# i gene#ated onl- in mall and mediummemo#- model. +hi e##o# occu# in p#og#am which attempt to change the bottomof the data egment. n Go#land2 > o# >"" compile#, Go#land place fou# /e#ob-te at the bottom of the data egment, followed b- the Go#land cop-#ight notice&Go#land >"" >op-#ight 1QQ1 Go#land ntl.&. n the mall and medium memo#-model, a null pointe# point to :*0000. +hu aigning a value to the memo#-#efe#enced b- thi pointe# will ove#w#ite the fi#t /e#o b-te in the data egment. tp#og#am te#mination, the fou# /e#o and the cop-#ight banne# a#e chec7ed. f eithe#ha been modified, then the 6ull Sointe# ignment e##o# i gene#ated. 6ote thatthe pointe# ma- not t#ul- be null, but ma- be a wild pointe# that #efe#ence thee7e- a#ea in the data egment.
:ata t#uctu#e
5ow to build an ep#eion t#ee ?
n* n ep#eion t#ee i a bina#- t#ee which i built f#om imple ope#and andope#ato# of an (a#ithmetic o# logical ) ep#eion b- placing imple ope#and a theleave of a bina#- t#ee and the ope#ato# a the inte#io# node. f an ope#ato# ibina#- , then it ha two nonempt- ubt#ee, that a#e it left and #ight ope#and(eithe# imple ope#and o# ub ep#eion). f an ope#ato# i una#-, then onl- one of it ubt#ee i nonempt-, the one on the left o# #ight acco#ding a the ope#ato# iw#itten on the #ight o# left of it ope#and. We t#aditionall- w#ite ome una#-ope#ato# to the left of thei# ope#and, uch a && ( una#- negation) o# the tanda#dfunction li7e log( ), in( ) etc. ;the# a#e w#itten on the #ight, uch a the facto#ial
8/11/2019 C-Q & A
http://slidepdf.com/reader/full/c-q-a 59/65
function ()8. f the ope#ato# i w#itten on the left, then in the ep#eion t#ee we ta7eit left ubt#ee a empt-. f it appea# on the #ight, then it #ight ubt#ee will beempt-. n eample of an ep#eion t#ee i hown below fo# the ep#eion ( a Eb ) o# ( c " d ) .
Q.
>an we get the #emainde# of a floating point diviion ?
n * Ve. lthough the % ope#ato# fail to wo#7 on float numbe# we can till getthe #emainde# of floating point diviion b- uing a function fmod( ). +he fmod( )function divide the two float numbe# paed to it a pa#amete# and #etu#n the#emainde# a a floatingpoint value. Hollowing p#og#am how fmod( ) function atwo#7.
Dinclude Emath.hF
main( ){p#intf ( &%f&, fmod ( A.1A, 3.0 ) ) !'
+he above code nippet would give the output a .1A0000.Q3.
5ow to et#act the intege# pa#t and a f#actional pa#t of a floating point numbe#?
n* > function modf( ) can be ued to get the intege# and f#actional pa#t of a
floating point.
Dinclude &math.h&
main( ){double val, i, f !val = A.1A !f = modf ( val, Li ) !p#intf ( &JnHo# the value %f intege# pa#t = %f and f#actional pa#t = %f&,val, i, f ) !'
+he output of the above p#og#am will be*
Ho# the value A.1A0000 intege# pa#t = A.000000 and f#actional pa#t =0.1A0000
Q9.
8/11/2019 C-Q & A
http://slidepdf.com/reader/full/c-q-a 60/65
8/11/2019 C-Q & A
http://slidepdf.com/reader/full/c-q-a 61/65
Q.
Wh- the output of i/eof ( 2a2 ) i and not 1 ?n* >ha#acte# contant in > a#e of t-pe int, hence i/eof ( 2a2 ) i eMuivalent toi/eof ( int ), i.e. . 5ence the output come out to be b-te.
Q@.
>an we ue canf( ) function to can a multiple wo#d t#ing th#ough 7e-boa#d?
n* Ve. lthough we uuall- ue canf( ) function to #eceive a ingle wo#d t#ingand get( ) to #eceive a multiwo#d t#ing f#om 7e-boa#d we can alo ue canf( )function fo# canning a multiwo#d t#ing f#om 7e-boa#d. Hollowing p#og#am howhow to achieve thi.
main( ){cha# buff[1A !canf ( &%[Jn&, buff ) !put ( buff ) !'
n the canf( ) function we can pecif- the delimite# in b#ac7et afte# the cha#acte#.We have pecified 2Jn2 a the delimite#. 5ence canf( ) te#minate onl- when the ue#hit Inte# 7e-.QQ.
5ow to et the -tem date th#ough a > p#og#am ?
n* We can et the -tem date uing the etdate( ) function a hown in thefollowing p#og#am. +he function aign the cu##ent time to at#uctu#e date.
Dinclude &tdio.h&Dinclude &do.h&
main( ){t#uct date newTdate !
newTdate.daTmon = 10 !newTdate.daTda- = 19 !newTdate.daT-ea# = 1QQ3 !
etdate ( LnewTdate ) !'
8/11/2019 C-Q & A
http://slidepdf.com/reader/full/c-q-a 62/65
100.
5ow can w#ite a gene#alpu#poe wap without uing template?
n* iven below i the p#og#am which ue the t#ingi/ing p#ep#oceo# di#ectiveDD fo# building a gene#al pu#poe wap mac#o which can wap two intege#, two
float, two cha#, etc.Ddefine wap( a, b, t ) ( g DD t = ( a ), ( a ) = ( b ), ( b ) = g DD t )int gint!cha# gcha#!float gfloat !main( ){int a = 10, b = 0 !cha# ch1 = 2a2 , ch = 2b2 !float f1 = 1.1, f = 3.19 !wap ( a, b, int ) !p#intf ( &Jna = %d b = %d&, a, b ) !
wap ( ch1, ch, cha# ) !p#intf ( &Jnch1 = %c ch = %c&, ch1, ch ) !wap ( f1, f, float ) !p#intf ( &Jnf1 = %9.f f = %9.f&, f1, f ) !'wap ( a, b, int ) would epand to,( gint = ( a ), ( a ) = ( b ), ( b ) = gint )101.What i a heap ?
n * 5eap i a chun7 of memo#-. When in a p#og#am memo#- i allocated
d-namicall-, the > #untime lib#a#- get the memo#- f#om a collection of unuedmemo#- called the heap. +he heap #eide in a p#og#am2 data egment. +he#efo#e,the amount of heap pace available to the p#og#am i fied, and can va#- f#om onep#og#am to anothe#.
10.
5ow to obtain a path of the given file?
n* +he function ea#chpath( ) ea#che fo# the pecified file in the ubdi#ecto#ie of
the cu##ent path. Hollowing p#og#am how how to ma7e ue of the ea#chpath( )function.
Dinclude &di#.h&
void main ( int a#gc, cha# 4a#gv[ ){cha# 4path !if ( path = ea#chpath ( a#gv[ 1 ) )
8/11/2019 C-Q & A
http://slidepdf.com/reader/full/c-q-a 63/65
p#intf ( &Sathname * %Jn&, path ) !elep#intf ( &Hile not foundJn& ) !'
103.
>an we get the p#oce identification numbe# of the cu##ent p#og#am?
n* Ve8 +he mac#o getpid( ) give u the p#oce identification numbe# of thep#og#am cu##entl- #unning. +he p#oce id. uniMuel- identifie a p#og#am. Bnde#:;, the getpid( ) #etu#n the S#og#am egment S#efi a the p#oce id. Hollowingp#og#am illut#ate the ue of thi mac#o.Dinclude Etdio.hFDinclude Ep#oce.hF
void main( )
{p#intf ( &+he p#oce identification numbe# of thi p#og#am i %\Jn&,getpid( ) ) !'
109.
5ow do w#ite a function that ta7e va#iable numbe# of a#gument?
n* +he following p#og#am demont#ate thi.
Dinclude Etdio.hFDinclude Etda#g.hF
void main( ){int i = 10 !float f = .A !cha# 4t# = &5ello8& !
vfpf ( &%d %f %Jn&, i, f, t# ) !vfpf ( &% %&, t#, &5i8& ) !'
void vfpf ( cha# 4fmt, ... ){vaTlit a#gpt# !vaTta#t ( a#gpt#, fmt ) !vfp#intf ( tdout, fmt, a#gpt# ) !
8/11/2019 C-Q & A
http://slidepdf.com/reader/full/c-q-a 64/65
8/11/2019 C-Q & A
http://slidepdf.com/reader/full/c-q-a 65/65
#eponibilit- to deallocate the memo#- uing f#ee( ).Dinclude Etdio.hFDinclude Et#ing.hFDinclude Ealloc.hF
void main( )
{cha# 4t#1, 4t# = &double&!
t#1 = t#dup ( t# ) !p#intf ( &%Jn&, t#1 ) !f#ee ( t#1 ) !'10.
;n including a file twice get e##o# #epo#ting #edefinition of function.
5ow can avoid duplicate incluion?
n* Uedefinition e##o# can be avoided b- uing the following mac#o definition.nclude thi definition in the heade# file.Dif 8defined filenameThDdefine filenameTh K4 function definition 4KDendif Ueplace filenameTh with the actual heade# file name. Ho# eample, if name of file tobe included i 2goto.h2 then #eplace filenameTh with [email protected] to w#ite a wap( ) function which wap the value of the va#iable uing bitwieope#ato#.
n* 5e#e i the wap( ) function.wap ( int 4, int 4- ){4 = 4- !4- = 4 !4 = 4- !'+he wap( ) function ue the bitwie \;U ope#ato# and doe not #eMui#e an-tempo#a#- va#iable fo# wapping.