Vous êtes sur la page 1sur 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 a))igned fi#)t to a[i i.e. a[0 and then
the value of i would get inc#emented b- 1. .ince a[i i.e. a[1 ha) not been
initiali/ed, a[i will have a ga#bage value.
00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00
1.
Wh- doe)n2t the following code give the de)i#ed #e)ult?
int 3 = 3000, - = 1000 !
long int / = 3 4 - !
(n)* 5e#e the multiplication i) ca##ied out between two int) 3 and -, and the #e)ult
that would ove#flow would be t#uncated befo#e being a))igned to the va#iable / of
t-pe long int. 5oweve#, to get the co##ect output, we )hould u)e an e3plicit ca)t to
fo#ce long a#ithmetic a) )hown below*
long int / = ( long int ) 3 4 - !
6ote that ( long int )( 3 4 - ) would not give the de)i#ed effect.
00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00
3.
Wh- doe)n2t 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 ba)ic
diffe#ence between a )t#ing and a cha#acte# i) that a )t#ing i) a collection of
cha#acte#), #ep#e)ented b- an a##a- of cha#acte#) whe#ea) a cha#acte# i) a )ingle
cha#acte#. +o ma7e the above )tatement wo#7 w#ite) the )tatement a) )hown below*
)t#cat ( )t#, &8& ) !
00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00
9.
5ow do , 7now how man- element) an a##a- can hold?
(n)* +he amount of memo#- an a##a- can con)ume depend) on the data t-pe of an
a##a-. ,n :;. envi#onment, the amount of memo#- an a##a- can con)ume depend)
on the cu##ent memo#- model (i.e. +in-, .mall, <a#ge, 5uge, etc.). ,n gene#al an
a##a- cannot con)ume mo#e than =9 7b. >on)ide# following p#og#am, which )how)
the ma3imum numbe# of element) an a##a- of t-pe int, float and cha# can have in
ca)e of .mall memo#- model.
main( )
{
int i[31?=? !
float f[1=3@3 !
cha# )[=AA3A !
'
00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00
A.
5ow do , w#ite code that #ead) data at memo#- location )pecified b- )egment and
off)et?
(n)* B)e pee7b( ) function. +hi) function #etu#n) b-te()) #ead f#om )pecific )egment
and off)et location) in memo#-. +he following p#og#am illu)t#ate) u)e of thi) function.
,n thi) p#og#am f#om C:B memo#- we have #ead cha#acte#) and it) att#ibute) of the
fi#)t #ow. +he info#mation )to#ed in file i) then fu#the# #ead and di)pla-ed u)ing pee7(
) function.
Dinclude E)tdio.hF
Dinclude Edo).hF
main( )
{
cha# fa# 4)c# = 03G@000000 !
H,<I 4fp !
int off)et !
cha# ch !
if ( ( fp = fopen ( &)c#.dat&, &wb& ) ) == 6B<< )
{
p#intf ( &JnBnable to open file& ) !
e3it( ) !
'
KK #ead) and w#ite) to file
fo# ( off)et = 0 ! off)et E 1=0 ! off)et"" )
fp#intf ( fp, &%c&, pee7b ( )c#, off)et ) ) !
fclo)e ( fp ) !
if ( ( fp = fopen ( &)c#.dat&, &#b& ) ) == 6B<< )
{
p#intf ( &JnBnable to open file& ) !
e3it( ) !
'
KK #ead) and w#ite) to file
fo# ( off)et = 0 ! off)et E 1=0 ! off)et"" )
{
f)canf ( fp, &%c&, Lch ) !
p#intf ( &%c&, ch ) !
'
fclo)e ( 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) containing
)t#ing). ,n )uch a )ituation we can u)e function) li7e memcmp( ) o# memicmp( ). +he
ba)ic diffe#ence between two function) i) that memcmp( ) doe) a ca)e0)en)itive
compa#i)on whe#ea) memicmp( ) igno#e) ca)e of cha#acte#). Hollowing p#og#am
illu)t#ate) the u)e of both the function).
Dinclude Emem.hF
main( )
{
cha# 4a##1 = &Nicit& !
cha# 4a##1 = &7icit6agpu#& !
int c !
c = memcmp ( a##1, a##1, )i/eof ( a##1 ) ) !
if ( c == 0 )
p#intf ( &Jn.t#ing) a##1 and a##1 compa#ed u)ing memcmp a#e identical& ) !
el)e
p#intf ( &Jn.t#ing) a##1 and a##1 compa#ed u)ing memcmp a#e not identical&
) !
c = memicmp ( a##1, a##1, )i/eof ( a##1 ) ) !
if ( c == 0 )
p#intf ( &Jn.t#ing) a##1 and a##1 compa#ed u)ing memicmp a#e identical& )
!
el)e
p#intf ( &Jn.t#ing) a##1 and a##1 compa#ed u)ing memicmp a#e not
identical& ) !
'
00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00
?.
Hi3ed0)i/e obOect) a#e mo#e app#op#iate a) compa#ed to va#iable )i/e data obOect).
B)ing va#iable0)i/e data obOect) )ave) ve#- little )pace. Ca#iable )i/e data obOect)
u)uall- have )ome ove#head. Panipulation of fi3ed0)i/e data obOect) i) u)uall- fa)te#
and ea)ie#. B)e fi3ed )i/e when ma3imum )i/e i) clea#l- bounded and clo)e to
ave#age. (nd u)e va#iable0)i/e data obOect) when a few of the data item) a#e bigge#
than the ave#age )i/e. Ho# e3ample,
cha# 4num[10 = { &;ne&, &+wo&, &+h#ee&, &Hou#&,
&Hive&, &.i3&, &.even&, &Iight&, &6ine&, &+en& ' !
,n)tead of u)ing the above, u)e
cha# num[10[= = { &;ne&, &+wo&, &+h#ee&, &Hou#&,
&Hive&, &.i3&, &.even&, &Iight&, &6ine&, &+en& ' !
+he fi#)t fo#m u)e) va#iable0)i/e data obOect). ,t allocate) 10 pointe#), which a#e
pointing to 10 )t#ing con)tant) 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 u)e) fi3ed )i/e data
obOect). ,t allocate) 10 a##a-) of = cha#acte#) each. ,t #eMui#e) onl- =0 b-te) of
)pace. .o, the va#iable0)i/e in thi) ca)e doe) not offe# an- advantage ove# fi3ed )i/e.
00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00
@.
+he .pawnl( ) function...
:;. i) a )ingle ta)7ing ope#ating )-)tem, thu) onl- one p#og#am #un) at a time. +he
.pawnl( ) function p#ovide) u) with the capabilit- of )ta#ting the e3ecution of one
p#og#am f#om within anothe# p#og#am. +he fi#)t p#og#am i) called the pa#ent p#oce))
and the )econd p#og#am that get) called f#om within the fi#)t p#og#am i) called a
child p#oce)). ;nce the )econd p#og#am )ta#t) e3ecution, the fi#)t i) put on hold until
the )econd p#og#am complete) e3ecution. +he fi#)t p#og#am i) then #e)ta#ted. +he
following p#og#am demon)t#ate) u)e of )pawnl( ) function.
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)...& ) !
e3it ( 0 ) !
'
fo# ( i = 1 ! i E a#gc ! i"" )
a[i = atoi ( a#gv[i ) !
#et = a[1 4 a[1 !
#etu#n #et !
'
K4 .pawn.c 4K
Dinclude Ep#oce)).hF
Dinclude E)tdio.hF
main( )
{
int val !
val = )pawnl ( STW(,+, &>*JJPult.e3e&, &3&, &10&,
&10&, 6B<< ) !
p#intf ( &JnUetu#ned value i)* %d&, val ) !
'
5e#e, the#e a#e two p#og#am). +he p#og#am 2Pult.e3e2 wo#7) a) a child p#oce))
whe#ea) 2.pawn.e3e2 wo#7) a) a pa#ent p#oce)). ;n e3ecution of 2.pawn.e3e2 it
invo7e) 2Pult.e3e2 and pa))e) the command0line a#gument) to it. 2Pult.e3e2 in tu#n
on e3ecution, calculate) the p#oduct of 10 and 10 and #etu#n) the value to val in
2.pawn.e3e2. ,n ou# call to )pawnl( ) function, we have pa))ed = pa#amete#), STW(,+
a) the mode of e3ecution, path of 2.e3e2 file to #un a) child p#oce)), total numbe# of
a#gument) to be pa))ed to the child p#oce)), li)t of command line a#gument) and
6B<<. STW(,+ will cau)e ou# application to f#ee/e e3ecution until the child p#oce))
ha) completed it) e3ecution. +hi) pa#amete# need) to be pa))ed a) the default
pa#amete# if -ou a#e wo#7ing unde# :;.. unde# othe# ope#ating )-)tem) that
)uppo#t multita)7ing, thi) pa#amete# can be ST6;W(,+ o# ST;CIU<(V. ST6;W(,+
will cau)e the pa#ent p#oce)) to e3ecute along with the child p#oce)), ST;CIU<(V will
load the child p#oce)) on top of the pa#ent p#oce)) in the memo#-.
00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00
Q.
(#e the following two )tatement) identical?
cha# )t#[= = &Nicit& !
cha# 4)t# = &Nicit& !
(n)* 6o8 (##a-) a#e not pointe#). (n a##a- i) a )ingle, p#e0allocated chun7 of
contiguou) element) (all of the )ame t-pe), fi3ed in )i/e and location. ( pointe# on
the othe# hand, i) a #efe#ence to an- data element (of a pa#ticula# t-pe) located
an-whe#e. ( pointe# mu)t be a))igned to point to )pace allocated el)ewhe#e, but it
can be #ea))igned an- time. +he a##a- decla#ation cha# )t#[= ! #eMue)t) that )pace
fo# = cha#acte#) be )et a)ide, to be 7nown
b- name )t#. ,n othe# wo#d) the#e i) a location named )t# at which )i3 cha#acte#) a#e
)to#ed. +he pointe# decla#ation cha# 4)t# ! on the othe# hand, #eMue)t) a place that
hold) a pointe#, to be 7nown b- the name )t#. +hi) pointe# can point almo)t an-whe#e
to an- cha#, to an- contiguou) a##a- of cha#), o# nowhe#e.
00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00
10.
,) the following code f#agment co##ect?
con)t int 3 = 10 !
int a##[3 !
(n)* 6o8 5e#e, the va#iable 3 i) fi#)t decla#ed a) an int )o memo#- i) #e)e#ved fo# it.
+hen it i) Mualified b- a con)t Mualifie#. 5ence, con)t Mualified obOect i) not a con)tant
full-. ,t i) an obOect with #ead onl- att#ibute, and in >, an obOect a))ociated with
memo#- cannot be u)ed in a##a- dimen)ion).
11.
5ow do , w#ite code to #et#ieve cu##ent date and time f#om the )-)tem and di)pla- it
a) a )t#ing?
(n)* B)e time( ) function to get cu##ent date and time and then ctime( ) function to
di)pla- 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 L
)to#e) in cu#time
p#intf ( &Jn>u##ent :ate L +ime* %)&, ctime (
Lcu#time ) ) !
'
00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00
11.
5ow do , change the t-pe of cu#)o# and hide a cu#)o#?
(n)* We can change the cu#)o# t-pe b- u)ing function T)etcu#)o#t-pe( ). +hi)
function 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#.
Dinclude Econio.hF
main( )
{
K4 5ide cu#)o# 4K
T)etcu#)o#t-pe ( T6;>BU.;U ) !
K4 >hange cu#)o# to a )olid cu#)o# 4K
T)etcu#)o#t-pe ( T.;<,:>BU.;U ) !
K4 >hange bac7 to the no#mal cu#)o# 4K
T)etcu#)o#t-pe ( T6;UP(<>BU.;U ) !
'
00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00
13.
5ow do , w#ite code that would get e##o# numbe# and di)pla- e##o# me))age if an-
)tanda#d e##o# occu#)?
(n)* Hollowing code demon)t#ate) thi).
Dinclude E)tdio.hF
Dinclude E)tdlib.hF
Dinclude Ee##no.hF
main( )
{
cha# 4e##m)g !
H,<I 4fp !
fp = fopen ( &>*Jfile.t3t&, &#& ) !
if ( fp == 6B<< )
{
e##m)g = )t#e##o# ( e##no ) !
p#intf ( &Jn%)&, e##m)g ) !
'
'
5e#e, we a#e t#-ing to open 2file.t3t2 file. 5oweve#, if the file doe) not e3i)t, then it
would cau)e an e##o#. () a #e)ult, a value (in thi) ca)e 1) #elated to the e##o#
gene#ated would get )et in e##no. e##no i) an e3te#nal int va#iable decla#ed in
2)tdlib.h2 and al)o in 2e##no.h2. 6e3t, we have called )te##o#( ) function which ta7e) an
e##o# numbe# and #etu#n) a pointe# to )tanda#d e##o# me))age #elated to the given
e##o# numbe#.
00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00
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 getdi)7( ) #etu#n) the d#ive numbe# of cu##ent d#ive. +he d#ive
numbe# 0 indicate) 2(2 a) the cu##ent d#ive, 1 a) 2G2 and )o on. +he .etdi)7( )
function )et) the cu##ent d#ive. +hi) function ta7e) one a#gument which i) an intege#
indicating the d#ive to be )et. Hollowing p#og#am demon)t#ate) u)e of both the
function).
Dinclude Edi#.hF
main( )
{
int dno, ma3d# !
dno = getdi)7( ) !
p#intf ( &Jn+he cu##ent d#ive i)* %cJn&, =A " dno
) !
ma3d# = )etdi)7 ( 3 ) !
dno = getdi)7( ) !
p#intf ( &Jn6ow the cu##ent d#ive i)* %cJn&, =A "
dno ) !
'
00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00
1A.
+he function) memcmp( ) and memicmp( )
+he function) memcmp( ) and memicmp( ) compa#e) fi#)t n b-te) of given two
bloc7) of memo#- o# )t#ing). 5oweve#, memcmp( ) pe#fo#m) compa#i)on a) un)igned
cha#) whe#ea) memicmp( ) pe#fo#m) compa#i)on a) cha#) but igno#e) ca)e (i.e.
uppe# o# lowe# ca)e). Goth the function) #etu#n an intege# value whe#e 0 indicate)
that 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 following
code )nippet demon)t#ate) u)e of both
Dinclude E)tdio.hF
Dinclude Emem.hF
main( )
{
cha# )t#1[ = &+hi) )t#ing contain) )ome
cha#acte#)& !
cha# )t#1[ = &thi) )t#ing contain)& !
int #e)ult !
#e)ult = memcmp ( )t#1, )t#1, )t#len ( )t#1 ) ) !
p#intf ( &JnUe)ult afte# comap#ing buffe# u)ing
memcmp( )& ) !
)how ( #e)ult ) !
#e)ult = memicmp ( )t#1, )t#1, )t#len ( )t#1 ) ) !
p#intf ( &JnUe)ult afte# comap#ing buffe# u)ing
memicmp( )& ) !
)how ( #e)ult ) !
'
)how ( int # )
{
if ( # == 0 )
p#intf ( &Jn+he buffe# )t#1 and )t#1 hold
identical data& ) !
if ( # F 0 )
p#intf ( &Jn+he buffe# )t#1 i) bigge# than buffe#
)t#1& ) !
if ( # E 0 )
p#intf ( &Jn+he buffe# )t#1 i) le)) than buffe#
)t#1& ) !
'
00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00
1=.
5ow do , w#ite code to find an amount of f#ee di)7 )pace available on cu##ent d#ive?
(n)* B)e getdf#ee( ) function a) )hown in follow code.
Dinclude E)tdio.hF
Dinclude E)tdlib.hF
Dinclude Edi#.hF
Dinclude Edo).hF
main( )
{
int d# ! )t#uct df#ee di)7 !
long f#ee)p !
d# = getdi)7( ) !
getdf#ee ( d# " 1 , Ldi)7 ) !
if ( di)7.dfT)clu) == 03HHHH )
{
p#intf ( &Jngetdf#ee( ) function failedJn&)!
e3it ( 1 ) !
'
f#ee)p = ( long ) di)7.dfTavail
4 ( long ) di)7.dfTb)ec
4 ( long ) di)7.dfT)clu) !
p#intf ( &Jn+he cu##ent d#ive %c* ha) %ld b-te)
available a) f#ee )paceJn&, 2(2 " d#, f#ee)p ) !
'
1?.
B)e of a##a- indice)...
,f we wi)h to )to#e a cha#acte# in a cha# va#iable ch and the cha#acte# to be )to#ed
depend) on the value of anothe# va#iable )a- colo# (of t-pe int), then the code would
be a) )hown below*
)witch ( colo# )
{
ca)e 0 *
ch = 2U2 !
b#ea7 !
ca)e 1 *
ch = 2W2 !
b#ea7 !
ca)e 1 *
ch = 2G2 !
b#ea7 !
'
,n place of )witch0ca)e we can ma7e u)e of the value in colo# a) an inde3 fo# a
cha#acte# a##a-. 5ow to do thi) i) )hown in following code )nippet.
cha# 4)t# = &UWG2 !
cha# ch !
int colo# !
KK code
ch = )t#[ colo# !
00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00
1@.
Hunction ate3it( ) #ecevie) pa#amete# a) the add#e)) of function of the t-pe void fun
( void ). +he function who)e add#e)) i) pa))ed to ate3it( ) get) called befo#e the
te#mination of p#og#am. ,f ate3it( ) i) called fo# mo#e than one function then the
function) a#e called in &fi#)t in la)t out& o#de#. Vou can ve#if- that f#om the output.
Dinclude E)tdio.hF
Dinclude E)tdlib.hF
void fun1( )
{
p#intf(&,n)ide fun1Jn&)!
'
void fun1( )
{
p#intf(&,n)ide fun1Jn&)!
'
main( )
{
ate3it ( fun1 ) !
K4 )ome code 4K
ate3it ( fun1 ) !
p#intf ( &+hi) i) the la)t )tatement of
p#og#am?Jn& )!
'
00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00
1Q.
5ow do , w#ite a u)e#0defined function, which delete) each cha#acte# in a )t#ing )t#1,
which matche) an- cha#acte# in )t#ing )t#1?
(n)* +he function i) a) )hown below*
>omp#e)) ( cha# )t#1[, cha# )t#1[ )
{
int i, O, 7 !
fo# ( i = 7 = 0 ! )t#1[i 8= XJ0Y ! i"" )
{
fo# ( O = 0 ! )t#1[O 8= XJ0Y LL )t#1[O 8=
)t#1[i ! O"" )
!
if ( )t#1[O == XJ0Y )
)t#1[7"" = )t#1[, !
'
)t#1[7 = XJ0Y
'
00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00
10.
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 bloc7
allocated and #etu#ned, )o it i) not nece))a#- to #emind it of the )i/e when f#eeing.
00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00
11.
What i) the u)e of #andomi/e( ) and )#and( ) function?
(n)* While gene#ating #andom numbe#) in a p#og#am, )ometime) we #eMui#e to
cont#ol the )e#ie) of numbe#) that #andom numbe# gene#ato# c#eate). +he p#oce)) of
a))igning the #andom numbe# gene#ato#) )ta#ting numbe# i) called )eeding the
gene#ato#. +he #andomi/e( ) and )#and( ) function) a#e u)ed to )eed the #andom
numbe# gene#ato#). +he #andomi/e( ) function u)e) S>2) cloc7 to p#oduce a #andom
)eed, whe#ea) the )#and( ) function allow) u) to )pecif- the #andom numbe#
gene#ato#2) )ta#ting value.
00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00
11.
5ow do , dete#mine amount of memo#- cu##entl- available fo# allocating?
(n)* We can u)e function co#eleft( ) to get the amount of memo#- available fo#
allocation. 5oweve#, thi) function doe) not give an e3act amount of unu)ed memo#-.
,f, we a#e u)ing a )mall memo#- model, co#eleft( ) #etu#n) the amount of unu)ed
memo#- between the top of the heap and )tac7. ,f we a#e u)ing a la#ge# model, thi)
function #etu#n) the amount of memo#- between the highe)t allocated memo#- and
the end of conventional memo#-. +he function #etu#n) amount of memo#- in te#m) of
b-te).
00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00
13.
5ow doe) a > p#og#am come to 7now about command line a#gument)?
(n)* When we e3ecute ou# > p#og#am, ope#ating )-)tem load) the p#og#am into
memo#-. ,n ca)e of :;., it fi#)t load) 1A= b-te) into memo#-, called p#og#am
)egment p#efi3. +hi) contain) file table, envi#onment )egment, and command line
info#mation. When we compile the > p#og#am the compile# in)e#t) additional code
that pa#)e) the command, a))igning it to the a#gv a##a-, ma7ing the a#gument)
ea)il- acce))ible within ou# > p#og#am.
19.
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 u)ing 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 po)ition pointe#, which 7eep) t#ac7 of cu##ent location within the file. ;n
opening 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 po)ition pointe# advance) one cha#acte#. ,f
we #ead one line of te3t at a )tep f#om the file, then file pointe# advance) to the )ta#t
of the ne3t line. ,f the file i) opened in append mode, the file pointe# i) placed at the
ve#- end of the file. B)ing f)ee7( ) function we can )et the file pointe# to )ome othe#
place within the file.
00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00

1A.
+he )i/eof( ) function doe)nYt #etu#n the )i/e of the bloc7 of memo#- pointed to b- a
pointe#. Wh-?
(n)* +he )i/eof( ) ope#ato# doe) not 7now that malloc( ) ha) been u)ed to allocate a
pointe#. )i/eof( ) give) u) the )i/e of pointe# it)elf. +he#e i) no hand- wa- to find out
the )i/e of a bloc7 allocated b- malloc( ).
00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00
1=.
HST.IW (nd HST;HHZ
.ometime) while wo#7ing with fa# pointe#) we need to b#ea7 a fa# add#e)) into it)
)egment and off)et. ,n )uch )ituation) we can u)e HST.IW and HST;HH mac#o).
Hollowing p#og#am illu)t#ate) the u)e of the)e two mac#o).
Dinclude Edo).hF
main( )
{
un)igned ), o !
cha# fa# 4pt# = &5ello8& !
) = HST.IW ( pt# ) !
o = HST;HH ( pt# ) !
p#intf ( &Jn%u %u&, ), o ) !
'
00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00
1?.
5ow do , w#ite a p#og#am to conve#t a )t#ing containing numbe# in a he3adecimal
fo#m to it) eMuivalent decimal?
(n)* +he following p#og#am demon)t#ate) thi)*
main( )
{
cha# )t#[ = &0(G& !
int h, he3, i, n !
n = 0 ! h = 1 !
fo# ( i = 0 ! h == 1 ! i"" )
{
if ( )t#[i F= 202 LL )t#[i E= 2Q2 )
he3 = )t#[i 0 202 !
el)e
{
if ( )t#[i F= 2a2 LL )t#[i E= 2f2 )
he3 = )t#[i 0 2a2 " 10 !
el)e
if ( )t#[i F= 2(2 LL )t#[i E= 2H2 )
he3 = )t#[i 0 2(2 " 10 !
el)e
h = 0 !
'
if ( h == 1 )
n = 1= 4 n " he3 !
'
p#intf ( &Jn+he decimal eMuivalent of %) i) %d&,
)t#, n ) !
'
+he output of thi) p#og#am would be the decimal eMuivalent of 0(G i) 1?1.
00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00
1@.
5ow do , w#ite code that #ead) the )egment #egi)te# )etting)?
(n)* We can u)e )eg#ead( ) function to #ead )egment #egi)te# )etting). +he#e a#e
fou# )egment #egi)te#)[code )egment, data )egment, )tac7 )egment and e3t#a
)egment. .ometime) when we u)e :;. and G,;. )e#vice) in a p#og#am we need to
7now the )egment #egi)te#2) value. ,n )uch a )ituation we can u)e )eg#ead( )
function. +he following p#og#am illu)t#ate) the u)e of thi) function.
Dinclude Edo).hF
main( )
{
)t#uct .UIW. ) !
)eg#ead ( L) ) !
p#intf ( &Jn>.* %\ :.* %\ ..* %\ I.* %\&,).c),
).d), ).)), ).e) ) !
'
00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00
1Q.
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 called
envi#onment. ,n thi) #egion we can place configu#ation )etting) )uch a) command
path, )-)tem p#ompt, etc. .ometime) in a p#og#am we need to acce)) the
info#mation contained in envi#onment. +he function getenv( ) can be u)ed when we
want to acce)) envi#onment fo# a )pecific ent#-. Hollowing p#og#am demon)t#ate) the
u)e of thi) function.
Dinclude E)tdio.hF
Dinclude E)tdlib.hF
main( )
{
cha# 4path = 6B<< !
path = getenv ( &S(+5& ) !
if ( 4path 8= 6B<< )
p#intf ( &JnSath* %)&, path ) !
el)e
p#intf ( &JnSath i) not )et& ) !
'
00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00
30.
5ow do , di)pla- cu##ent date in the fo#mat given below?
.atu#da- ;ctobe# 11, 1001
(n)* Hollowing p#og#am illu)t#ate) how we can di)pla- date in above given fo#mat.
Dinclude E)tdio.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), elap)ed )ince 00*00*00 WP+, ]anua#- 1, 1Q?0. +o
e3t#act the wee7 da-, da- of month, etc. f#om thi) value we need to b#ea7 down the
value to a tm )t#uctu#e. +hi) i) done b- the function localtime( ). +hen we have
called )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 u)ing it in anothe# file
then wh- doe)n2t the )i/eof ope#ato# wo#7) on an e3te#n a##a-?
(n)* (n e3te#n a##a- i) of incomplete t-pe a) it doe) not contain the )i/e. 5ence we
cannot u)e )i/eof ope#ato#, a) it cannot get the )i/e of the a##a- decla#ed in anothe#
file. +o #e)olve thi) u)e 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#
e3ample,
a##a-.c
int a##[A !
int a##)/ = )i/eof ( a## ) !
m-p#og.c
e3te#n int a##[ !
e3te#n int a##)/ !
1. :efine a mac#o which can be u)ed in an a##a-
decla#ation. Ho# e3ample,
m-heade#.h
Ddefine .^ A
a##a-.c
Dinclude &m-heade#.h&
int a##[.^ !
m-p#og.c
Dinclude &m-heade#.h&
e3te#n int a##[.^ !
00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00
31.
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# the
numbe# 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 li)t )hould be u)ed fo# the field width.
00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00
33.
5ow to find the #ow and column dimen)ion of a given 10: a##a-?
(n)* Wheneve# we initiali/e a 10: a##a- at the )ame place whe#e it ha) been
decla#ed, it i) not nece))a#- to mention the #ow dimen)ion of an a##a-. +he #ow and
column dimen)ion) of )uch an a##a- can be dete#mined p#og#ammaticall- a) )hown
in following p#og#am.
void main( )
{
int a[[3 = { 0, 1, 1,
Q,0=, @,
?, A, 99,
13, 11,1A ' !
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& ) !
'
'
00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00
39.
+he acce))( ) function...
+he acce))( ) function chec7) fo# the e3i)tence of a file and al)o dete#mine) whethe#
it can be #ead, w#itten to o# e3ecuted. +hi) function ta7e) two a#gument) the
filename and an intege# indicating the acce)) mode. +he value) =, 9, 1, and 1 chec7)
fo# #eadKw#ite, #ead, w#ite and e3ecute pe#mi))ion of a given file, whe#ea) value 0
chec7) whethe# the file e3i)t) o# not. Hollowing p#og#am demon)t#ate) how we can
u)e acce))( ) function to chec7 if a given file e3i)t).
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 e3i)t.& ) !
#etu#n !
'
'
00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00
3A.
5ow do , conve#t a floating0point numbe# to a )t#ing?
(n)* B)e function gcvt( ) to conve#t a floating0point numbe# to a )t#ing. Hollowing
p#og#am demon)t#ate) the u)e of thi) function.
Dinclude E)tdlib.hF
main( )
{
cha# )t#[1A !
float no !
int dg = A ! K4 )ignificant digit) 4K
no = 19.311= !
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#e
data a) the- e3ecute. Ho# e3ample, when a p#og#am pa))e) pa#amete#) to function),
> place) the pa#amete#) on the )tac7. When the function complete), > #emove) the
item) 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) e3ecution. :epending on the
p#og#am2) u)e of function) and pa#amete#), the amount of )tac7 )pace that a
p#og#am #eMui#e) will diffe#.
00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00
3?.
(llocating memo#- fo# a 30: a##a-
Dinclude &alloc.h&
Ddefine P(\\ 3
Ddefine P(\V 9
Ddefine P(\^ A
main( )
{
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& ) !
'
'
:ata .t#uctu#e)
5ow to di)tingui)h 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#ee
)t#uctu#e in which an- node can have at mo)t two b#anche). Ho# bina#- t#ee) we
di)tingui)h 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.
>on)ide# the following figu#e...
+hi) above figu#e )how) two bina#- t#ee), but the)e bina#- t#ee) a#e diffe#ent. +he
fi#)t ha) an empt- #ight )ubt#ee while the )econd ha) an empt- left )ubt#ee. ,f the
above a#e #ega#ded a) t#ee) (not the bina#- t#ee)), then the- a#e )ame de)pite the
fact that the- a#e d#awn diffe#entl-. (l)o, an empt- bina#- t#ee can e3i)t, but the#e i)
no t#ee having /e#o node).
00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00
3@.
5ow do , u)e the function lde3p( ) in a p#og#am?
(n)* +he math function lde3p( ) i) u)ed while )olving the comple3 mathematical
eMuation). +hi) function ta7e) two a#gument), a double value and an int #e)pectivel-.
+he o#de# in which lde3p( ) function pe#fo#m) calculation) i) ( n 4 pow ( 1, e3p ) )
whe#e n i) the double value and e3p i) the intege#. +he following p#og#am
demon)t#ate) the u)e of thi) function.
Dinclude E)tdio.hF
Dinclude Emath.hF
void main( )
{
double an) !
double n = 9 !
an) = lde3p ( n, 1 ) !
p#intf ( &Jn+he lde3p value i) * %lfJn&, an) ) !
'
5e#e, lde3p( ) function would get e3panded a) ( 9 4 1 4 1 ), and the output would be
the lde3p value i) * 1=.000000
00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00
3Q.
>an we get the manti))a and e3ponent fo#m of a given numbe#?
(n)* +he function f#e3p( ) )plit) the given numbe# into a manti))a and e3ponent
fo#m. +he function ta7e) two a#gument), the numbe# to be conve#ted a) a double
value and an int to )to#e the e3ponent fo#m. +he function #etu#n) the manti))a pa#t
a) a double value. Hollowing e3ample demon)t#ate) the u)e of thi) function.
Dinclude Emath.hF
Dinclude E)tdio.hF
void main( )
{
double manti))a, numbe# !
int e3ponent !
numbe# = @.0 !
manti))a = f#e3p ( numbe#, Le3ponent ) !
p#intf ( &+he numbe# %lf i) &, numbe# ) !
p#intf ( &%lf time) two to the &, manti))a ) !
p#intf ( &powe# of %dJn&, e3ponent ) !
#etu#n 0 !
'
00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00
90.
5ow do , w#ite code that e3ecute) ce#tain function onl- at p#og#am te#mination?
(n)* B)e ate3it( ) function a) )hown in following p#og#am.
Dinclude E)tdlib.hF
main( )
{
int ch !
void fun ( void ) !
ate3it ( fun ) !
KK code
'
void fun( void )
{
p#intf ( &Jn+e#minate p#og#am......& ) !
getch( ) !
'
00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00
91.
What a#e memo#- model)?
(n)* +he compile# u)e) a memo#- model to dete#mine how much memo#- i)
allocated to the p#og#am. +he S> divide) memo#- into bloc7) called )egment) of )i/e
=9 NG. B)uall-, p#og#am u)e) one )egment fo# code and a )econd )egment fo# data.
( memo#- model define) the numbe# of )egment) the compile# can u)e fo# each. ,t
i) impo#tant to 7now which memo#- model can be u)ed fo# a p#og#am. ,f we u)e
w#ong memo#- model, the p#og#am might not have enough memo#- to e3ecute. +he
p#oblem can be )olved u)ing la#ge# memo#- model. 5oweve#, la#ge# the memo#-
model, )lowe# i) -ou# p#og#am e3ecution. .o we mu)t choo)e the )malle)t memo#-
model that )ati)fie) ou# p#og#am need). Po)t of the compile#) )uppo#t memo#-
model) li7e tin-, )mall, medium, compact, la#ge and huge.
91.
5ow doe) > compile# )to#e element) in a multi0dimen)ional a##a-?
(n)* +he compile# map) multi0dimen)ional 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 it
i) called column0maOo# o#de#. When the compile# place) element) in #ow) of an a##a-
fi#)t then it i) called #ow0maOo# o#de#. > compile#) )to#e multidimen)ional a##a-) in
#ow0maOo# o#de#. Ho# e3ample, if the#e i) a multi0dimen)ional a##a- a[1[3, then
acco#ding #ow0maOo# o#de#, the element) would get )to#ed in memo#- following
o#de#*
a[0[0, a[0[1, a[0[1, a[1[0, a[1[1, a[1[1
00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00
93.
,f the #e)ult of an Te3p#e))ion ha) to be )to#ed to one of two va#iable), depending on
a condition, can we u)e conditional ope#ato#) a) )hown below?
( ( i E 10 ) ? O * 7 ) = l 4 1 " p !
(n)* 6o8 +he above )tatement i) invalid. We cannot u)e the conditional ope#ato#) in
thi) fa)hion. +he conditional ope#ato#) li7e mo)t ope#ato#), -ield) a value, and we
cannot a))ign the value of an Te3p#e))ion to a value. 5oweve#, we can u)e
conditional ope#ato#) a) )hown in following code )nippet.
main( )
{
int i, O, 7, l !
i = A ! O = 10 ! 7 = 11, l = 1 !
4 ( ( i E 10 ) ? LO * L7 ) = l 4 1 " 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 = 11 l = 1
00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00
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 given
date.
da-ofwee7 ( int --, int mm, int dd )
{
K4Ponda- = 1 and .unda- = 0 4K
K4 month numbe# F= 1 and E= 11, -- F 1?A1 o# )o 4K
)tatic int a##[ = { 0, 3, 1, A, 0, 3, A, 1, 9, =, 1, 9 ' !
-- = -- 0 mm E 3 !
#etu#n ( -- " -- K 9 0 -- K 100 " -- K 900 " a##[ mm 0 1 " dd ) % ? !
'
void main( )
{
p#intf ( &JnJnJn:a- of wee7 * %d &, da-ofwee7 ( 1001, A, 1@ ) ) !
'
00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00


9A.
What2) the diffe#ence between the)e two decla#ation)?
)t#uct )t#1 { ... ' !
t-pedef )t#uct { ... ' )t#1 !
(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 ab)t#act t-pe
00 it) u)e#) don2t nece))a#il- 7now that it i) a )t#uctu#e, and the 7e-wo#d )t#uct i) not
u)ed when decla#ing in)tance) of it.
00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00
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#) to
the )t#ing). Iach pointe# point) to an envi#onment va#iable f#om the li)t of
envi#onment va#iable).
00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00
9?.
div( )...
+he function div( ) divide) two intege#) and #etu#n) the Muotient and #emainde#. +hi)
function ta7e) two intege# value) a) a#gument)! divide) fi#)t intege# with the )econd
one and #etu#n) the an)we# of divi)ion of t-pe divTt. +he data t-pe divTt i) a
)t#uctu#e that contain) two long int), namel- Muot and #em, which )to#e Muotient and
#emainde# of divi)ion #e)pectivel-. +he following e3ample )how) the u)e of div( )
function.
Dinclude E)tdlib.hF
void main( )
{
divTt #e) !
#e) = div ( 31, A ) !
p#intf ( &Jn+he Muotient = %d and #emainde# = %d &, #e).Muot, #e).#em ) !
'
9@.
What would the )econd and the thi#d p#intf( ) output the following p#og#am?
main( )
{
cha# 4)t#[ = {
&Wood Po#ning&
&Wood Ivening&
&Wood (fte#noon&
' !
p#intf ( &JnHi#)t )t#ing = %)&, )t#[0 ) !
p#intf ( &Jn.econd )t#ing = %)&, )t#[1 ) !
p#intf ( &Jn+hi#d )t#ing = %)&, )t#[1 ) !
'
(n)* Ho# the above given p#og#am, we e3pect the output a) Wood Ivening and Wood
(fte#noon, fo# the )econd and thi#d p#intf( ). 5oweve#, the output would be a) )hown
below.
Hi#)t )t#ing = Wood Po#ningWood IveningWood (fte#noon
.econd )t#ing = ( null )
+hi#d )t#ing =
What i) mi))ing in the above given code )nippet i) a comma )epa#ato# which )hould
)epa#ate the )t#ing) Wood Po#ning, Wood Ivening and Wood (fte#noon. ;n adding
comma, we would get the output a) )hown below.
Hi#)t )t#ing = Wood Po#ning
.econd )t#ing = Wood Ivening
+hi#d )t#ing = Wood (fte#noon
00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00
9Q.
5ow do , u)e )canf( ) to #ead the date in the fo#m 2dd0mm0--2 ?
(n)* +he#e a#e two wa-) to #ead the date in the fo#m of 2dd0mm0--2 one po))ible
wa- i)...
int dd, mm, -- !
cha# ch ! K4 fo# cha# 202 4K
p#intf ( &JnInte# the date in the fo#m of dd0mm0-- * & ) !
)canf( &%d%c%d%c%d&, Ldd, Lch, Lmm, Lch, L-- ) !
(nd anothe# be)t wa- i) to u)e )upp#e))ion cha#acte# 4 a)...
int dd, mm, -- !
)canf( &%d%4c%d%4c%d&, Ldd, Lmm, L-- ) !
+he )upp#e))ion cha#acte# 4 )upp#e))e) the input #ead f#om the )tanda#d input
buffe# fo# the a))igned cont#ol cha#acte#.
00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00
A0.
5ow do , p#int a floating0point numbe# with highe# p#eci)ion )a- 13.39A=@?39 with
onl- p#eci)ion up to two decimal place)?
(n)* +hi) can be achieved th#ough the u)e of )upp#e))ion cha# 242 in the fo#mat
)t#ing of p#intf( ) a) )hown in the following p#og#am.
main( )
{
int i = 1 !
float f = 13.39A=@?39 !
p#intf ( &%.4f&, i, f ) !
'
+he output of the above p#og#am would be 13.3A.
00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00
A1.
(#e the e3p#e))ion) 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#.
00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00
A1.
)t#pb#7( )
+he function )t#pb#7( ) ta7e) two )t#ing) a) pa#amete#). ,t )can) the fi#)t )t#ing, to
find, the fi#)t occu##ence of an- cha#acte# appea#ing in the )econd )t#ing. +he
function #etu#n) a pointe# to the fi#)t occu##ence of the cha#acte# it found in the fi#)t
)t#ing. +he following p#og#am demon)t#ate) the u)e of )t#ing function )t#pb#7( ).
Dinclude E)t#ing.hF
main( )
{
cha# 4)t#1 = &5ello8& !
cha# 4)t#1 = &Gette#& !
cha# 4p !
p = )t#pb#7 ( )t#1, )t#1 ) !
if ( p )
p#intf ( &+he fi#)t cha#acte# found in )t#1 i) %c&, 4p ) !
el)e
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 un)igned long intege# value to a )t#ing?
(n)* +he function ultoa( ) can be u)ed to conve#t an un)igned long intege# value to a
)t#ing. +hi) function ta7e) th#ee a#gument), fi#)t the value that i) to be conve#ted,
)econd the ba)e add#e)) of the buffe# in which the conve#ted numbe# ha) to be
)to#ed (with a )t#ing te#minating null cha#acte# 2J02) and the la)t a#gument )pecifie)
the ba)e to be u)ed in conve#ting the value. Hollowing e3ample demon)t#ate) the u)e
of thi) function.
Dinclude E)tdlib.hF
void main( )
{
un)igned long ul = 3139A=?131< !
cha# )t#[1A !
ultoa ( ul, )t#, 10 ) !
p#intf ( &)t# = %) un)igned long = %luJn&, )t#, ul ) !
'
00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00
A9.
ceil( ) and floo#( )
+he math function ceil( ) ta7e) a double value a) an a#gument. +hi) function find)
the )malle)t po))ible 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#ge)t po))ible intege# to which the given double value can be #ounded
down. +he following p#og#am demon)t#ate) the u)e of both the function).
Dinclude Emath.hF
void main( )
{
double no = 193?.131=? !
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?.131=?
+he numbe# #ounded down 193?.00000
+he numbe# #ounded up 193@.00000
00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00
AA.
5ow do , u)e function ecvt( ) in a p#og#am?
(n)* +he function ecvt( ) conve#t) a floating0point 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, the
numbe# of digit) to be conve#ted to )t#ing, and two intege# pointe#). +he two0intege#
pointe# )to#e) the po)ition of the decimal point (#elative to the )t#ing) and the )ign of
the numbe#, #e)pectivel-. ,f the value in a va#iable, u)ed to )to#e )ign i) 0, then the
numbe# i) po)itive and, if it i) non0/e#o, then the numbe# i) negative. +he function
#etu#n) a pointe# to the )t#ing containing digit). Hollowing p#og#am demon)t#ate) the
u)e of thi) function.
Dinclude E)tdlib.hF
main( )
{
cha# 4)t# !
double val !
int dec, )ign !
int ndig = 9 !
val = 11 !
)t# = ecvt ( val, ndig, Ldec, L)ign ) !
p#intf ( &)t#ing = %) dec = %d )ign = %dJn&, )t#, dec, )ign ) !
val = 039A.=? !
ndig = @ !
)t# = ecvt ( val, ndig, Ldec, L)ign ) !
p#intf ( &)t#ing = %) dec = %d )ign = %dJn&, )t#, dec, )ign ) !
KK numbe# with a )cientific notation
val = 3.A9=?11eA !
ndig = A !
)t# = ecvt ( val, ndig, Ldec, L)ign ) !
p#intf ( &)t#ing = %) dec = %d )ign = %dJn&, )t#, dec, )ign ) !
'
+he output of thi) p#og#am would be
)t#ing = 1100 dec = 1 )ign = 0
)t#ing = 39A=?000 dec = 3 )ign = 1
)t#ing = 3A9=? dec = = )ign = 0
00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00
A=.
5ow to #un :,U command p#og#ammaticall-?
(n)* We can u)e the )-)tem( ) function to e3ecute the :,U command along with it)
option). 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 1 )
e3it ( 0 ) !
)p#intf ( )t#, &di# %) %)&, a#gv[1, a#gv[1 ) !
)-)tem ( )t# ) !
'
,f we #un the e3ecutable file of thi) p#og#am at command p#ompt pa))ing 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#-.
00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00
A?.
.uppo)e , have a )t#uctu#e having field) name, age, )ala#- and have pa))ed add#e))
of age to a function fun( ). 5ow , can acce)) the othe# membe# of the )t#uctu#e u)ing
the add#e)) of age?
(n)*
)t#uct emp
{
cha# name[10 !
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 off)et !
off)et = ( cha# 4 ) ( L ( ( )t#uct emp 4 ) 0 ) 0F age ) 0 ( cha# 4 ) ( (
)t#uct emp4 ) 0 ) !
M = ( )t#uct emp 4 ) ( ( cha# 4 ) p 0 off)et ) !
p#intf ( &Jnname* %)&, M 0F name ) !
p#intf ( &Jnage* %d&, M 0F age ) !
p#intf ( &Jn)ala#-* %f&, M 0F )ala#- ) !
'
A@.
5ow to #e)t#ict the p#og#am2) output to a )pecific )c#een #egion?
(n)* ( > function window( ) can be u)ed to #e)t#ict the )c#een output to a )pecific
#egion. +he window( ) function define) a te3t0mode window. +he pa#amete#) pa))ed
to thi) function define) the uppe#0left and lowe#0#ight co#ne# of the #egion within
which -ou want the output. ,n the following p#og#am, the )t#ing 25ello82 get) p#inted
within the )pecified #egion. +o p#int the )t#ing we mu)t u)e cp#intf( ) function which
p#int) di#ectl- on the te3t0mode window.
Dinclude Econio.hF
main( )
{
int i, O !
window ( 10, @, =0, 1? ) !
fo# ( i = 0 ! i E @ ! i"" )
fo# ( O = 0 ! O E 10 ! O"" )
cp#intf ( &5ello8& ) !
'
00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00
AQ.
.ometime) -ou need to p#ompt the u)e# fo# a pa))wo#d. When the u)e# t-pe) in the
pa))wo#d, the cha#acte#) the u)e# ente#) )hould not appea# on the )c#een. (
)tanda#d lib#a#- function getpa))( ) can be u)ed to pe#fo#m )uch function. Pa3imum
numbe# of cha#acte#) that can be ente#ed a) pa))wo#d i) @.
main( )
{
cha# 4pwd !
pwd = getpa)) ( &Inte# Sa))wo#d& ) !
if ( )t#cmp ( pwd, &o#gcit-& ) )
p#intf ( &JnSa))wo#d %) i) inco##ect&, pwd ) !
el)e
p#intf ( &Jn>o##ect Sa))wo#d& ) !
'
00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00
=0.
5ow to obtain the cu##ent d#ive th#ough > ?
(n)* We can u)e the function Tgetd#ive( ) to obtain the cu##ent d#ive. +he Tgetd#ive(
) function u)e) :;. function 0\1Q to get the cu##ent d#ive numbe#
Dinclude Edi#ect.hF
main( )
{
int di)7 !
di)7 = Tgetd#ive( ) " 2(2 0 1 !
p#intf ( &+he cu##ent d#ive i)* %cJn&, di)7 ) !
'
00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00
=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"" )
{
goto3- ( 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"" )
{
goto3- ( 1, 1 ) !
p#intf ( &%d %d&, i, O ) !
'
'
;utput 0F A A
Iven if logic of both the p#og#am) i) )ame the output of the fi#)t p#og#am come) out
to be 100, 100, but of the )econd p#og#am it i) A, A. +he comma ope#ato# pla-) a
vital #ole in)ide the fo# loop. ,t alwa-) con)ide#) the value of the late)t va#iable. .o,
at the time of te)ting the condition in fo# loop, the value of O will be con)ide#ed in the
fi#)t p#og#am and value of i in the )econd.
00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00
=1.
>an we get the 3 and - coo#dinate of the cu##ent cu#)o# po)ition ?
(n) * +he function whe#e3( ) and whe#e-( ) #etu#n) the 30coo#dinate and -0
coo#dinate of the cu##ent cu#)o# po)ition #e)pectivel-. Goth the function) #etu#n an
intege# value. +he value #etu#ned b- whe#e3( ) i) the ho#i/ontal po)ition of cu#)o#
and the value #etu#ned b- whe#e-( ) i) the ve#tical po)ition of the cu#)o#. Hollowing
p#og#am )how) how to u)e the whe#e3( ) and whe#e-( ) function).
Dinclude Econio.hF
main( )
{
p#intf ( &]u)tJn +oJn +e)tJn Whe#eJn the cu#)o#Jn goe)& ) !
p#intf ( &>u##ent location i) \* %d V* %dJn&, whe#e3( ), whe#e-( ) ) !
'
=3.
5ow do , p#og#ammaticall- delete line) in the te3t window?
(n)* While w#iting p#og#am) that pe#fo#m )c#een0ba)ed ,K;, -ou ma- want to0delete
the cu##ent line2) content), moving one line up, all of the output that follow). ,n )uch
ca)e) a function called delline( ) can be u)ed. Hollowing code )nippet illu)t#ate) the
u)e of function delline( ).
Dinclude Econio.hF
main( )
{
int i !
cl#)c#( ) !
fo# ( i = 0! i E= 13! i"" )
p#intf ( &<ine %dJ#Jn&, i ) !
p#intf ( &S#e)) a 7e- to continue * & ) !
getch( ) !
goto3- ( 1, = ) !
fo# ( i = =! i E= 11! i"" )
delline( ) !
getch( ) !
'
00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00
=9.
5ow do , get the time elap)ed between two function call) ?
(n)* +he function difftime( ) find) the diffe#ence between two time). ,t calculate) the
elap)ed time in )econd) and #etu#n) the diffe#ence between two time) a) a double
value.
Dinclude Etime.hF
Dinclude E)tdio.hF
Dinclude Edo).hF
main( )
{
int a[ = { 1, 039, A=, ?@, 111, 33, 0?, 11, 9A, 1Q, = ' !
int ) !
timeTt t1, t1 ! KK timeTt define) the value u)ed fo# time function
) = )i/eof ( a ) K 1 !
t1 = time ( 6B<< ) !
)elT)o#t ( a, ) ) ! KK )o#t a##a- b- )election )o#t
bubT)o#t ( a, ) ) ! KK )o#t a##a- b- bubble )o#t method
t1 = time ( 6B<< ) !
p#intf ( &Jn+he diffe#ence between two function call) i) %f&, difftime (
t1, t1 ) ) !
'
,n the above p#og#am we have called difftime( ) function that #etu#n) the time
elap)ed f#om t1 to t1.
00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00
=A.
5ow do , u)e )wab( ) in m- p#og#am ?
(n)* +he function )wab( ) )wap) the adOacent b-te) of memo#-. ,t copie) the b-te)
f#om )ou#ce )t#ing to the ta#get )t#ing, p#ovided that the numbe# of cha#acte#) in the
)ou#ce )t#ing i) even. While cop-ing, it )wap) the b-te) which a#e then a))igned to
the ta#get )t#ing.
Dinclude E)tdlib.hF
Dinclude E)tdio.hF
Dinclude E)t#ing.hF
main ( )
{
cha# 4)t#1 = &h. ee)ll )n)ia)l not eh e) a) ohe# & !
cha# 4)t#1 !
cl#)c#( ) !
)wab ( )t#1, )t#1, )t#len ( )t#1 ) ) !
p#intf ( &+he ta#get )t#ing i) * %)Jn&, )t#1 ) ! KK output 00 .he )ell)
)nail) on the )ea )ho#e
getch( ) !
'
00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00
==.
+u#bo > p#ovide) va#iou) command line compile# option) which we can u)e th#ough
+>>. +he compile# option) include * di)pla-ing )pecific wa#ning me))age), gene#ating
@0@? ha#dwa#e in)t#uction), u)ing a filename fo# gene#ating a))embl- code, etc.
,n)tead of compile# option) being e3ecuted at command line we can u)e the)e
compile# option) in ou# p#og#am. +hi) can be achieved u)ing Dp#agma option). We
can u)e va#iou) flag) with Dp#agma option) to u)e the compile# option). (ll the)e
flag) a#e available in tu#bo >2) online help.
00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00
=?.
, have an a##a- decla#ed in file 2H1.>2 a),
int a[ = { 1, 1, 3, 9, A, = ' !
and u)ed in the file 2H1.>2 a),
e3te#n int a[ !
,n the file H1.>, wh- )i/eof doe)n2t wo#7 on the a##a- a[ ?
(n)* (n e3te#n a##a- of un)pecified )i/e i) an incomplete t-pe. Vou cannot appl-
)i/eof to it, becau)e )i/eof ope#ate) du#ing compile time and it i) unable to lea#n the
)i/e of an a##a- that i) defined in anothe# file. Vou have th#ee wa-) to #e)olve thi)
p#oblem*
1. ,n file 2H1.>2 define a),
int a[ = { 1, 1, 3, 9, A, = ' !
int )i/eTa = )i/eof ( a ) !
and in file H1.> decla#e a),
e3te#n int a[ !
e3te#n int )i/eTa !
1. ,n file 2H1.52 define,
Ddefine (UUT.,^ =
,n file H1.> decla#e a),
Dinclude &H1.5&
int a[ (UUT.,^ !
and in file H1.> decla#e a),
Dinclude &H1.5&
e3te#n int a[ (UUT.,^ !
3. ,n file 2H1.>2 define a),
int a[ = { 1, 1, 3, 9, A, =, 01 ' !
and in file 2H1.>2 decla#e a),
e3te#n int a[ !
5e#e the element 01 i) u)ed a) a )entinel value, )o the code can
unde#)tand the end without an- e3plicit )i/e.
=@.
5ow to delete a line f#om te3t di)pla-ed on the )c#een?
(n)* .ometime), )peciall- when we a#e c#eating a te3t edito# li7e p#og#am we ma-
wi)h to allow u)e# to delete a line. We can do )o b- u)ing two function) namel-
cl#eol( ) and delline( ). +he cl#eol( ) function delete) the line f#om the cu##ent cu#)o#
po)ition to the end of line. +he delline() function delete) the enti#e line at the cu##ent
cu#)o# po)ition and
move) up the following line. Hollowing p#og#am )how) how to u)e the)e function).
Dinclude Econio.hF
main( )
{
int i !
fo# ( i = 1 ! i E= 10 ! i"" )
p#intf ( &+hi) i) <ine %dJn&, i ) !
getch( ) !
goto3- ( 1, ? ) !
cl#eol( ) !
getch( ) !
goto3- ( 1, 11 ) !
delline( ) !
getch( ) !
'
00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00
=Q.
5ow do , p#og#ammaticall- in)e#t line) in the te3t window?
(n)* We can in)e#t a blan7 line in the te3t window u)ing the in)line( ) function. +hi)
function in)e#t) line at cu##ent cu#)o# po)ition. While doing )o, it )hift) down the line)
that a#e below the newl- in)e#ted line.
Dinclude Econio.hF
void 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# )ea)on. Po)t of the animal) we#e #e)ting inJ#Jn& ) !
p#intf ( &thei# ne)t) a) the#e wa) a heav- )now fall.J#Jn& ) !
p#intf ( &J#JnS#e)) an- 7e- to continue*& ) !
goto3- ( 10, 1 ) !
getch( ) !
in)line( ) !
getch( ) !
'
00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00
?0.
What will be the output of the following p#og#am?
main( )
{
un)igned int num !
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#. We
have u)ed bitwi)e ope#ato#) to get the bina#- numbe#.
00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00
?1.
W#aphic)
Guilding Pou)e >u#)o#)...
,n te3t mode the mou)e cu#)o# appea#) a) a bloc7, whe#ea) in g#aphic) mode it
appea#) a) an a##ow. ,f we wi)h we can change the g#aphic) cu#)o# to an- othe#
)hape the wa- Window) doe). +he mou)e cu#)o# in g#aphic) mode occupie) a 1= b-
1= pi3el bo3. G- highlighting o# dehighlighting )ome of the pi3el) in thi) bo3 we can
get the de)i#ed )hape. Ho# e3ample, the following bit0patte#n can be u)ed to
gene#ate the cu#)o# which loo7) li7e an hou#0gla)).
1111111111111111 0000000000000000
1000000000000001 0000000000000000
1111111111111111 0000000000000000
1000000000000001 0000000000000000
0100000000000010 1000000000000001
0010000000000100 1100000000000011
0000100000010000 1111000000001111
0000001001000000 1111110000111111
0000001001000000 1111110000111111
0000100000010000 1111000000001111
0010000000000100 1100000000000011
0100000000000010 1000000000000001
1000000000000001 0000000000000000
1111111111111111 0000000000000000
1000000000000001 0000000000000000
1111111111111111 0000000000000000
Pou)e pointe# bitmap .c#een Pa)7 the one2) in the mou)e pointe# bitmap indicate
that the pi3el would be d#awn whe#ea) the /e#o) indicate that the pi3el would )tand
e#a)ed. ,t i) impo#tant to note that the mou)e pointe# bit patte#n i) 31 b-te) long.
5oweve#, while actuall- w#iting a p#og#am to change the pointe# )hape we need a =9
b-te bit0map. +hi) p#ovi)ion i) made to en)u#e that when the cu#)o# #eache) a
po)ition on the )c#een whe#e )omething i) al#ead- w#itten o# d#awn onl- that po#tion
)hould get ove#w#itten which i) to be occupied b- the mou)e cu#)o#. ;f the =9 b-te)
the fi#)t 31 b-te) contain a bit ma)7 which i) fi#)t (6:ed with the )c#een image, and
then the )econd 31 b-te) bit ma)7 i) \;Ued with the )c#een image.
+he following p#og#am change) the mou)e cu#)o# in g#aphic) mode to #e)emble an
hou# gla)).
D include &g#aphic).h&
D include &do).h&
union UIW. i, o !
)t#uct .UIW. ) !
int cu#)o#[31 =
{
K4 5ou#0gla)) )c#een ma)7 4K
030000, 030000, 030000, 030000,
03@001, 03c003, 03f00f, 03fc3f,
03fc3f, 03f00f, 03c003, 03@001,
030000, 030000, 030000, 030000,
K4 +he mou)e pointe# bitmap 4K
03ffff, 03@001, 03ffff, 03@001,
039001, 031009, 03100@, 030190,
030190, 030@10, 031009, 039001,
03@001, 03ffff, 03@001, 03ffff,
' !
main( )
{
int gd = :I+I>+, gm !
initg#aph ( Lgd, Lgm, &c*JJtcJJbgi& ) !
if ( initmou)e( ) == 01 )
{
clo)eg#aph( ) !
p#intf ( &Jn Pou)e not in)talled8& ) !
e3it( ) !
'
goto3- ( 10, 1 ) ! p#intf ( &S#e)) an- 7e- to e3it...& ) !
changecu#)o# ( cu#)o# ) ! )howmou)ept#( ) !
getch( ) !
'
initmou)e( )
{
i.3.a3 = 0 ! int@= ( 0333, Li, Lo ) !
#etu#n ( o.3.a3 == 0 ? 01 * 0 ) !
'
)howmou)ept#( )
{
i.3.a3 = 1 ! int@= ( 0333, Li, Lo ) !
'
changecu#)o# ( int 4)hape )
{
i.3.a3 = Q ! K4 )e#vice numbe# 4K
i.3.b3 = 0 ! K4 actual cu#)o# po)ition f#om left 4K
i.3.c3 = 0 ! K4 actual cu#)o# po)ition f#om top 4K
i.3.d3 = ( un)igned ) )hape ! K4 off)et add#e)) of pointe# image4K
)eg#ead ( L) ) !
).e) = ).d) ! K4 )egment add#e)) of pointe# 4K
int@=3 ( 0333, Li, Li, L) ) !
'
?1.
+owe#) ;f 5anoi
.uppo)e the#e a#e th#ee peg) labeled (, G and >. Hou# di)7) a#e placed on peg (.
+he bottom0mo)t di)7 i) la#ge)t, and di)7) go on dec#ea)ing in )i/e with the topmo)t
di)7 being )malle)t. +he obOective of the game i) to move the di)7) f#om peg ( to
peg >, u)ing peg G a) an au3ilia#- peg. +he #ule) of the game a#e a) follow)*
;nl- one di)7 ma- be moved at a time, and it mu)t be the top di)7 on one of the
peg). ( la#ge# di)7 )hould neve# be placed on the top of a )malle# di)7. .uppo)e we
a#e to w#ite a p#og#am to p#int out the )eMuence in which the di)7) )hould be moved
)uch that all di)7) on peg ( a#e finall- t#an)fe##ed to peg >. 5e#e it i)...
main( )
{
int n = 9 !
move ( n, 2(2, 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 ) !
el)e
{
move ( n 0 1, )p, ep, ap ) !
move ( 1, )p, 2 2, ep ) !
move ( n 0 1, ap, )p, ep ) !
'
'
(nd he#e i) the output...
Pove f#om ( to G
Pove f#om ( to >
Pove f#om G to >
Pove f#om ( to G
Pove f#om > to (
Pove f#om > to G
Pove f#om ( to G
Pove 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 G
Pove f#om ( to >
Pove f#om G to >
+hi) p#oblem i) the famou) +owe#) of 5anoi p#oblem, whe#ein th#ee peg) a#e to be
emplo-ed fo# t#an)fe##ing the di)7) with the given c#ite#ia. 5e#e2) how we go about
it. We have th#ee peg)* the )ta#ting peg, )p, the au3ilia#- peg ap, and the ending
peg, ep, whe#e the di)7) mu)t finall- be. Hi#)t, u)ing the ending peg a) an au3ilia#-
o# )uppo#ting peg, we t#an)fe# all but the la)t di)7 to ap. 6e3t the la)t di)7 i) moved
f#om )p to ep. 6ow, u)ing )p a) the )uppo#ting peg, all the di)7) a#e moved f#om ap
to ep. X(Y, G and > denote the th#ee peg). +he #ecu#)ive function move( ) i) called
with diffe#ent combination) of the)e peg) a) )ta#ting, au3ilia#- and ending peg).
00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00
?3.
What would be the output of following p#og#am?
)t#uct )-nta3
{
int i !
float g !
cha# c !
'
main( )
{
p#intf ( &, won2t give -ou an- e##o#& ) !
'
(n)* +he above p#og#am would get compiled )ucce))full- and on e3ecution it would
p#int the me))age given in p#intf(). What )t#i7e) in the above code )nippet i) the
)t#uctu#e )-nta3 which i) decla#ed but not te#minated with the )tatement te#minato#,
the )emicolon. +he compile# would not give an- e##o# me))age fo# it, a) it a))ume)
that main( ) function have a #etu#n t-pe of )t#uct )-nta3 and hence would
)ucce))full- compile and e3ecute the p#og#am.
00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00
?9.
5ow to get the memo#- )i/e ?
(n)* >on)ide# the following p#og#am
Dinclude E)tdio.hF
Dinclude Ebio).hF
main( )
{
int mem)i/e!
mem)i/e = bio)memo#-( ) !
p#intf ( &U(P )i/e = %dNJn&,mem)i/e ) !
#etu#n 0 !
'
+he function bio)memo#- u)e) G,;. inte##upt 0311 to #etu#n the )i/e of memo#-.
00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00
?A.
Hloat Ho#mat
5ow doe) > compile# )to#e) float value) ?
(n)* ,n >, the float value) a#e )to#ed in a manti))a and e3ponent fo#m. While w#iting
a numbe# we )pecif- the e3ponent pa#t in the fo#m of ba)e 10. Gut, in ca)e of >
compile#, the e3ponent fo# float) i) )to#ed in the fo#m of ba)e 1. ;bviou)l-, becau)e,
compute# )to#e) the numbe#) in bina#- fo#m. +he > compile# follow) an ,III
)tanda#d to )to#e a float. +he ,III fo#mat e3p#e))e) a floating0point numbe# in a
bina#- fo#m 7nown a) _no#mali/ed2 fo#m. 6o#mali/ation involve) adOu)ting the
e3ponent )o that the &bina#- point& (the bina#- analog of the decimal point) in the
manti))a alwa-) lie) to the #ight of mo)t )ignificant non/e#o digit. ,n bina#-
#ep#e)entation, thi) mean) that the mo)t )ignificant digit of the manti))a i) alwa-) a
1. +hi) p#ope#t- of the no#mali/ed #ep#e)entation i) e3ploited b- the ,III fo#mat
when )to#ing the manti))a. <et u) con)ide# an e3ample of gene#ating the no#mali/ed
fo#m of a floating point numbe#. .uppo)e we want to #ep#e)ent the decimal numbe#
A.3?A. ,t) bina#- eMuivalent can be obtained a) )hown below*
1 R A
.3?A 3 1 = 0.?A0 0
R000000
.?A0 3 1 = 1.A00 1
1 R 1 1
.A00 3 1 = 1.000 1
R000000
1 R 1 0
R000000
R 0 1
W#iting #emainde#) in #eve#)e w#iting whole pa#t) in the )ame o#de# we get 101
o#de# in which the- a#e obtained we get 011 thu) the bina#- eMuivalent of A.3?A
would be 101.011. +he no#mali/ed fo#m of thi) bina#- numbe# i) obtained b-
adOu)ting the e3ponent until the decimal point i) to the #ight of mo)t )ignificant 1. ,n
thi) ca)e the #e)ult i) 1.01011 3 11. +he ,III fo#mat fo# floating point )to#age u)e) a
)ign bit, a manti))a and an e3ponent fo# #ep#e)enting the powe# of 1. +he )ign bit
denote) the )ign of the numbe#* a 0 #ep#e)ent) a po)itive value and a 1 denote) a
negative value. +he manti))a i) #ep#e)ented in bina#-. >onve#ting the floating0point
numbe# to it) no#mali/ed fo#m #e)ult) in a manti))a who)e mo)t )ignificant digit i)
alwa-) 1. +he ,III fo#mat ta7e) advantage of thi) b- not )to#ing thi) bit at all. +he
e3ponent i) an intege# )to#ed in un)igned bina#- fo#mat afte# adding a po)itive
intege# bia). +hi) en)u#e) that the )to#ed e3ponent i) alwa-) po)itive. +he value of
the bia) i) 11? fo# float) and 1013 fo# double). +hu), 1.01011 3 11 i) #ep#e)ented a)
)hown below*
000 000000000000000 0000000000000000000000000000000000000000000000
R 0 R 100 0000 1 R 010 1100 0000 0000 0000 0000 R
000 0000000000000000 000000000000000000000000000000000000000000000
)ign bit e3ponent0 manti))a )to#ed in no#mali/ed fo#m obtained afte# adding a bia)
11? to e3ponent 1
:ata .t#uctu#e)
Which i) the be)t )o#ting method?
(n)* +he#e i) no )o#ting method that i) unive#)all- )upe#io# to all othe#). +he
p#og#amme# mu)t ca#efull- e3amine the p#oblem and the de)i#ed #e)ult) befo#e
deciding the pa#ticula# )o#ting method. .ome of the )o#ting method) a#e given
below*
Gubble )o#t * When a file containing #eco#d) i) to be )o#ted then Gubble )o#t i) the
be)t )o#ting method when )o#ting b- add#e)) i) u)ed.
G)o#t * ,t can be #ecommended if the input to the file i) 7nown to be nea#l- )o#ted.
Pean)o#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 con)tantl-
being )wapped bac7 and fo#th between e3te#nal and inte#nal )to#age. ,n p#actical
)ituation), Muic7 )o#t i) often the fa)te)t available becau)e of it) low ove#head and it)
ave#age behavio#.
5eap )o#t * Wene#all- u)ed fo# )o#ting of complete bina#- t#ee. .imple in)e#tion )o#t
and )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 )ituation
in)e#tion )o#t i) #ecommended. +he heap )o#t and Muic7 )o#t a#e both mo#e efficient
than in)e#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#ed
element).
Uadi3 )o#t * ,t i) #ea)onabl- efficient if the numbe# of digit) in the 7e-) i) not too
la#ge.
?=.
>alculating Wa)ted G-te) ;n :i)7
When a file get) )to#ed on the di)7, at a time :;. allocate) one clu)te# fo# it. (
clu)te# i) nothing but a g#oup of )ecto#). 5oweve#, )ince all file )i/e) cannot be
e3pected to be a multiple of A11 b-te), when a file get) )to#ed often pa#t of the
clu)te# #emain) unoccupied. +hi) )pace goe) wa)te unle)) the file )i/e g#ow) to
occup- the)e wa)ted b-te). +he
following p#og#am find) out how much )pace i) wa)ted fo# all file) in all the
di#ecto#ie) of the cu##ent d#ive.
Dinclude Edi#.hF
Dinclude Edo).hF
Dinclude E)tdio.hF
Dinclude E)t#ing.hF
Dinclude E)tdlib.hF
un)igned b-te)Tpe#Tclu)te# !
un)igned long wa)tedTb-te) !
un)igned long numTfile) = 0 !
main( )
{
int pt# = 0, flag = 0, fi#)t = 0 !
)t#uct ffbl7 f[A0 !
)t#uct df#ee f#ee !
K4 get clu)te# info#mation and calculate b-te) pe# clu)te# 4K
getdf#ee ( 0, Lf#ee ) !
b-te)Tpe#Tclu)te# = f#ee.dfTb)ec 4 f#ee.dfT)clu) !
chdi# ( &JJ& ) !
K4 chec7 out file) in #oot di#ecto#- fi#)t 4K
calTwa)te( ) !
K4 loop until all di#ecto#ie) )canned 4K
while ( pt# 8= 01 )
{
K4 )hould , do a findfi#)t o# a findne3t? 4K
if ( fi#)t == 0 )
flag = findfi#)t ( &4.4&, Lf[pt#, H(T:,UI> ) !
el)e
flag = findne3t ( 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 == H(T:,UI> LL f[pt#.ffTname[0 8= 2.2 )
{
flag = chdi# ( f[pt#.ffTname ) ! K4 t#- changing di#ecto#ie) 4K
if ( flag == 0 ) K4 did change di# wo#7? 4K
{
calTwa)te( ) !
fi#)t = 0 ! K4 )et fo# findfi#)t on ne3t pa)) 4K
b#ea7 !
'
'
flag = findne3t ( 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#00 !
chdi# ( &..& ) ! K4 go bac7 one level 4K
fi#)t = 1 ! K4 )et to findne3t on ne3t pa)) 4K
'
el)e
pt#"" !
'
p#intf ( &+he#e a#e %lu b-te) wa)ted in %lu file).Jn&, wa)tedTb-te),
numTfile) ) !
'
calTwa)te( )
{
int flag = 0 !
long fullTclu)te# !
)t#uct ffbl7 ff !
K4 loo7 fo# all file t-pe) 4K
flag = findfi#)t ( &4.4&, Lff, H(TU:;6<V R H(T5,::I6 R H(T.V.+IP R H(T(U>5
) !
while ( flag == 0 )
{
numTfile)"" !
fullTclu)te# = ff.ffTf)i/e K b-te)Tpe#Tclu)te# 4 b-te)Tpe#Tclu)te# !
wa)tedTb-te) "= b-te)Tpe#Tclu)te# 0 ( ff.ffTf)i/e 0 fullTclu)te# ) !
flag = findne3t ( Lff ) !
'
'
:ata .t#uctu#e)
Soli)h 6otation
+he method of w#iting all ope#ato#) eithe# befo#e thei# ope#ation, o# afte# them, i)
called Soli)h notation, in hono# of it) di)cove#e#, the Soli)h mathematician ]an
<u7a)iewic/. When the ope#ato#) a#e w#itten befo#e thei# ope#and), it i) called the
p#efi3 fo#m. When the ope#ato#) come afte# thei# ope#and). ,t i) called the po)tfi3
fo#m, o#, )ometime) #eve#)e Soli)h fo#m o# )uffi3 fo#m. ,n thi) conte3t, it i)
cu)toma#- to u)e the coined ph#a)e infi3 fo#m to denote the u)ual cu)tom of w#iting
bina#- ope#ato#) between thei# ope#and). Ho# e3ample, the e3p#e))ion ( " G
become) "(G in p#efi3 fo#m and (G" in po)tfi3 fo#m. ,n the e3p#e))ion ( " G 3 >,
the multiplication i) done fi#)t, )o we conve#t it fi#)t, obtaining fi#)t ( " ( G>3 ) and
then (G>3" in po)tfi3 fo#m. +he p#efi3 fo#m of thi) e3p#e))ion i) "( 3 G>. +he p#efi3
and po)tfi3 fo#m) a#e not #elated b- ta7ing mi##o# image) o# othe# )uch )imple
t#an)fo#mation. (l)o all pa#enthe)e) have been omitted in the Soli)h fo#m).
00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00
??.
+he <ongOmp (nd .etOmp
+he > p#og#amming language doe) not let -ou ne)t function). Vou cannot w#ite a
function definition in)ide anothe# function definition, a) in*
int fun1( )
{
int fun1() K4 )uch ne)ting of function) i) not allowed 4K
{
.....
'
'
Gecau)e of thi) #e)t#iction it i) not po))ible to hide function name) in)ide a hie#a#ch-.
() a #e)ult all the function) that -ou decla#e within a p#og#am a#e vi)ible to each
othe#. +hi) of cou#)e i) not a maOo# d#awbac7 )ince one can limit vi)ibilit- 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- becau)e of thi) de)ign
deci)ion. ,t p#ovide) no ea)- wa- to t#an)fe# cont#ol out of a function e3cept b-
#etu#ning to the e3p#e))ion that called the function. Ho# the va)t maOo#it- of function
call), that i) a de)i#able limitation. Vou want the di)cipline of ne)ted function call)
and #etu#n) to help -ou unde#)tand flow of cont#ol th#ough a p#og#am. 6eve#thele)),
on )ome occa)ion) that di)cipline i) too #e)t#ictive. +he p#og#am i) )ometime) ea)ie#
to w#ite, and to unde#)tand, if -ou can Oump out of one o# mo#e function invocation)
at a )ingle )t#o7e. Vou want to b-pa)) the no#mal function #etu#n) and t#an)fe#
cont#ol to )omewhe#e in an ea#lie# function invocation.
Ho# e3ample, -ou ma- want to #etu#n to e3ecute )ome code fo# e##o# #ecove#- no
matte# whe#e an e##o# i) detected in -ou# application. +he )etOmp and the longOmp
function) p#ovide the tool) to accompli)h thi). +he )etOmp function )ave) the &)tate&
o# the &conte3t& of the p#oce)) and the longOmp u)e) the )aved conte3t to #eve#t to a
p#eviou) point in the p#og#am. What i) the conte3t of the p#oce))? ,n gene#al, the
conte3t of a p#oce)) #efe#) to info#mation that enable) -ou to #econ)t#uct e3actl- the
wa- the p#oce)) i) at a pa#ticula# point in it) flow of e3ecution. ,n > p#og#am the
#elevant info#mation include) Muantitie) )uch a) value) of .S, .., H<(W., >., ,S, GS,
:,, I., ., and :. #egi)te#).
+o )ave thi) info#mation +u#bo > u)e) the following )t#uctu#e, which i) defined, in the
heade# file 2)etOmp.h2.
t-pedef )t#uct
{
un)igned OT)p !
un)igned OT)) !
un)igned OTflag !
un)igned OTc) !
un)igned OTip !
un)igned OTbp !
un)igned OTdi !
un)igned OTe) !
un)igned OT)i !
un)igned OTd) !
' OmpTbuf[1 !
+hi) i) a )-)tem0dependent data t-pe becau)e diffe#ent )-)tem) might #eMui#e
diffe#ent amount) of info#mation to captu#e the conte3t of a p#oce)). ,n +u#bo >,
OmpTbuf i) )impl- an a##a- of ten 10b-te intege#). +o unde#)tand the mechanic) of
)etOmp and longOmp, loo7 at the following code
f#agment.
Dinclude &)etOmp.h&
OmpTbuf buf !
main( )
{
if ( )etOmp ( buf ) == 0 )
p#oce))( ) !
el)e
handleTe##o#( ) ! K4 e3ecuted when longOmp i) called 4K
'
p#oce))( )
{
int flag = 0 !
K4 )ome p#oce))ing i) done he#e 4K
K4 if an e##o# occu#) du#ing p#oce))ing 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 the
add#e)) of the if )tatement in the main function, to which )etOmp will #etu#n. +he
)etOmp function copie) thi) #etu#n add#e)) a) well a) the cu##ent value) of #egi)te#),
.S, .., H<(W., GS, :,, I., ., and :., into the buffe# buf. +hen )etOmp #etu#n) with a
/e#o. ,n thi) ca)e, the if )tatement i) )ati)fied and the p#oce))( ) function i) called. ,f
)omething goe) w#ong in p#oce))( ) (indicated b- the flag va#iable), we call longOmp
with two a#gument)* the fi#)t i) the buffe# that contain) the conte3t to which we will
#etu#n. When the )tac7 #eve#t) bac7 to thi) )aved )tate, and the #etu#n )tatement in
longOmp i) e3ecuted, it will be a) if we we#e #etu#ning f#om the call to )etOmp, which
o#iginall- )aved the buffe# buf. +he )econd a#gument to longOmp )pecifie) the #etu#n
value to be u)ed 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 u)ing the conventional #etu#n )tatement). I))entiall-,
)etOmp ma#7) the de)tination of the Oump and longOmp i) a non0local goto that
e3ecute) the Oump.
:ata .t#uctu#e)
>ompa#i)on +#ee)...
+he compa#i)on t#ee) al)o called deci)ion t#ee o# )ea#ch t#ee of an algo#ithm, i)
obtained b- t#acing th#ough the action) of the algo#ithm, #ep#e)enting each
compa#i)on of 7e-) b- a ve#te3 of the t#ee (which we d#aw a) a ci#cle). ,n)ide the
ci#cle we put the inde3 of the 7e- again)t which we a#e compa#ing the ta#get 7e-.
G#anche) (line)) d#awn down f#om the ci#cle #ep#e)ent the po))ible outcome) of the
compa#i)on and a#e labeled acco#dingl-. When the algo#ithm te#minate), we put
eithe# H (fo# failu#e) o# the location whe#e the ta#get i) found at the end of the
app#op#iate b#anch, which we call a leaf, and d#aw a) a )Mua#e. <eave) a#e al)o
)ometime) called end ve#tice) o# e3te#nal ve#tice) of the t#ee. +he #emaining ve#tice)
a#e called the inte#nal ve#tice) of the t#ee. +he compa#i)on t#ee fo# )eMuential )ea#ch
i) e)peciall- )imple.
00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00
?@.
.uppo)e we have a floating0point numbe# with highe# p#eci)ion )a- 11.11=9@?=@?
and we wi)h it to be p#inted with onl- p#eci)ion up to two decimal place). 5ow can ,
do thi)?
(n). +hi) can achieved th#ough the u)e of )upp#e))ion cha# 242 in the fo#mat )t#ing of
p#intf( ) which i) )hown in the following p#og#am.
main( )
{
int p = 1 !
float n = 11.11=9@?=@? !
p#intf ( &%.4f&,p, n ) !
'
00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00
?Q.
.pawning
(ll p#og#am) that we e3ecute f#om :;. p#ompt can be thought of a) child#en of
>;PP(6:.>;P. +hu), the p#og#am that we e3ecute i) a child p#oce)), whe#ea)
>;PP(6:.>;P #unning in memo#- i) it) pa#ent. +he p#oce)) of a pa#ent p#oce))
giving bi#th to a child p#oce)) i) 7nown a) 2)pawning2. ,f the )pawned p#og#am )o
de)i#e), it ma- in tu#n )pawn child#en of it) own, which then e3ecute and #etu#n
cont#ol to thei# pa#ent. Who i) the pa#ent of >;PP(6:.>;P? >;PP(6:.>;P it)elf.
We can t#ace the ance)to#) of ou# p#og#am u)ing the field Sa#ent S#oce)) ,: (S,:)
p#e)ent at off)et 031= in the S#og#am .egment S#efi3 (S.S). +o t#ace thi) ance)t#-
ou# p#og#am )hould fi#)t locate it) S.S, e3t#act the pa#ent p#oce)) ,: f#om it and
then u)e thi) to find S.S of the pa#ent. +hi) p#oce)) can be #epeated till we #each
>;PP(6:.>;P (p#oce)) ,: of >;PP(6:.>;P i) it) own S.S), the fathe# of all
p#oce))e). 5e#e i) a p#og#am which achieve) thi)...
K4 .S(W6.> 4K
Dinclude &do).h&
un)igned oldp)p, newp)p, fa# 4ebT)eg, i !
cha# fa# 4ebTpt# !
main( )
{
oldp)p = Tp)p !
while ( 1 )
{
p#intf ( &Jn& ) !
p#intname ( oldp)p ) !
p#intf ( & )pawned b- & ) !
newp)p = 4 ( ( un)igned fa# 4 ) PNTHS ( oldp)p, 031= ) ) !
if ( 4 ( ( un)igned 4 ) PNTHS ( newp)p, 031= ) ) == newp)p )
b#ea7 !
el)e
oldp)p = newp)p !
p#intname ( newp)p ) !
'
p#intf ( &%010) (%09\)&, &>;PP(6:.>;P&, newp)p ) !
'
p#intname ( un)igned lp)p )
{
cha# d#ive[A, di#[=@, name[13, e3t[A !
ebT)eg = ( un)igned fa# 4 ) PNTHS ( lp)p, 031> ) !
ebTpt# = PNTHS ( 4ebT)eg, 0 ) !
i = 0 !
while ( 1 )
{
if ( ebTpt#[i == 0 )
{
if ( ebTpt#[i " 1 == 0 LL ebTpt#[i " 1 == 1 )
{
i "= 9 !
b#ea7 !
'
'
i"" !
'
fn)plit ( ebTpt# " i, d#ive, di#, name, e3t ) !
)t#cat ( name, e3t ) !
p#intf ( &%010) (%09\)&, name, oldp)p ) !
'
;n #unning the p#og#am f#om within +> the output obtained i) )hown below.
.SW(6.I\I (A@(Q) )pawned b- +>.I\I (0=?1) +>.I\I (0=?1) )pawned b-
>;PP(6:.>;P (0AG@). +he p#og#am )impl- copie) it) own p#oce)) ,: in the
va#iable oldp)p and then u)e) it to e3t#act it) own filename f#om it) envi#onment
bloc7. +hi) i) done b- the function p#intname( ). +he value in oldp)p i) then u)ed to
#et#ieve the pa#ent2) S,: in newp)p. H#om the#e the p#og#am loop) #epo#ting the
value) of oldp)p, newp)p and the co##e)ponding file name) until the p#og#am #eache)
>;PP(6:.>;P.
+he p#intname( ) function fi#)t locate) the envi#onment bloc7 of the p#og#am and
then e3t#act) the file name f#om the envi#onment bloc7. +he fn)plit( ) function ha)
been u)ed to eliminate the path p#e)ent p#io# to the file name. :o not #un the
p#og#am f#om command line )ince it would give -ou onl- one level of ance)t#-.
:ata .t#uctu#e)
>hoo)ing the data )t#uctu#e) to be u)ed fo# info#mation #et#ieval. Ho# p#oblem) of
info#mation #et#ieval, con)ide# the )i/e, numbe#, and location of the #eco#d) along
with the t-pe and )t#uctu#e of the 7e-) while choo)ing the data )t#uctu#e) to be
u)ed. Ho# )mall #eco#d), high0)peed inte#nal memo#- will be u)ed, and bina#- )ea#ch
t#ee) will li7el- p#ove adeMuate. Ho# info#mation #et#ieval f#om di)7 file), method)
emplo-ing multiwa- b#anching, )uch a) t#ee), G0t#ee) , and ha)h table), will u)uall-
be )upe#io#. +#ie) a#e pa#ticula#l- )uited to application) whe#e the 7e-) a#e )t#uctu#ed
a) a )eMuence of )-mbol) and whe#e the )et of 7e-) i) #elativel- den)e in the )et of
all po))ible 7e-). Ho# othe# application), method) that t#eat the 7e- a) a )ingle unit
will often p#ove )upe#io#. G0t#ee), togethe# with va#iou) gene#ali/ation and
e3ten)ion), can be u)efull- applied to man- p#oblem) conce#ned with e3te#nal
info#mation #et#ieval.
@0.
Ca#iabl- :imen)ioned (##a-)
While dealing with .cientific o# Inginee#ing p#oblem) one i) often #eMui#ed to ma7e
u)e of multi0dimen)ioned a##a-. 5oweve#, when it come) to pa))ing multidimen)ional
a##a-) to a function > i) found wanting. +hi) i) becau)e the > compile# want) to
7now the )i/e of all but the fi#)t dimen)ion of an- a##a- pa))ed to a function. Ho#
in)tance, we can define a function compute ( int n, float 3[ ), but not compute ( int
n, 3[[).
+hu), > can deal with va#iabl- dimen)ioned 10: a##a-), but when an a##a- ha) mo#e
than one dimen)ion, the > compile# ha) to 7now the )i/e of the la)t dimen)ion)
e3p#e))ed a) a con)tant. +hi) p#oblem ha) long been #ecogni/ed, and )ome of the
)olution) that a#e often u)ed a#e*
:ecla#e the a##a-) in the function) to be big enough to tac7le all po))ible )ituation).
+hi) can lead to a wa)tage of lot of p#eciou) memo#- in mo)t ca)e). (nothe# )olution
i) to con)t#uct multiple0dimen)ion a##a- a) an a##a- of pointe#). Ho# e3ample, a
mat#i3 (10: a##a-) of float) can be decla#ed a) a 10: a##a- of float pointe#), with
each element pointing to an a##a- of float). +he p#oblem with thi) method i) that the
calling function ha) to define all a##a-) in thi) fa)hion. +hi) mean) that an- othe#
computation) done on the a##a-) mu)t ta7e thi) )pecial )t#uctu#e into account.
(nothe# ea)- )olution, though )eldom u)ed, e3i)t). +hi) i) ba)ed on the following
method*
Sa)) the a##a- to the function a) though it i) a pointe# to an a##a- of float) (o# the
app#op#iate data t-pe), no matte# how man- dimen)ion) the a##a- actuall- ha),
along with the dimen)ion) of the a##a-. Uefe#ence individual a##a- element) a)
off)et) f#om thi) pointe#.
W#ite -ou# algo#ithm )o that a##a- element) a#e acce))ed in )to#age o#de#. +he
following p#og#am fo# multipl-ing two mat#ice) illu)t#ate) thi)
p#ocedu#e.
D define P 3
D define 6 1
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 )
{
float 4pt#tob, 4pt#toc !
int i, O, 7, nc !
K4 )et all element) of mat#i3 c to 0 4K
fo# ( 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 #ow0maOo# o#de#. 5ence to en)u#e that
the element) a#e acce))ed in the )to#age o#de# the above p#og#am u)e) a va#iation
of the no#mal mat#i30multiplication p#ocedu#e. +he p)eudo code fo# thi) i) given
below*
fo# i = 1 to m
fo# O = 1 to p
c[i[O = 0
end
fo# 7 = 1 to n
fo# O = 1 to p
c[i[O = c[i[O " a[i[7 4 b[7[O
end
end
end
00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00
@1.
Wh- i) it not po))ible to )can )t#ing) f#om 7e-boa#d in ca)e of a##a- of pointe#) to
)t#ing?
(n)* When an a##a- i) decla#ed, dimen)ion of a##a- )hould be )pecified )o that
compile# can allocate memo#- fo# the a##a-. When a##a- of pointe#) to )t#ing) i)
decla#ed it) element) would contain ga#bage add#e))e). +he)e add#e))e) would be
pa))ed to )canf( ). .o )t#ing) can be #eceived but the- would get )to#ed at un7own
location). +hi) i) un)afe.
00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00
@1.
Git (##a-)
,f in a p#og#am a va#iable i) to ta7e onl- two value) 1 and 0, we #eall- need onl- a
)ingle bit to )to#e it. .imila#l-, if a va#iable i) to ta7e value) f#om 0 to 3, then two
bit) a#e )ufficient to )to#e the)e value). (nd if a va#iable i) to ta7e value) f#om 0
th#ough ?, then th#ee bit) will be enough, and )o on. Wh- wa)te an enti#e intege#
when one o# two o# th#ee bit) will do? Gecau)e 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) who)e
ma3imum value) a#e )mall enough to pac7 into a )ingle memo#- location, we can
u)e _bit field)2 to )to#e )eve#al value) in a )ingle intege#. Git field) a#e di)cu))ed in
mo)t )tanda#d > te3t). +he- a#e u)uall- u)ed when we want to )to#e a))o#ted
info#mation which can be accommodated in 1, 1, 3 bit) etc.
Ho# e3ample, the following data about an emplo-ee can be ea)il- )to#ed u)ing bit
field).
male o# female
)ingle, ma##ied, divo#ced o# widowed
have one of the eight diffe#ent hobbie)
can choo)e f#om an- of the fifteen diffe#ent )cheme) p#opo)ed b- the compan- to
pu#)ue hi)Khe# 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 u)ed fo# tho)e who a#e not de)i#ou) of
availing an- of the )cheme)). We need ten bit) altogethe#, which mean) we can pac7
all 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# Hal)e )tatu)e). ,n )uch ca)e) in)tead
of u)ing bit field) u)ing an a##a- of bit) would be mo#e )en)ible. ;n thi) a##a- we
ma- be #eMui#ed to pe#fo#m the following ope#ation)*
.et a bit (ma7e it 1).
>lea# a bit (ma7e it 0).
+e)t the )tatu) of a bit in the a##a-.
Ueach the app#op#iate bit )lot in the a##a-.
Wene#ate a bit ma)7 fo# )etting and clea#ing a bit.
We can implement the)e ope#ation) u)ing mac#o) given below*
Ddefine >5(U.,^I @
Ddefine P(.N ( - ) ( 1 EE - % >5(U.,^I )
Ddefine G,+.<;+ ( - ) ( - K >5(U.,^I )
Ddefine .I+ ( 3, - ) ( 3[G,+.<;+( - ) R= P(.N( - ) )
Ddefine ><I(U ( 3, - ) ( 3[G,+.<;+( - ) L= aP(.N( - ) )
Ddefine +I.+ ( 3, - ) ( 3[G,+.<;+( - ) L P(.N( - ) )
Ddefine 6BP.<;+. ( n ) ( ( n " >5(U.,^I 0 1) K >5(U.,^I )
B)ing the)e mac#o) we can decla#e an a##a- of A0 bit) be )a-ing,
cha# a##[6BP.<;+.(A0) !
+o )et the 10th bit we can )a-,
.I+(a##, 10 ) !
(nd if we a#e to te)t the )tatu) of 90th bit we ma- )a-,
if ( +I.+ ( a##, 90 ) )
B)ing bit a##a-) often #e)ult) into )aving a lot of p#eciou) memo#-. Ho# e3ample, the
following p#og#am which implement) the .ieve of I#ato)thene) fo# gene#ating p#ime
numbe#) )malle# than 100 #eMui#e) onl- 13 b-te). 5ad we implemented the )ame
logic u)ing an a##a- of intege#) we would have #eMui#ed an a##a- of 100 intege#),
that i) 100 b-te).
Dinclude E)tdio.hF
Dinclude E)t#ing.hF
Ddefine P(\ 100
main( )
{
cha# a##[6BP.<;+.( P(\ ) !
int i, O !
mem)et ( a##, 0, 6BP.<;+.( P(\ ) ) !
fo# ( i = 1 ! 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 ) !
'
'
'
00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00
@3.
,nfo#mation 5iding in >
+hough > language doe)n2t full- )uppo#t encap)ulation a) >"" doe), the#e i) a
)imple techniMue th#ough which we can implement encap)ulation in >. +he techniMue
that achieve) thi) i) modula# p#og#amming in >. Podula# p#og#amming #eMui#e) a
little e3t#a wo#7 f#om the p#og#amme#, but pa-) fo# it)elf du#ing maintenance. +o
unde#)tand thi) techniMue let u) ta7e the e3ample of the popula# )tac7 data
)t#uctu#e. +he#e a#e man- method) of implementing a )tac7 (a##a-, lin7ed li)t, etc.).
,nfo#mation hiding teache) that u)e#) )hould be able to pu)h and pop the )tac72)
element) without 7nowing about the )tac72) implementation. ( benefit of thi) )o#t of
info#mation hiding i) that u)e#) don2t have to change thei# code even if the
implementation detail) change.
>on)ide# 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 data
)t#uctu#e u)ing pointe#) and a lin7ed li)t of )t#uctu#e). +he main( ) function call) the
pu)h( ) and pop( ) function).
Dinclude Ealloc.hF
t-pedef int element !
void initiali/eT)tac7 ( )t#uct node 44 ) !
void pu)h ( )t#uct node 44, element ) !
element pop ( )t#uct node 4 ) !
int i)empt- ( )t#uct node 4 ) !
)t#uct node
{
element data !
)t#uct node 4ne3t !
' !
void main( )
{
)t#uct node 4top !
element num !
initiali/eT)tac7 ( Ltop ) !
pu)h ( Ltop, 10 ) !
pu)h ( Ltop, 10 ) !
pu)h ( Ltop, 30 ) !
if ( i)empt- ( top ) )
p#intf ( &Jn.tac7 i) empt-& ) !
el)e
{
num = pop ( top ) !
p#intf ( &Jn Sopped %d&, num ) !
'
'
void initiali/eT)tac7 ( )t#uct node 44p )
{
4p = 6B<< !
'
void pu)h ( )t#uct node 44p, element n )
{
)t#uct node 4# !
# = ( )t#uct node 4) malloc ( )i/eof ( )t#uct node ) ) !
# 0F data = n !
if ( 4p == 6B<< )
# 0F ne3t = 6B<< !
el)e
# 0F ne3t = 4p !
4p = # !
'
element pop ( )t#uct node 4p )
{
element n !
)t#uct node 4# !
n = p 0F data !
# = p !
p = p 0F ne3t !
f#ee ( # ) !
#etu#n ( n ) !
'
int i)empt- ( )t#uct node 4p )
{
if ( p == 6B<< )
#etu#n ( 01 ) !
el)e
#etu#n ( 0 ) !
'
6otice how the )pecific implementation of the data )t#uctu#e i) )t#ewn th#oughout
main( ). main( ) mu)t )ee the definition of the )t#uctu#e node to u)e the pu)h( ),
pop( ), and othe# )tac7 function). +hu) the implementation i) not hidden, but i)
mi3ed with the ab)t#act ope#ation).
:ata .t#uctu#e)
Uadi3 .o#t
+hi) )o#ting techniMue i) ba)ed on the value) of the actual digit) in the po)itional
#ep#e)entation) of the numbe#) being )o#ted. B)ing the decimal ba)e, fo# e3ample,
whe#e the #adi3 i) 10, the numbe#) can be pa#titioned into ten g#oup) on the )o#te#.
Ho# e3ample, to )o#t a collection of numbe#) whe#e each numbe# i) a fou#0digit
numbe#, then, (ll the numbe#) a#e fi#)t )o#ted acco#ding to the the digit at unit2)
place.
,n the )econd pa)), the numbe#) a#e )o#ted acco#ding to the digit at tenth place. ,n
the thi#d pa)), the numbe#) a#e )o#ted acco#ding to the digit at hund#edth place. ,n
the fo#th and la)t pa)), the numbe#) a#e )o#ted acco#ding to the digit at thou)andth
place.
:u#ing each pa)), each numbe# i) ta7en in the o#de# in which it appea#) in pa#tition)
f#om unit2) place onwa#d). When the)e action) have been pe#fo#med fo# each digit,
)ta#ting with the lea)t )ignificant and ending with mo)t )ignificant, the numbe#) a#e
)o#ted. +hi) )o#ting method i) called the #adi3 )o#t.
<et u) ta7e anothe# e3ample. .uppo)e we have a li)t of name). +o )o#t the)e name)
u)ing #adi3 )o#t method we will have to cla))if- them into 1= g#oup) +he li)t i) fi#)t
)o#ted on the fi#)t lette# of each name, i.e. the name) a#e a##anged in 1= cla))e),
whe#e the fi#)t cla)) con)i)t) of tho)e name) that begin with alphabet 2(2, the )econd
cla)) con)i)t) of tho)e name) that begin with alphabet 2G2 and )o on. :u#ing the
)econd pa)) each cla)) i) alphabeti/ed acco#ding to the )econd lette# of the name,
and )o on.
@9.
I3ception 5andling in >
>on)ide# the following p#og#am*
Dinclude Emath.hF
void main( )
{
float i !
i = pow ( 01, 3 ) !
p#intf ( &%f&, i ) !
'
int mathe## ( )t#uct e3ception 4a )
{
if ( a 0F t-pe == :;P(,6 )
{
if ( 8)t#cmp ( a 0F name, &pow& ) )
{
a 0F #etval = pow ( 0 ( a 0F a#g1 ), a 0F a#g1 ) !
#etu#n 1 !
'
'
#etu#n 0 !
'
,f we pa)) a negative value in pow( ) function a #un time e##o# occu#). ,f we wi)h to
get the p#ope# output even afte# pa))ing a negative value in the pow( ) function we
mu)t handle the #un time e##o#. Ho# thi), we can define a function mathe##( ) which i)
decla#ed in the 2math.h2 file. ,n thi) function we can detect the #un0time e##o# and
w#ite ou# code to co##ect the e##o#. +he element) of the e3ception )t#uctu#e #eceive)
the function name and a#gument) of the function cau)ing the e3ception.
:ata .t#uctu#e)
(C< +#ee)
Ho# ideal )ea#ching in a bina#- )ea#ch t#ee, the height) of the left and #ight )ub0t#ee)
of an- node )hould be eMual. Gut, due to #andom in)e#tion) and deletion) pe#fo#med
on a bina#- )ea#ch t#ee, it often tu#n) out to be fa# f#om ideal. ( clo)e app#o3imation
to an ideal bina#- )ea#ch t#ee i) achievable if it can be en)u#ed that the diffe#ence
between the height) of the left and the #ight )ub t#ee) of an- node in the t#ee i) at
mo)t one. ( bina#- )ea#ch t#ee in which the diffe#ence of height) of the #ight and left
)ub0t#ee) of an- node i) le)) than o# eMual to one i) 7nown a) an (C< t#ee. (C< t#ee
i) al)o called a) Galanced +#ee. +he name &(C< +#ee& i) de#ived f#om the name) of it)
invento#) who a#e (del)on0Ceil)7ii 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 height
between the left and the #ight )ub0t#ee) of the t#ee #ooted at that node. (nd it can
a))ume one of the
th#ee po))ible value) {01,0,1'.
00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00
@A.
BniMue combination) fo# a given numbe#
5ow do , w#ite a p#og#am which can gene#ate all po))ible combination) of numbe#)
f#om 1 to one le)) than the given numbe# ?
main( )
{
long )tep), fval, b)tp, cnt1 !
int num, unit, bo3[1[13, cnt1, cnt3, cnt9 !
p#intf ( &Inte# 6umbe# & ) !
)canf ( &%d&, Lnum ) !
num = num E 1 ? 1 * num F 11 ? 11 * num !
fo# ( )tep) = 1, cnt1 = 1 ! cnt1 E= num ! )tep) 4= cnt1"" ) !
fo# ( cnt1 = 1 ! cnt1 E= )tep) ! cnt1"" )
{
fo# ( cnt1 = 1 ! cnt1 E= num ! cnt1"" )
bo3[0[cnt1 = cnt1 !
fo# ( fval = )tep), b)tp = cnt1, cnt1 = 1 ! cnt1 E= num ! cnt1"" )
{
if ( b)tp == 0 )
{
cnt9=num !
while ( bo3[0[cnt9 == 0 )
cnt900 !
'
el)e
{
fval K= num 0 cnt1 " 1 !
unit = ( b)tp " fval 0 1 ) K fval !
b)tp %= fval !
fo# ( cnt9 = 0, cnt3 = 1 ! cnt3 E= unit ! cnt3"" )
while ( bo3[0[""cnt9 == 0 ) !
'
bo3[1[cnt1 = bo3[0[cnt9 !
bo3[0[cnt9 = 0 !
'
p#intf ( &Jn.eM.6o.%ld*&, cnt1 ) !
fo# ( cnt1 = 1 ! cnt1 E= num ! cnt1"" )
p#intf ( & %d&, bo3[1[cnt1 ) !
'
'
+hi) p#og#am compute) the total numbe# of )tep). Gut in)tead of ente#ing into the
loop of the fi#)t and la)t combination to be gene#ated it u)e) a loop of 1 to numbe# of
combination). Ho# e3ample, in ca)e of input being A the numbe# of po))ible
combination) would be facto#ial A, i.e. 110. +he p#og#am )uffe#) f#om the limitation
that it cannot gene#ate combination) fo# input be-ond 11 )ince a long int cannot
handle the #e)ulting combination).
:ata .t#uctu#e)
5a)hing...
5a)hing o# ha)h add#e))ing i) a )ea#ching techniMue. B)uall-, )ea#ch of an element i)
ca##ied out via a )eMuence of compa#i)on). 5a)hing diffe#) f#om thi) a) it i)
independent of the numbe# of element) n in the collection of data. 5e#e, the add#e))
o# location of an element i) obtained b- computing )ome a#ithmetic function.
5a)hing i) u)uall- u)ed in file management. +he gene#al idea i) of u)ing the 7e- to
dete#mine the add#e)) of a #eco#d. Ho# thi), a function fun( ) i) applied to each 7e-,
called the ha)h function. .ome of the popula# ha)h function) a#e* 2:ivi)ion2 method,
2Pid)Mua#e2 method, and 2Holding2 method. +wo #eco#d) cannot occup- the )ame
po)ition. .uch a )ituation i) called a ha)h colli)ion o# a ha)h cla)h. +he#e a#e two
ba)ic method) of dealing with a ha)h cla)h. +he fi#)t techniMue, called #eha)hing,
involve) u)ing )econda#- ha)h function on the ha)h 7e- of the item. +he #eha)h
function i) applied )ucce))ivel- until an empt- po)ition i) found whe#e the item can
be in)e#ted. ,f the ha)h po)ition of the item i) found to be occupied du#ing a )ea#ch,
the #eha)h function i) again u)ed to locate the item. +he )econd techniMue, called
chaining, build) a lin7ed li)t of all item) who)e 7e-) ha)h to the )ame value). :u#ing
)ea#ch, thi) )ho#t lin7ed li)t i) t#ave#)ed )eMuentiall- fo# the de)i#ed 7e-. +hi)
techniMue involve) adding an e3t#a lin7 field to each table po)ition.
00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00
@=.
+he following p#og#am demon)t#ate) how to get input f#om the u)e# in g#aphic)
mode, echoed in the cu##ent colo#) and font )i/e and font )t-le.
Ddefine ;6 1
Ddefine ;HH 0
Dinclude Eg#aphic).hF
main( )
{
cha# name.t#ing[@0, age.t#ing[@0 !
int age, gd = :I+I>+, gm !
initg#aph ( Lgd, Lgm, &c*JJtcJJbgi& ) !
)etb7colo# ( G<BI ) !
)etcolo# ( VI<<;W ) !
)ette3t)t-le ( W;+5,>TH;6+, 5;U,^T:,U, 0 ) !
moveto ( 0, 0 ) !
outte3t ( &Inte# -ou# name* & ) !
getW#.t#ing ( name.t#ing ) !
moveto ( 0, get-( ) " te3theight ( &(& ) ) !
outte3t ( &6ame* & ) !
outte3t ( name.t#ing ) !
moveto ( 0, get-( ) " te3theight ( &(& ) ) !
outte3t ( &S#e)) 7e- to e3it8 & ) !
getch( ) !
clo)eg#aph( ) !
#e)to#ec#tmode( ) !
'
getW#.t#ing ( cha# 4input.t#ing )
{
int )t#ing,nde3 = 0, old>olo# !
cha# ch, out.t#ing[1 !
K4 3Cal will )to#e the )c#een po)ition fo# each cha# 4K
int 3Cal[1AA !
out.t#ing[1 = 0 !
3Cal[0 = get3( ) !
do
{
cu#)o# ( ;6 ) !
ch = getch( ) !
cu#)o# ( ;HH ) !
if ( ch == 0 ) K4 avoid dealing with all )pecial 7e-) 4K
getch( ) !
el)e
{
if ( ch == @ ) K4 bac7)pace 4K
{
old>olo# = getcolo#( ) !
00)t#ing,nde3 !
if ( )t#ing,nde3 E 0 )
)t#ing,nde3 = 0 !
K4 move to ( old ho#/ po)ition, cu##ent ve#t po)ition ) 4K
moveto ( 3Cal[)t#ing,nde3, get-( ) ) !
)etcolo# ( getb7colo#( ) ) !
out.t#ing[0 = input.t#ing[)t#ing,nde3 !
outte3t ( out.t#ing ) !
moveto ( 3Cal [)t#ing,nde3, get-( ) ) !
)etcolo# ( old>olo# ) !
'
el)e
{
input.t#ing[)t#ing,nde3 = ch !
out.t#ing[0 = ch !
outte3t ( out.t#ing ) !
"")t#ing,nde3 !
3Cal[)t#ing,nde3 = get3( ) !
'
'
' while ( ch 8= 13 LL ch 8= 10 ) !
input.t#ing[)t#ing,nde3 = 0 !
'
cu#)o# ( int on )
{
int cu#\, old>olo# !
K4 we2ll u)e an unde#)co#e a) a cu#)o# 4K
cha# uGa#.t#[1 = { 2T2, 0 ' !
if ( 8on )
{
old>olo# = getcolo#( ) !
)etcolo# ( getb7colo#( ) ) !
'
K4 )ave ho#i/ontal po)ition befo#e d#awing cu#)o# 4K
cu#\ = get3( ) !
outte3t ( uGa#.t# ) !
moveto ( cu#\, get-( ) ) !
K4 if we changed the colo# to e#a)e cu#)o#, change it bac7 4K
if ( 8on )
)etcolo# ( old>olo# ) !
'
+he function getW#.t#ing( ) echoe) g#aphicall- the u)e# input and )to#e) it in a buffe#,
and the function cu#)o#( ) handle) the cu#)o# po)ition.
.-)tem Btilit-
What i) ga#bage collection?
(n)* .uppo)e )ome memo#- )pace become) #eu)able becau)e a node i) #elea)ed
f#om a lin7ed li)t. 5ence, we want the )pace to be available fo# futu#e u)e. ;ne wa-
to b#ing thi) about i) to immediatel- #ein)e#t the )pace into the f#ee0)to#age li)t.
5oweve#, thi) method ma- be too time0con)uming fo# the ope#ating )-)tem. +he
ope#ating )-)tem ma- pe#iodicall- collect all the deleted )pace onto the f#ee0)to#age
li)t. +he techniMue that doe) thi) collection i) called Wa#bage >ollection. Wa#bage
>ollection u)uall- ta7e) place in two )tep)* Hi#)t the Wa#bage >ollecto# #un) th#ough
all li)t), tagging who)e cell) a#e cu##entl- in u)e, and then it #un) th#ough the
memo#-, collecting all untagged )pace onto the f#ee0)to#age li)t. +he Wa#bage
>ollection ma- ta7e place when the#e i) onl- )ome minimum amount of )pace o# no
)pace at all left in the f#ee0)to#age li)t, o# when the >SB i) idle and ha) time to do
the collection. Wene#all- )pea7ing, the Wa#bage >ollection i) invi)ible to the
p#og#amme#.
@?.
5ow do , get the time elap)ed between two function call) ?
(n)* +he function difftime( ) find) the diffe#ence between two time). ,t calculate) the
elap)ed time in )econd) and #etu#n) the diffe#ence between two time) a) a double
value.
Dinclude Etime.hF
Dinclude E)tdio.hF
Dinclude Edo).hF
main( )
{
int a[ = { 1, 039, A=, ?@, 111, 33, 0?, 11, 9A, 1Q, = ' !
int ) !
timeTt t1, t1 ! KK timeTt define) the value u)ed fo# time function
) = )i/eof ( a ) K 1 !
t1 = time ( 6B<< ) !
)elT)o#t ( a, ) ) ! KK )o#t a##a- b- )election )o#t
bubT)o#t ( a, ) ) ! KK )o#t a##a- b- bubble )o#t method
t1 = time ( 6B<< ) !
p#intf ( &Jn+he diffe#ence between two function call) i) %f&, difftime (
t1, t1 ) ) !
'
,n the above p#og#am we have called difftime( ) function that #etu#n) the time
elap)ed f#om t1 to t1.
00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00
@@.
Wene#al
main( )
{
cha# 4) !
) = fun ( 11@, 1 ) !
p#intf ( &Jn%)&, ) ) !
'
fun ( un)igned int num, int ba)e )
{
)tatic cha# buff[33 !
cha# 4pt# !
pt# = Lbuff [ )i/eof ( buff ) 0 1 !
4pt# = 2J02 !
do
{
400pt# = &01139A=?@Qabcdef&[ num % ba)e !
num K= ba)e !
' while ( num 8= 0 ) !
#etu#n pt# !
'
+he above p#og#am would conve#t the numbe# 11@ to the ba)e 1. Vou can conve#t a
numbe# to a he3adecimal o# octal fo#m b- pa))ing the numbe# and the ba)e, to the
function fun( ).
:ata .t#uctu#e)
What i) a p#io#it- Mueue?
(n)* () we 7now in a )tac7, the late)t element i) deleted and in a Mueue the olde)t
element i) deleted. ,t ma- be #eMui#ed to delete an element with the highe)t p#io#it-
in the given )et of value) and not onl- the olde)t o# the newe)t one. ( data )t#uctu#e
that )uppo#t) efficient in)e#tion) of a new element and deletion) of element) with the
highe)t p#io#it- i) 7nown a) p#io#it- Mueue. +he#e a#e two t-pe) of p#io#it- Mueue)*
an a)cending p#io#it- Mueue i) a collection of item) into which item) can be in)e#ted
a#bit#a#il- and f#om which onl- the )malle)t item can be #emoved. ( de)cending
o#de# p#io#it- Mueue i) )imila# but allow) onl- the la#ge)t item to be deleted.
00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00
@Q.
What i) the diffe#ence between con)t cha# 4p, cha# con)t 4p, and cha#4 con)t p ?
2con)t cha# 4p2 and 2cha# con)t 4p2 a#e the )ame, i.e. p point) to a con)tant cha#acte#.
;n the othe# hand, 2cha#4 con)t p2 mean) p i) a con)tant pointe# pointing to a
cha#acte# which mean) we cannot change the pointe# p but we can change the
cha#acte# which p i) pointing to.
00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00
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 doe)n2t point an-whe#e. ( 6B<< mac#o i) u)ed
to #ep#e)ent the null pointe# in )ou#ce code. ,t ha) a value 0 a))ociated with it. +he
(.>,, 6B< cha#acte# ha) all it) bit) a) 0 but doe)n2t have an- #elation)hip with the
null pointe#. +he null )t#ing i) Ou)t anothe# name fo# an empt- )t#ing &&.
.-)tem Btilit-
.pa#)e Pat#i3...
( )pa#)e mat#i3 i) one whe#e mo)t of it) element) a#e /e#o. +he#e i) no p#eci)e
definition a) to 7now whethe# a mat#i3 i) )pa#)ed o# not, but it i) a concept which we
all can #ecogni/e intuitivel-. +he natu#al method of #ep#e)enting mat#ice) in memo#-
a) two0dimen)ional a##a-) ma- not be )uitable fo# )pa#)e mat#ice). +hat i) one ma-
)ave )pace b- )to#ing onl- tho)e ent#ie) which ma- be non/e#o. ,f thi) i) done, then
the mat#i3 ma- be thought of a) an o#de#ed li)t of non0/e#o element) onl-.
,nfo#mation about a non0/e#o element ha) th#ee pa#t)*
an intege# #ep#e)enting it) #ow,
an intege# #ep#e)enting it) column and
the data a))ociated with thi) element.
+hat i), each element of a mat#i3 i) uniMuel- cha#acte#i/ed b- it) #ow and column
po)ition, )a- i, O. We might )to#e that mat#i3 a) a li)t of 30tuple) of the fo#m (i, O,
data), a) )hown below,
(lthough the non0/e#o element) ma- be )to#ed in the a##a- in an- o#de#, 7eeping
them o#de#ed in )ome fa)hion ma- be advantageou) fo# fu#the# p#oce))ing. 6ote
that above a##a- i) a##anged in inc#ea)ing o#de# of the #ow numbe# of non0/e#o
element). Po#eove#, fo# element) in the )ame #ow numbe#, the a##a- i) a##anged in
o#de# of inc#ea)ing column numbe#.
00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00
Q1.
Sointe#)
What doe) the e##o# &6ull Sointe# ())ignment& mean and what cau)e) thi) e##o#?
(n)* +he 6ull Sointe# ())ignment e##o# i) gene#ated onl- in )mall and medium
memo#- model). +hi) e##o# occu#) in p#og#am) which attempt to change the bottom
of the data )egment. ,n Go#land2) > o# >"" compile#), Go#land place) fou# /e#o
b-te) at the bottom of the data )egment, followed b- the Go#land cop-#ight notice
&Go#land >"" 0 >op-#ight 1QQ1 Go#land ,ntl.&. ,n the )mall and medium memo#-
model), a null pointe# point) to :.*0000. +hu) a))igning 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. (t
p#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 that
the pointe# ma- not t#ul- be null, but ma- be a wild pointe# that #efe#ence) the)e
7e- a#ea) in the data )egment.
:ata .t#uctu#e)
5ow to build an e3p#e))ion t#ee) ?
(n)* (n e3p#e))ion t#ee i) a bina#- t#ee which i) built f#om )imple ope#and) and
ope#ato#) of an (a#ithmetic o# logical ) e3p#e))ion b- placing )imple ope#and) a) the
leave) of a bina#- t#ee and the ope#ato#) a) the inte#io# node). ,f an ope#ato# i)
bina#- , then it ha) two nonempt- )ubt#ee), that a#e it) left and #ight ope#and)
(eithe# )imple ope#and) o# )ub e3p#e))ion)). ,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# i)
w#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) &0& ( una#- negation) o# the )tanda#d
function) li7e log( ), )in( ) etc. ;the#) a#e w#itten on the #ight, )uch a) the facto#ial
function ()8. ,f the ope#ato# i) w#itten on the left, then in the e3p#e))ion t#ee we ta7e
it) left )ubt#ee a) empt-. ,f it appea#) on the #ight, then it) #ight )ubt#ee will be
empt-. (n e3ample of an e3p#e))ion t#ee i) )hown below fo# the e3p#e))ion ( 0a E
b ) o# ( c " d ) .
00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00
Q1.
>an we get the #emainde# of a floating point divi)ion ?
(n) * Ve). (lthough the % ope#ato# fail) to wo#7 on float numbe#) we can )till get
the #emainde# of floating point divi)ion b- u)ing a function fmod( ). +he fmod( )
function divide) the two float numbe#) pa))ed to it a) pa#amete#) and #etu#n) the
#emainde# a) a floating0point value. Hollowing p#og#am )how) fmod( ) function at
wo#7.
Dinclude Emath.hF
main( )
{
p#intf ( &%f&, fmod ( A.1A, 3.0 ) ) !
'
+he above code )nippet would give the output a) 1.1A0000.
00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00
Q3.
5ow to e3t#act the intege# pa#t and a f#actional pa#t of a floating point numbe#?
(n)* > function modf( ) can be u)ed 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.
5ow do , define a pointe# to a function which #etu#n) a cha# pointe#?
(n)*
cha# 4 ( 4p )( ) !
o#
t-pedef cha# 4 ( 4 pt#tofun )( ) !
pt#tofun p !
5e#e i) a )ample p#og#am which u)e) thi) definition.
main( )
{
t-pedef cha# 4 ( 4 pt#tofun ) ( ) !
cha# 4 fun( ) !
pt#tofun fpt# !
cha# 4cpt# !
fpt# = fun !
cpt# = (4fpt#) ( ) !
p#intf ( &JnUetu#ned )t#ing i) J&%)J&&, cpt# ) !
'
cha# 4 fun( )
{
)tatic cha# )[ = &5ello8& !
p#intf ( &Jn%)&, ) ) !
#etu#n ) !
'
00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00
QA.
What2) w#ong with the following decla#ation* cha#4 pt#1, pt#1 ! get e##o#) when , t#-
to u)e pt#1 a) a pointe#.
(n)* cha# 4 applie) onl- to pt#1 and not to pt#1. 5ence pt#1 i) getting decla#ed a) a
cha# pointe#, whe#ea), pt#1 i) being decla#ed me#el- a) a cha#. +hi) can be #ectified
in two wa-) *
cha# 4pt#1, 4pt#1 !
t-pedef cha#4 >5(US+U ! >5(US+U pt#1, pt#1 !
00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00
Q=.
5ow to u)e )canf( ) to #ead the date in the fo#m of dd0mm0--?
(n)* +o #ead the date in the fo#m of dd0mm0-- one po))ible wa- i),
int dd, mm, -- !
cha# ch ! K4 fo# cha# 202 4K
p#intf ( &JnInte# the date in the fo#m of dd0mm0-- * & ) !
)canf( &%d%c%d%c%d&, Ldd, Lch, Lmm, Lch, L-- ) !
(nothe# wa- i) to u)e )upp#e))ion cha#acte# 4 i) a) follow)*
int dd, mm, -- !
)canf( &%d%4c%d%4c%d&, Ldd, Lmm, L-- ) !
+he )upp#e))ion cha#acte# 242 )upp#e))e) the input #ead f#om the )tanda#d input
buffe# fo# the a))igned cont#ol cha#acte#.
00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00
Q?.
Wh- the output of )i/eof ( 2a2 ) i) 1 and not 1 ?
(n)* >ha#acte# con)tant) in > a#e of t-pe int, hence )i/eof ( 2a2 ) i) eMuivalent to
)i/eof ( int ), i.e. 1. 5ence the output come) out to be 1 b-te).
00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00
Q@.
>an we u)e )canf( ) function to )can a multiple wo#d) )t#ing th#ough 7e-boa#d?
(n)* Ve). (lthough we u)uall- u)e )canf( ) function to #eceive a )ingle wo#d )t#ing
and get)( ) to #eceive a multi0wo#d )t#ing f#om 7e-boa#d we can al)o u)e )canf( )
function fo# )canning a multi0wo#d )t#ing f#om 7e-boa#d. Hollowing p#og#am )how)
how to achieve thi).
main( )
{
cha# buff[1A !
)canf ( &%[bJn)&, buff ) !
put) ( buff ) !
'
,n the )canf( ) function we can )pecif- the delimite# in b#ac7et) afte# the b cha#acte#.
We have )pecified 2Jn2 a) the delimite#. 5ence )canf( ) te#minate) onl- when the u)e#
hit) Inte# 7e-.
00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00
QQ.
5ow to )et the )-)tem date th#ough a > p#og#am ?
(n)* We can )et the )-)tem date u)ing the )etdate( ) function a) )hown in the
following p#og#am. +he function a))ign) the cu##ent time to a
)t#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 ) !
'
00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00
100.
5ow can , w#ite a gene#al0pu#po)e )wap without u)ing template)?
(n)* Wiven below i) the p#og#am which u)e) the )t#ingi/ing p#ep#oce))o# di#ective
DD fo# building a gene#al pu#po)e )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 = 10 !
cha# ch1 = 2a2 , ch1 = 2b2 !
float f1 = 1.11, f1 = 3.19 !
)wap ( a, b, int ) !
p#intf ( &Jna = %d b = %d&, a, b ) !
)wap ( ch1, ch1, cha# ) !
p#intf ( &Jnch1 = %c ch1 = %c&, ch1, ch1 ) !
)wap ( f1, f1, float ) !
p#intf ( &Jnf1 = %9.1f f1 = %9.1f&, f1, f1 ) !
'
)wap ( a, b, int ) would e3pand to,
( gint = ( a ), ( a ) = ( b ), ( b ) = gint )
00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00
101.
What i) a heap ?
(n) * 5eap i) a chun7 of memo#-. When in a p#og#am memo#- i) allocated
d-namicall-, the > #un0time lib#a#- get) the memo#- f#om a collection of unu)ed
memo#- called the heap. +he heap #e)ide) in a p#og#am2) data )egment. +he#efo#e,
the amount of heap )pace available to the p#og#am i) fi3ed, and can va#- f#om one
p#og#am to anothe#.
101.
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 u)e 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 ) )
p#intf ( &Sathname * %)Jn&, path ) !
el)e
p#intf ( &Hile not foundJn& ) !
'
00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00
103.
>an we get the p#oce)) identification numbe# of the cu##ent p#og#am?
(n)* Ve)8 +he mac#o getpid( ) give) u) the p#oce)) identification numbe# of the
p#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#efi3 a) the p#oce)) id. Hollowing
p#og#am illu)t#ate) the u)e of thi) mac#o.
Dinclude E)tdio.hF
Dinclude Ep#oce)).hF
void main( )
{
p#intf ( &+he p#oce)) identification numbe# of thi) p#og#am i) %\Jn&,
getpid( ) ) !
'
00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00

109.
5ow do , w#ite a function that ta7e) va#iable numbe# of a#gument)?
(n)* +he following p#og#am demon)t#ate) thi).
Dinclude E)tdio.hF
Dinclude E)tda#g.hF
void main( )
{
int i = 10 !
float f = 1.A !
cha# 4)t# = &5ello8& !
vfpf ( &%d %f %)Jn&, i, f, )t# ) !
vfpf ( &%) %)&, )t#, &5i8& ) !
'
void vfpf ( cha# 4fmt, ... )
{
vaTli)t a#gpt# !
vaT)ta#t ( a#gpt#, fmt ) !
vfp#intf ( )tdout, fmt, a#gpt# ) !
vaTend ( a#gpt# ) !
'
5e#e, the function vfpf( ) ha) called vfp#intf( ) that ta7e va#iable a#gument li)t).
vaTli)t i) an a##a- that hold) info#mation #eMui#ed fo# the mac#o) vaT)ta#t and
vaTend. +he mac#o) vaT)ta#t and vaTend p#ovide a po#table wa- to acce)) the
va#iable a#gument li)t). vaT)ta#t would )et up a pointe# a#gpt# to point to the fi#)t of
the va#iable a#gument) being pa))ed to the function. +he mac#o vaTend help) the
called function to pe#fo#m a no#mal #etu#n.
00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00
10A.
>an we change the )-)tem date to )ome othe# date?
(n)* Ve), We can8 +he function )time( ) )et) the )-)tem date to the )pecified date. ,t
al)o )et) the )-)tem time. +he time and date i) mea)u#ed in )econd) f#om the
00*00*00 WP+, ]anua#- 1, 1Q?0. +he following p#og#am )how) how to u)e thi)
function.
Dinclude E)tdio.hF
Dinclude Etime.hF
void main( )
{
timeTt tm !
int d !
tm = time ( 6B<< ) !
p#intf ( &+he .-)tem :ate * %)&, ctime ( Ltm ) ) !
p#intf ( &Jn5ow man- da-) ahead -ou want to )et the date * & ) !
)canf ( &%d&, Ld ) !
tm "= ( 19< 4 d ) 4 =0< 4 =0< !
)time ( Ltm ) !
p#intf ( &Jn6ow the new date i) * %)&, ctime ( Ltm ) ) !
'
,n thi) p#og#am we have u)ed function ctime( ) in addition to function )time( ). +he
ctime( ) function conve#t) time value to a 1=0cha#acte# long )t#ing that contain) date
and time.
00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00
10=.
5ow to u)e function )t#dup( ) in a p#og#am?
(n) * +he )t#ing function )t#dup( ) copie) the given )t#ing to a new location. +he
function u)e) malloc( ) function to allocate )pace #eMui#ed fo# the duplicated )t#ing.
,t ta7e) one a#gument a pointe# to the )t#ing to be duplicated. +he total numbe# of
cha#acte#) p#e)ent in the given )t#ing plu) one b-te) get allocated fo# the new )t#ing.
() thi) function u)e) malloc( ) to allocate memo#-, it i) the p#og#amme#Y)
#e)pon)ibilit- to deallocate the memo#- u)ing f#ee( ).
Dinclude E)tdio.hF
Dinclude E)t#ing.hF
Dinclude Ealloc.hF
void main( )
{
cha# 4)t#1, 4)t#1 = &double&!
)t#1 = )t#dup ( )t#1 ) !
p#intf ( &%)Jn&, )t#1 ) !
f#ee ( )t#1 ) !
'
00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00
10?.
;n including a file twice , get e##o#) #epo#ting #edefinition of function.
5ow can , avoid duplicate inclu)ion?
(n)* Uedefinition e##o#) can be avoided b- u)ing the following mac#o definition.
,nclude thi) definition in the heade# file.
Dif 8defined filenameTh
Ddefine filenameTh
K4 function definition) 4K
Dendif
Ueplace filenameTh with the actual heade# file name. Ho# e3ample, if name of file to
be included i) 2goto.h2 then #eplace filenameTh with 2gotoTh2.
00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00
10@.
5ow to w#ite a )wap( ) function which )wap) the value) of the va#iable) u)ing bitwi)e
ope#ato#).
(n)* 5e#e i) the )wap( ) function.
)wap ( int 43, int 4- )
{
43 b= 4- !
4- b= 43 !
43 b= 4- !
'
+he )wap( ) function u)e) the bitwi)e \;U ope#ato# and doe) not #eMui#e an-
tempo#a#- va#iable fo# )wapping.

Vous aimerez peut-être aussi