You are on page 1of 55

www.technicalsymposium.

com 1
1.
What will be the output of the following code?
void main ()
{ int i = 0 , a!" #
ai" = i$$#
p%intf (&'d(,ai") #
)
*ns+ ,he output fo% the above code would be a ga%bage value. -n the statement ai" = i$$# the value of the va%iable i
would get assigned fi%st to ai" i.e. a0" and then the value of i would get inc%emented by 1. .ince ai" i.e. a1" has not
been initiali/ed, ai" will have a ga%bage value.
0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
1.
Why doesn2t the following code give the desi%ed %esult?
int 3 = !000, y = 1000 #
long int / = 3 4 y #
*ns+ 5e%e the multiplication is ca%%ied out between two ints 3 and y, and the %esult that would ove%flow would be
t%uncated befo%e being assigned to the va%iable / of type long int. 5oweve%, to get the co%%ect output, we should use an
e3plicit cast to fo%ce long a%ithmetic as shown below+
long int / = ( long int ) 3 4 y #
6ote that ( long int )( 3 4 y ) would not give the desi%ed effect.
0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
!.
Why doesn2t the following statement wo%7?
cha% st% " = (5ello( #
st%cat ( st%, 282 ) #
*ns+ ,he st%ing function st%cat( ) concatenates st%ings and not a cha%acte%. ,he basic diffe%ence between a st%ing and a
cha%acte% is that a st%ing is a collection of cha%acte%s, %ep%esented by an a%%ay of cha%acte%s whe%eas a cha%acte% is a
single cha%acte%. ,o ma7e the above statement wo%7 w%ites the statement as shown below+
st%cat ( st%, (8( ) #
0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
9.
5ow do - 7now how many elements an a%%ay can hold?
*ns+ ,he amount of memo%y an a%%ay can consume depends on the data type of an a%%ay. -n :;.
envi%onment, the amount of memo%y an a%%ay can consume depends on the cu%%ent memo%y model (i.e. ,iny, .mall,
<a%ge, 5uge, etc.). -n gene%al an a%%ay cannot consume mo%e than =9 7b. >onside% following p%og%am, which shows the
ma3imum numbe% of elements an a%%ay of type int, float and cha% can have in case of .mall memo%y model.
main( )
{
int i!1?=?" #
float f1=!@!" #
cha% s=AA!A" #
)
0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
www.technicalsymposium.com 2
A.
5ow do - w%ite code that %eads data at memo%y location specified by segment and offset?
*ns+ Bse pee7b( ) function. ,his function %etu%ns byte(s) %ead f%om specific segment and offset locations in memo%y.
,he following p%og%am illust%ates use of this function. -n this p%og%am f%om C:B memo%y we have %ead cha%acte%s and
its att%ibutes of the fi%st %ow. ,he info%mation sto%ed in file is then fu%the% %ead and displayed using pee7( ) function.
Dinclude Estdio.hF
Dinclude Edos.hF
main( )
{
cha% fa% 4sc% = 03G@000000 #
H-<I 4fp #
int offset #
cha% ch #
if ( ( fp = fopen ( (sc%.dat(, (wb( ) ) == 6B<< )
{
p%intf ( (JnBnable to open file( ) #
e3it( ) #
)
KK %eads and w%ites to file
fo% ( offset = 0 # offset E 1=0 # offset$$ )
fp%intf ( fp, ('c(, pee7b ( sc%, offset ) ) #
fclose ( fp ) #
if ( ( fp = fopen ( (sc%.dat(, (%b( ) ) == 6B<< )
{
p%intf ( (JnBnable to open file( ) #
e3it( ) #
)
KK %eads and w%ites to file
fo% ( offset = 0 # offset E 1=0 # offset$$ )
{
fscanf ( fp, ('c(, Lch ) #
p%intf ( ('c(, ch ) #
)
fclose ( fp ) #
)
0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
www.technicalsymposium.com 3
=.
5ow do - compa%e cha%acte% data sto%ed at two diffe%ent memo%y locations?
*ns+ .ometimes in a p%og%am we %eMui%e to compa%e memo%y %anges containing st%ings. -n such a situation we can
use functions li7e memcmp( ) o% memicmp( ). ,he basic diffe%ence between two functions is that memcmp( ) does a
case0sensitive compa%ison whe%eas memicmp( ) igno%es case of cha%acte%s. Hollowing p%og%am illust%ates the use of
both the functions.

Dinclude Emem.hF
main( )
{
cha% 4a%%1 = (Nicit( #
cha% 4a%%1 = (7icit6agpu%( #
int c #
c = memcmp ( a%%1, a%%1, si/eof ( a%%1 ) ) #
if ( c == 0 )
p%intf ( (Jn.t%ings a%%1 and a%%1 compa%ed using memcmp a%e identical( ) #
else
p%intf ( (Jn.t%ings a%%1 and a%%1 compa%ed using memcmp a%e not identical(
) #
c = memicmp ( a%%1, a%%1, si/eof ( a%%1 ) ) #
if ( c == 0 )
p%intf ( (Jn.t%ings a%%1 and a%%1 compa%ed using memicmp a%e identical( )
#
else
p%intf ( (Jn.t%ings a%%1 and a%%1 compa%ed using memicmp a%e not
identical( ) #
)
0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
?.
Hi3ed0si/e obOects a%e mo%e app%op%iate as compa%ed to va%iable si/e data obOects. Bsing va%iable0si/e data obOects
saves ve%y little space. Ca%iable si/e data obOects usually have some ove%head. Panipulation of fi3ed0si/e data
obOects is usually faste% and easie%. Bse fi3ed si/e when ma3imum si/e is clea%ly bounded and close to ave%age. *nd
use va%iable0si/e data obOects when a few of the data items a%e bigge% than the ave%age si/e. Ho% e3ample,
cha% 4num10" = { (;ne(, (,wo(, (,h%ee(, (Hou%(,
(Hive(, (.i3(, (.even(, (Iight(, (6ine(, (,en( ) #
-nstead of using the above, use
cha% num10"=" = { (;ne(, (,wo(, (,h%ee(, (Hou%(,
(Hive(, (.i3(, (.even(, (Iight(, (6ine(, (,en( ) #
,he fi%st fo%m uses va%iable0si/e data obOects. -t allocates 10 pointe%s, which a%e pointing to 10 st%ing constants of
va%iable si/e. *ssuming each pointe% is of 9 bytes, it %eMui%es Q0 bytes. ;n the othe% hand, the second fo%m uses fi3ed
si/e data obOects. -t allocates 10 a%%ays of = cha%acte%s each. -t %eMui%es only =0 bytes of space. .o, the va%iable0si/e in
this case does not offe% any advantage ove% fi3ed si/e.
0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
www.technicalsymposium.com 4
@.
,he .pawnl( ) function...
:;. is a single tas7ing ope%ating system, thus only one p%og%am %uns at a time. ,he .pawnl( )
function p%ovides us with the capability of sta%ting the e3ecution of one p%og%am f%om within anothe% p%og%am. ,he
fi%st p%og%am is called the pa%ent p%ocess and the second p%og%am that gets called f%om within the fi%st p%og%am is called
a child p%ocess. ;nce the second p%og%am sta%ts e3ecution, the fi%st is put on hold until the second p%og%am completes
e3ecution. ,he fi%st p%og%am is then %esta%ted. ,he following p%og%am demonst%ates use of spawnl( ) function.
K4 Pult.c 4K
int main ( int a%gc, cha%4 a%gv " )
{
int a!", i, %et #
if ( a%gc E ! RR a%gc F ! )
{
p%intf ( (,oo many o% ,oo few a%guments...( ) #
e3it ( 0 ) #
)
fo% ( i = 1 # i E a%gc # i$$ )
ai" = atoi ( a%gvi" ) #
%et = a1" 4 a1" #
%etu%n %et #
)
K4 .pawn.c 4K
Dinclude Ep%ocess.hF
Dinclude Estdio.hF
main( )
{
int val #
val = spawnl ( STW*-,, (>+JJPult.e3e(, (!(, (10(,
(10(, 6B<< ) #
p%intf ( (JnUetu%ned value is+ 'd(, val ) #
)
5e%e, the%e a%e two p%og%ams. ,he p%og%am 2Pult.e3e2 wo%7s as a child p%ocess whe%eas 2.pawn.e3e2 wo%7s as a pa%ent
p%ocess. ;n e3ecution of 2.pawn.e3e2 it invo7es 2Pult.e3e2 and passes the command0line a%guments to it.
2Pult.e3e2 in tu%n on e3ecution, calculates the p%oduct of 10 and 10 and %etu%ns the value to val in 2.pawn.e3e2. -n ou%
call to spawnl( ) function, we have passed = pa%amete%s, STW*-, as the mode of e3ecution, path of 2.e3e2 file to %un as
child p%ocess, total numbe% of a%guments to be passed to the child p%ocess, list of command line a%guments
and 6B<<. STW*-, will cause ou% application to f%ee/e e3ecution until the child p%ocess has
completed its e3ecution. ,his pa%amete% needs to be passed as the default pa%amete% if you a%e
wo%7ing unde% :;.. unde% othe% ope%ating systems that suppo%t multitas7ing, this pa%amete% can be
ST6;W*-, o% ST;CIU<*V. ST6;W*-, will cause the pa%ent p%ocess to e3ecute along with the child
p%ocess, ST;CIU<*V will load the child p%ocess on top of the pa%ent p%ocess in the memo%y.
0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
www.technicalsymposium.com 5
Q.
*%e the following two statements identical?
cha% st%=" = (Nicit( #
cha% 4st% = (Nicit( #
*ns+ 6o8 *%%ays a%e not pointe%s. *n a%%ay is a single, p%e0allocated chun7 of contiguous elements
(all of the same type), fi3ed in si/e and location. * pointe% on the othe% hand, is a %efe%ence to any data element (of a
pa%ticula% type) located anywhe%e. * pointe% must be assigned to point to space allocated elsewhe%e, but it can
be %eassigned any time. ,he a%%ay decla%ation cha% st%=" # %eMuests that space fo% = cha%acte%s be set aside, to be 7nown
by name st%. -n othe% wo%ds the%e is a location named st% at which si3 cha%acte%s a%e sto%ed. ,he
pointe% decla%ation cha% 4st% # on the othe% hand, %eMuests a place that holds a pointe%, to be 7nown by the name st%. ,his
pointe% can point almost anywhe%e to any cha%, to any contiguous a%%ay of cha%s, o% nowhe%e.
0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
10.
-s the following code f%agment co%%ect?
const int 3 = 10 #
int a%%3" #
*ns+ 6o8 5e%e, the va%iable 3 is fi%st decla%ed as an int so memo%y is %ese%ved fo% it. ,hen it is
Mualified by a const Mualifie%. 5ence, const Mualified obOect is not a constant fully. -t is an obOect with %ead only
att%ibute, and in >, an obOect associated with memo%y cannot be used in a%%ay dimensions.
0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
11.
5ow do - w%ite code to %et%ieve cu%%ent date and time f%om the system and display it as a st%ing?
*ns+ Bse time( ) function to get cu%%ent date and time and then ctime( ) function to display it as a
st%ing. ,his is shown in following code snippet.
Dinclude EsysJtypes.hF
void main( )
{
timeTt cu%time #
cha% ctmA0" #
time ( Lcu%time ) # KK%et%ieves cu%%ent time L
sto%es in cu%time
p%intf ( (Jn>u%%ent :ate L ,ime+ 's(, ctime (
Lcu%time ) ) #
)
0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
11.
5ow do - change the type of cu%so% and hide a cu%so%?
*ns+ We can change the cu%so% type by using function Tsetcu%so%type( ). ,his function can change the cu%so% type to
solid cu%so% and can even hide a cu%so%. Hollowing code shows how to change the cu%so% type and hide cu%so%.
Dinclude Econio.hF
main( )
www.technicalsymposium.com 6
{
K4 5ide cu%so% 4K
Tsetcu%so%type ( T6;>BU.;U ) #
K4 >hange cu%so% to a solid cu%so% 4K
Tsetcu%so%type ( T.;<-:>BU.;U ) #
K4 >hange bac7 to the no%mal cu%so% 4K
Tsetcu%so%type ( T6;UP*<>BU.;U ) #
)
0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
1!.
5ow do - w%ite code that would get e%%o% numbe% and display e%%o% message if any standa%d e%%o%
occu%s?
*ns+ Hollowing code demonst%ates this.
Dinclude Estdio.hF
Dinclude Estdlib.hF
Dinclude Ee%%no.hF
main( )
{
cha% 4e%%msg #
H-<I 4fp #
fp = fopen ( (>+Jfile.t3t(, (%( ) #
if ( fp == 6B<< )
{
e%%msg = st%e%%o% ( e%%no ) #
p%intf ( (Jn's(, e%%msg ) #
)
)
5e%e, we a%e t%ying to open 2file.t3t2 file. 5oweve%, if the file does not e3ist, then it would cause an e%%o%. *s a %esult, a
value (in this case 1) %elated to the e%%o% gene%ated would get set in e%%no. e%%no is an e3te%nal int va%iable decla%ed
in 2stdlib.h2 and also in 2e%%no.h2. 6e3t, we have called ste%%o%( ) function which ta7es an e%%o% numbe% and %etu%ns a
pointe% to standa%d e%%o% message %elated to the given e%%o% numbe%.
0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
19.
5ow do - w%ite code to get the cu%%ent d%ive as well as set the cu%%ent d%ive?
*ns+ ,he function getdis7( ) %etu%ns the d%ive numbe% of cu%%ent d%ive. ,he d%ive numbe% 0 indicates 2*2 as the cu%%ent
d%ive, 1 as 2G2 and so on. ,he .etdis7( ) function sets the cu%%ent d%ive. ,his function ta7es one a%gument which is
an intege% indicating the d%ive to be set. Hollowing p%og%am demonst%ates use of both the functions.
Dinclude Edi%.hF
main( )
{
int dno, ma3d% #
dno = getdis7( ) #
p%intf ( (Jn,he cu%%ent d%ive is+ 'cJn(, =A $ dno
) #
www.technicalsymposium.com 7
ma3d% = setdis7 ( ! ) #
dno = getdis7( ) #
p%intf ( (Jn6ow the cu%%ent d%ive is+ 'cJn(, =A $
dno ) #
)
0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
1A.
,he functions memcmp( ) and memicmp( )
,he functions memcmp( ) and memicmp( ) compa%es fi%st n bytes of given two bloc7s of memo%y o%
st%ings. 5oweve%, memcmp( ) pe%fo%ms compa%ison as unsigned cha%s whe%eas memicmp( ) pe%fo%ms
compa%ison as cha%s but igno%es case (i.e. uppe% o% lowe% case). Goth the functions %etu%n an intege% value whe%e 0
indicates that two memo%y buffe%s compa%ed a%e identical. -f the value %etu%ned is g%eate% than 0 then it indicates that
the fi%st buffe% is bigge% than the second one. ,he value less than 0 indicate that the fi%st buffe% is less than the second
buffe%. ,he following code snippet demonst%ates use of both
Dinclude Estdio.hF
Dinclude Emem.hF
main( )
{
cha% st%1" = (,his st%ing contains some
cha%acte%s( #
cha% st%1" = (this st%ing contains( #
int %esult #
%esult = memcmp ( st%1, st%1, st%len ( st%1 ) ) #
p%intf ( (JnUesult afte% comap%ing buffe% using
memcmp( )( ) #
show ( %esult ) #
%esult = memicmp ( st%1, st%1, st%len ( st%1 ) ) #
p%intf ( (JnUesult afte% comap%ing buffe% using
memicmp( )( ) #
show ( %esult ) #
)
show ( int % )
{
if ( % == 0 )
p%intf ( (Jn,he buffe% st%1 and st%1 hold
identical data( ) #
if ( % F 0 )
p%intf ( (Jn,he buffe% st%1 is bigge% than buffe%
st%1( ) #
if ( % E 0 )
p%intf ( (Jn,he buffe% st%1 is less than buffe%
st%1( ) #
)
0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
www.technicalsymposium.com 8
1=.
5ow do - w%ite code to find an amount of f%ee dis7 space available on cu%%ent d%ive?
*ns+ Bse getdf%ee( ) function as shown in follow code.
Dinclude Estdio.hF
Dinclude Estdlib.hF
Dinclude Edi%.hF
Dinclude Edos.hF
main( )
{
int d% # st%uct df%ee dis7 #
long f%eesp #
d% = getdis7( ) #
getdf%ee ( d% $ 1 , Ldis7 ) #
if ( dis7.dfTsclus == 03HHHH )
{
p%intf ( (Jngetdf%ee( ) function failedJn()#
e3it ( 1 ) #
)
f%eesp = ( long ) dis7.dfTavail
4 ( long ) dis7.dfTbsec
4 ( long ) dis7.dfTsclus #
p%intf ( (Jn,he cu%%ent d%ive 'c+ has 'ld bytes
available as f%ee spaceJn(, 2*2 $ d%, f%eesp ) #
)
0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
1?.
Bse of a%%ay indices...
-f we wish to sto%e a cha%acte% in a cha% va%iable ch and the cha%acte% to be sto%ed depends on the
value of anothe% va%iable say colo% (of type int), then the code would be as shown below+
switch ( colo% )
{
case 0 +
ch = 2U2 #
b%ea7 #
case 1 +
ch = 2W2 #
b%ea7 #
case 1 +
ch = 2G2 #
b%ea7 #
)
-n place of switch0case we can ma7e use of the value in colo% as an inde3 fo% a cha%acte% a%%ay. 5ow to do this is shown
in following code snippet.
cha% 4st% = (UWG2 #
cha% ch #
www.technicalsymposium.com 9
int colo% #
KK code
ch = st% colo% " #
0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
1@.
Hunction ate3it( ) %ecevies pa%amete% as the add%ess of function of the type void fun ( void ). ,he function whose
add%ess is passed to ate3it( ) gets called befo%e the te%mination of p%og%am. -f ate3it( ) is called fo% mo%e than one
function then the functions a%e called in (fi%st in last out( o%de%. Vou can ve%ify that f%om the output.
Dinclude Estdio.hF
Dinclude Estdlib.hF
void fun1( )
{
p%intf((-nside fun1Jn()#
)

void fun1( )
{
p%intf((-nside fun1Jn()#
)
main( )
{
ate3it ( fun1 ) #
K4 some code 4K
ate3it ( fun1 ) #
p%intf ( (,his is the last statement of
p%og%am?Jn( )#
)
0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
1Q.
5ow do - w%ite a use%0defined function, which deletes each cha%acte% in a st%ing st%1, which matches any cha%acte% in
st%ing st%1?
*ns+ ,he function is as shown below+
>omp%ess ( cha% st%1", cha% st%1" )
{
int i, O, 7 #
fo% ( i = 7 = 0 # st%1i" 8= XJ0Y # i$$ )
{
fo% ( O = 0 # st%1O" 8= XJ0Y LL st%1O" 8=
st%1i" # O$$ )
#
if ( st%1O" == XJ0Y )
st%17$$" = st%1-" #
)
st%17" = XJ0Y
)
0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
www.technicalsymposium.com 10
10.
5ow does f%ee( ) 7now how many bytes to f%ee?
*ns+ ,he malloc( ) K f%ee( ) implementation %emembe%s the si/e of each bloc7 allocated and %etu%ned, so it is not
necessa%y to %emind it of the si/e when f%eeing.
0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
11.
What is the use of %andomi/e( ) and s%and( ) function?
*ns+ While gene%ating %andom numbe%s in a p%og%am, sometimes we %eMui%e to cont%ol the se%ies of
numbe%s that %andom numbe% gene%ato% c%eates. ,he p%ocess of assigning the %andom numbe% gene%ato%s
sta%ting numbe% is called seeding the gene%ato%. ,he %andomi/e( ) and s%and( ) functions a%e used to seed the %andom
numbe% gene%ato%s. ,he %andomi/e( ) function uses S>2s cloc7 to p%oduce a %andom seed, whe%eas the s%and( ) function
allows us to specify the %andom numbe% gene%ato%2s sta%ting value.
0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
11.
5ow do - dete%mine amount of memo%y cu%%ently available fo% allocating?
*ns+ We can use function co%eleft( ) to get the amount of memo%y available fo% allocation. 5oweve%, this function does
not give an e3act amount of unused memo%y. -f, we a%e using a small memo%y model, co%eleft( ) %etu%ns the amount of
unused memo%y between the top of the heap and stac7. -f we a%e using a la%ge% model, this function %etu%ns the amount
of memo%y between the highest allocated memo%y and the end of conventional memo%y. ,he function %etu%ns amount
of memo%y in te%ms of bytes.
0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
1!.
5ow does a > p%og%am come to 7now about command line a%guments?
*ns+ When we e3ecute ou% > p%og%am, ope%ating system loads the p%og%am into memo%y. -n case of
:;., it fi%st loads 1A= bytes into memo%y, called p%og%am segment p%efi3. ,his contains file table,
envi%onment segment, and command line info%mation. When we compile the > p%og%am the compile% inse%ts
additional code that pa%ses the command, assigning it to the a%gv a%%ay, ma7ing the a%guments easily
accessible within ou% > p%og%am.
0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
19.
When we open a file, how does functions li7e f%ead( )Kfw%ite( ), etc. get to 7now f%om whe%e to %ead o% to w%ite the
data?
*ns+ When we open a file fo% %eadKw%ite ope%ation using function li7e fopen( ), it %etu%ns a pointe% to the st%uctu%e of
type H-<I. ,his st%uctu%e sto%es the file pointe% called position pointe%, which 7eeps t%ac7 of cu%%ent location within the
file. ;n opening file fo% %eadKw%ite ope%ation, the file pointe% is set to the sta%t of the file. Iach time
we %eadKw%ite a cha%acte%, the position pointe% advances one cha%acte%. -f we %ead one line of te3t at a step f%om the file,
then file pointe% advances to the sta%t of the ne3t line. -f the file is opened in append mode, the file pointe% is
placed at the ve%y end of the file. Bsing fsee7( ) function we can set the file pointe% to some othe%
place within the file.
0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
www.technicalsymposium.com 11
1A.
,he si/eof( ) function doesnYt %etu%n the si/e of the bloc7 of memo%y pointed to by a pointe%. Why?
*ns+ ,he si/eof( ) ope%ato% does not 7now that malloc( ) has been used to allocate a pointe%. si/eof( ) gives us the si/e
of pointe% itself. ,he%e is no handy way to find out the si/e of a bloc7 allocated by malloc( ).
0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
1=.
HST.IW *nd HST;HHZ
.ometimes while wo%7ing with fa% pointe%s we need to b%ea7 a fa% add%ess into its segment and offset. -n such
situations we can use HST.IW and HST;HH mac%os. Hollowing p%og%am illust%ates the use of these two mac%os.
Dinclude Edos.hF
main( )
{
unsigned s, o #
cha% fa% 4pt% = (5ello8( #
s = HST.IW ( pt% ) #
o = HST;HH ( pt% ) #
p%intf ( (Jn'u 'u(, s, o ) #
)
0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
1?.
5ow do - w%ite a p%og%am to conve%t a st%ing containing numbe% in a he3adecimal fo%m to its
eMuivalent decimal?
*ns+ ,he following p%og%am demonst%ates this+
main( )
{
cha% st%" = (0*G( #
int h, he3, i, n #
n = 0 # h = 1 #
fo% ( i = 0 # h == 1 # i$$ )
{
if ( st%i" F= 202 LL st%i" E= 2Q2 )
he3 = st%i" 0 202 #
else
{
if ( st%i" F= 2a2 LL st%i" E= 2f2 )
he3 = st%i" 0 2a2 $ 10 #
else
if ( st%i" F= 2*2 LL st%i" E= 2H2 )
he3 = st%i" 0 2*2 $ 10 #
else
h = 0 #
)
if ( h == 1 )
n = 1= 4 n $ he3 #
)
p%intf ( (Jn,he decimal eMuivalent of 's is 'd(,
st%, n ) #
)
,he output of this p%og%am would be the decimal eMuivalent of 0*G is 1?1.
0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
www.technicalsymposium.com 12
1@.
5ow do - w%ite code that %eads the segment %egiste% settings?
*ns+ We can use seg%ead( ) function to %ead segment %egiste% settings. ,he%e a%e fou% segment
%egiste%s[code segment, data segment, stac7 segment and e3t%a segment. .ometimes when we use
:;. and G-;. se%vices in a p%og%am we need to 7now the segment %egiste%2s value. -n such a situation
we can use seg%ead( ) function. ,he following p%og%am illust%ates the use of this function.
Dinclude Edos.hF
main( )
{
st%uct .UIW. s #
seg%ead ( Ls ) #
p%intf ( (Jn>.+ '\ :.+ '\ ..+ '\ I.+ '\(,s.cs,
s.ds, s.ss, s.es ) #
)
0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
1Q.
What is envi%onment and how do - get envi%onment fo% a specific ent%y?
*ns+ While wo%7ing in :;., it sto%es info%mation in a memo%y %egion called envi%onment. -n this
%egion we can place configu%ation settings such as command path, system p%ompt, etc. .ometimes in a
p%og%am we need to access the info%mation contained in envi%onment. ,he function getenv( ) can be used when we
want to access envi%onment fo% a specific ent%y. Hollowing p%og%am demonst%ates the use of this function.
Dinclude Estdio.hF
Dinclude Estdlib.hF
main( )
{
cha% 4path = 6B<< #
path = getenv ( (S*,5( ) #
if ( 4path 8= 6B<< )
p%intf ( (JnSath+ 's(, path ) #
else
p%intf ( (JnSath is not set( ) #
)
0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
!0.
5ow do - display cu%%ent date in the fo%mat given below?
.atu%day ;ctobe% 11, 1001
*ns+ Hollowing p%og%am illust%ates how we can display date in above given fo%mat.
Dinclude Estdio.hF
Dinclude Etime.hF
main( )
{
st%uct tm 4cu%time #
timeTt dtime #
cha% st%!0" #
www.technicalsymposium.com 13
time ( Ldtime ) #
cu%time = localtime ( Ldtime ) #
st%ftime ( st%, !0, ('* 'G 'd, 'V(, cu%time ) #
p%intf ( (Jn's(, st% ) #
)
5e%e we have called time( ) function which %etu%ns cu%%ent time. ,his time is %etu%ned in te%ms of
seconds, elapsed since 00+00+00 WP,, ]anua%y 1, 1Q?0. ,o e3t%act the wee7 day, day of month, etc.
f%om this value we need to b%ea7 down the value to a tm st%uctu%e. ,his is done by the function
localtime( ). ,hen we have called st%ftime( ) function to fo%mat the time and sto%e it in a st%ing st%.
0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
!1.
-f we have decla%ed an a%%ay as global in one file and we a%e using it in anothe% file then why doesn2t the si/eof ope%ato%
wo%7s on an e3te%n a%%ay?
*ns+ *n e3te%n a%%ay is of incomplete type as it does not contain the si/e. 5ence we cannot use
si/eof ope%ato%, as it cannot get the si/e of the a%%ay decla%ed in anothe% file. ,o %esolve this use any of one the
following two solutions+
1. -n the same file decla%e one mo%e va%iable that holds the si/e of a%%ay. Ho% e3ample,
a%%ay.c
int a%%A" #
int a%%s/ = si/eof ( a%% ) #
myp%og.c
e3te%n int a%%" #
e3te%n int a%%s/ #
1. :efine a mac%o which can be used in an a%%ay
decla%ation. Ho% e3ample,
myheade%.h
Ddefine .^ A
a%%ay.c
Dinclude (myheade%.h(
int a%%.^" #
myp%og.c
Dinclude (myheade%.h(
e3te%n int a%%.^" #
0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
www.technicalsymposium.com 14
!1.
5ow do - w%ite p%intf( ) so that the width of a field can be specified at %untime?
*ns+ ,his is shown in following code snippet.
main( )
{
int w, no #
p%intf ( (Inte% numbe% and the width fo% the
numbe% field+( ) #
scanf ( ('d'd(, Lno, Lw ) #
p%intf ( ('4d(, w, no ) #
)
5e%e, an 242 in the fo%mat specifie% in p%intf( ) indicates that an int value f%om the a%gument list
should be used fo% the field width.
0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
!!.
5ow to find the %ow and column dimension of a given 10: a%%ay?
*ns+ Wheneve% we initiali/e a 10: a%%ay at the same place whe%e it has been decla%ed, it is not
necessa%y to mention the %ow dimension of an a%%ay. ,he %ow and column dimensions of such an
a%%ay can be dete%mined p%og%ammatically as shown in following p%og%am.
void main( )
{
int a"!" = { 0, 1, 1,
Q,0=, @,
?, A, 99,
1!, 11,1A ) #
int c = si/eof ( a0" ) K si/eof ( int ) #
int % = ( si/eof ( a ) K si/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 (, ai"O" ) #
p%intf ( (Jn( ) #
)
)
0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
!9.
,he access( ) function...
,he access( ) function chec7s fo% the e3istence of a file and also dete%mines whethe% it can be %ead,
w%itten to o% e3ecuted. ,his function ta7es two a%guments the filename and an intege% indicating the access mode. ,he
values =, 9, 1, and 1 chec7s fo% %eadKw%ite, %ead, w%ite and e3ecute pe%mission of a given file, whe%eas value 0 chec7s
whethe% the file e3ists o% not. Hollowing p%og%am demonst%ates how we can use access( ) function to
chec7 if a given file e3ists.
www.technicalsymposium.com 15
Dinclude Eio.hF
main( )
{
cha% fname=?" #
p%intf ( (JnInte% name of file to open( ) #
gets ( fname ) #
if ( access ( fname, 0 ) 8= 0 )
{
p%intf ( (JnHile does not e3ist.( ) #
%etu%n #
)
)
0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
!A.
5ow do - conve%t a floating0point numbe% to a st%ing?
*ns+ Bse function gcvt( ) to conve%t a floating0point numbe% to a st%ing. Hollowing p%og%am demonst%ates the use of
this function.
Dinclude Estdlib.hF
main( )
{
cha% st%1A" #
float no #
int dg = A # K4 significant digits 4K
no = 19.!11= #
gcvt ( no, dg, st% ) #
p%intf ( (.t%ing+ 'sJn(, st% ) #
)
0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
!=.
What is a stac7 ?

*ns+ ,he stac7 is a %egion of memo%y within which ou% p%og%ams tempo%a%ily sto%e data as they e3ecute. Ho%
e3ample, when a p%og%am passes pa%amete%s to functions, > places the pa%amete%s on the stac7. When the function
completes, > %emoves the items f%om the stac7. .imila%ly, when a function decla%es local va%iables, > sto%es the
va%iable2s values on the stac7 du%ing the function2s e3ecution. :epending on the p%og%am2s use of functions and
pa%amete%s, the amount of stac7 space that a p%og%am %eMui%es will diffe%.
0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
!?.
*llocating memo%y fo% a !0: a%%ay

Dinclude (alloc.h(
Ddefine P*\\ !
Ddefine P*\V 9
Ddefine P*\^ A
main( )
{
int 444p, i, O, 7 #
www.technicalsymposium.com 16
p = ( int 444 ) malloc ( P*\\ 4 si/eof ( int 44 ) ) #
fo% ( i = 0 # i E P*\\ # i$$ )
{
pi" = ( int 44 ) malloc ( P*\V 4 si/eof ( int 4 ) ) #
fo% ( O = 0 # O E P*\V # O$$ )
pi"O" = ( int 4 ) malloc ( P*\^ 4 si/eof ( int ) ) #
)
fo% ( 7 = 0 # 7 E P*\^ # 7$$ )
{
fo% ( i = 0 # i E P*\\ # i$$ )
{
fo% ( O = 0 # O E P*\V # O$$ )
{
pi"O"7" = i $ O $ 7 #
p%intf ( ('d (, pi"O"7" ) #
)
p%intf ( (Jn( ) #
)
p%intf ( (JnJn( ) #
)
)
:ata .t%uctu%es
5ow to distinguish between a bina%y t%ee and a t%ee?

*ns+ * node in a t%ee can have any numbe% of b%anches. While a bina%y t%ee is a t%ee st%uctu%e in which any node can
have at most two b%anches. Ho% bina%y t%ees we distinguish between the subt%ee on the left and subt%ee on the %ight,
whe%eas fo% t%ees the o%de% of the subt%ees is i%%elevant.
>onside% the following figu%e...
,his above figu%e shows two bina%y t%ees, but these bina%y t%ees a%e diffe%ent. ,he fi%st has an empty %ight subt%ee while
the second has an empty left subt%ee. -f the above a%e %ega%ded as t%ees (not the bina%y t%ees), then they a%e same despite
the fact that they a%e d%awn diffe%ently. *lso, an empty bina%y t%ee can e3ist, but the%e is no t%ee having /e%o nodes.
0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
!@.
5ow do - use the function lde3p( ) in a p%og%am?
*ns+ ,he math function lde3p( ) is used while solving the comple3 mathematical eMuations. ,his function ta7es two
a%guments, a double value and an int %espectively. ,he o%de% in which lde3p( ) function pe%fo%ms calculations is ( n 4
pow ( 1, e3p ) ) whe%e n is the double value and e3p is the intege%. ,he following p%og%am demonst%ates the use of this
function.
Dinclude Estdio.hF
Dinclude Emath.hF
void main( )
{
double ans #
double n = 9 #
ans = lde3p ( n, 1 ) #
p%intf ( (Jn,he lde3p value is + 'lfJn(, ans ) #
)
5e%e, lde3p( ) function would get e3panded as ( 9 4 1 4 1 ), and the output would be the lde3p value is + 1=.000000
0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
www.technicalsymposium.com 17
!Q.
>an we get the mantissa and e3ponent fo%m of a given numbe%?
*ns+ ,he function f%e3p( ) splits the given numbe% into a mantissa and e3ponent fo%m. ,he function ta7es two
a%guments, the numbe% to be conve%ted as a double value and an int to sto%e the e3ponent fo%m. ,he function %etu%ns the
mantissa pa%t as a double value. Hollowing e3ample demonst%ates the use of this function.
Dinclude Emath.hF
Dinclude Estdio.hF
void main( )
{
double mantissa, numbe% #
int e3ponent #
numbe% = @.0 #
mantissa = f%e3p ( numbe%, Le3ponent ) #
p%intf ( (,he numbe% 'lf is (, numbe% ) #
p%intf ( ('lf times two to the (, mantissa ) #
p%intf ( (powe% of 'dJn(, e3ponent ) #
%etu%n 0 #
)
0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
90.
5ow do - w%ite code that e3ecutes ce%tain function only at p%og%am te%mination?
*ns+ Bse ate3it( ) function as shown in following p%og%am.
Dinclude Estdlib.hF
main( )
{
int ch #
void fun ( void ) #
ate3it ( fun ) #
KK code
)
void fun( void )
{
p%intf ( (Jn,e%minate p%og%am......( ) #
getch( ) #
)
0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
91.
What a%e memo%y models?
*ns+ ,he compile% uses a memo%y model to dete%mine how much memo%y is allocated to the p%og%am. ,he S> divides
memo%y into bloc7s called segments of si/e =9 NG. Bsually, p%og%am uses one segment fo% code and a second segment
fo% data. * memo%y model defines the numbe% of segments the compile% can use fo% each. -t is impo%tant to 7now which
memo%y model can be used fo% a p%og%am. -f we use w%ong memo%y model, the p%og%am might not have enough
memo%y to e3ecute. ,he p%oblem can be solved using la%ge% memo%y model. 5oweve%, la%ge% the memo%y
model, slowe% is you% p%og%am e3ecution. .o we must choose the smallest memo%y model that
satisfies ou% p%og%am needs. Post of the compile%s suppo%t memo%y models li7e tiny, small, medium, compact, la%ge
and huge.
0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
www.technicalsymposium.com 18
91.
5ow does > compile% sto%e elements in a multi0dimensional a%%ay?
*ns+ ,he compile% maps multi0dimensional a%%ays in two ways[Uow maOo% o%de% and >olumn o%de%. When
the compile% places elements in columns of an a%%ay fi%st then it is called column0maOo% o%de%. When the compile%
places elements in %ows of an a%%ay fi%st then it is called %ow0maOo% o%de%. > compile%s sto%e multidimensional a%%ays in
%ow0maOo% o%de%. Ho% e3ample, if the%e is a multi0dimensional a%%ay a1"!", then acco%ding %ow0maOo% o%de%, the
elements would get sto%ed in memo%y following o%de%+
a0"0", a0"1", a0"1", a1"0", a1"1", a1"1"
0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
9!.
-f the %esult of an Te3p%ession has to be sto%ed to one of two va%iables, depending on a condition, can we use
conditional ope%ato%s as shown below?
( ( i E 10 ) ? O + 7 ) = l 4 1 $ p #
*ns+ 6o8 ,he above statement is invalid. We cannot use the conditional ope%ato%s in this fashion. ,he conditional
ope%ato%s li7e most ope%ato%s, yields a value, and we cannot assign the value of an Te3p%ession to a value. 5oweve%, we
can use conditional ope%ato%s as shown in following code snippet.
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 as given below+
i = A O = 1= 7 = 11 l = 1
0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
99.
5ow can - find the day of the wee7 of a given date?
*ns+ ,he following code snippet shows how to get the day of wee7 f%om the given date.
dayofwee7 ( int yy, int mm, int dd )
{
K4Ponday = 1 and .unday = 0 4K
K4 month numbe% F= 1 and E= 11, yy F 1?A1 o% so 4K
static int a%% " = { 0, !, 1, A, 0, !, A, 1, 9, =, 1, 9 ) #
yy = yy 0 mm E ! #
%etu%n ( yy $ yy K 9 0 yy K 100 $ yy K 900 $ a%% mm 0 1" $ dd ) ' ? #
)
void main( )
{
p%intf ( (JnJnJn:ay of wee7 + 'd (, dayofwee7 ( 1001, A, 1@ ) ) #
)
0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
www.technicalsymposium.com 19
9A.
What2s the diffe%ence between these two decla%ations?
st%uct st%1 { ... ) #
typedef st%uct { ... ) st%1 #
*ns + ,he fi%st fo%m decla%es a st%uctu%e tag whe%eas the second decla%es a typedef. ,he main diffe%ence is that the
second decla%ation is of a slightly mo%e abst%act type 00 its use%s don2t necessa%ily 7now that it is a st%uctu%e, and the
7eywo%d st%uct is not used when decla%ing instances of it.
0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
9=.
5ow do - p%int the contents of envi%onment va%iables?
*ns+. ,he following p%og%am shows how to achieve this+
main( int a%gc, cha% 4a%gv ", cha% 4env " )
{
int i = 0 #
cl%sc%( ) #
while ( env i " )
p%intf ( (Jn's(, env i$$ " ) #
)

main( ) has the thi%d command line a%gument env, which is an a%%ay of pointe%s to the st%ings. Iach pointe% points to
an envi%onment va%iable f%om the list of envi%onment va%iables.
0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
9?.
div( )...
,he function div( ) divides two intege%s and %etu%ns the Muotient and %emainde%. ,his function ta7es two intege% values
as a%guments# divides fi%st intege% with the second one and %etu%ns the answe% of division of type divTt. ,he data type
divTt is a st%uctu%e that contains two long ints, namely Muot and %em, which sto%e Muotient and %emainde% of division
%espectively. ,he following e3ample shows the use of div( ) function.
Dinclude Estdlib.hF
void main( )
{
divTt %es #
%es = div ( !1, A ) #
p%intf ( (Jn,he Muotient = 'd and %emainde% = 'd (, %es.Muot, %es.%em ) #
)
0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
9@.
What would the second and the thi%d p%intf( ) output the following p%og%am?
main( )
{
cha% 4st% " = {
(Wood Po%ning(
(Wood Ivening(
(Wood *fte%noon(
) #
p%intf ( (JnHi%st st%ing = 's(, st%0" ) #
p%intf ( (Jn.econd st%ing = 's(, st%1" ) #
www.technicalsymposium.com 20
p%intf ( (Jn,hi%d st%ing = 's(, st%1" ) #
)
*ns+ Ho% the above given p%og%am, we e3pect the output as Wood Ivening and Wood *fte%noon, fo% the second and
thi%d p%intf( ). 5oweve%, the output would be as shown below.
Hi%st st%ing = Wood Po%ningWood IveningWood *fte%noon
.econd st%ing = ( null )
,hi%d st%ing =
What is missing in the above given code snippet is a comma sepa%ato% which should sepa%ate the st%ings Wood
Po%ning, Wood Ivening and Wood *fte%noon. ;n adding comma, we would get the output as shown below.
Hi%st st%ing = Wood Po%ning
.econd st%ing = Wood Ivening
,hi%d st%ing = Wood *fte%noon
0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
9Q.
5ow do - use scanf( ) to %ead the date in the fo%m 2dd0mm0yy2 ?
*ns+ ,he%e a%e two ways to %ead the date in the fo%m of 2dd0mm0yy2 one possible way is...
int dd, mm, yy #
cha% ch # K4 fo% cha% 202 4K
p%intf ( (JnInte% the date in the fo%m of dd0mm0yy + ( ) #
scanf( ('d'c'd'c'd(, Ldd, Lch, Lmm, Lch, Lyy ) #
*nd anothe% best way is to use supp%ession cha%acte% 4 as...
int dd, mm, yy #
scanf( ('d'4c'd'4c'd(, Ldd, Lmm, Lyy ) #
,he supp%ession cha%acte% 4 supp%esses the input %ead f%om the standa%d input buffe% fo% the assigned cont%ol cha%acte%.
0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
A0.
5ow do - p%int a floating0point numbe% with highe% p%ecision say 1!.!9A=@?!9 with only p%ecision up to two decimal
places?
*ns+ ,his can be achieved th%ough the use of supp%ession cha% 242 in the fo%mat st%ing of p%intf( ) as shown in the
following p%og%am.
main( )
{
int i = 1 #
float f = 1!.!9A=@?!9 #
p%intf ( ('.4f(, i, f ) #
)
,he output of the above p%og%am would be 1!.!A.
0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
A1.
*%e the e3p%essions 4pt%$$ and $$4pt% same?
*ns+ 6o. 4pt%$$ inc%ements the pointe% and not the value pointed by it, whe%eas $$4pt% inc%ements the value being
pointed to by pt%.
0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
www.technicalsymposium.com 21
A1.
st%pb%7( )
,he function st%pb%7( ) ta7es two st%ings as pa%amete%s. -t scans the fi%st st%ing, to find, the fi%st occu%%ence of any
cha%acte% appea%ing in the second st%ing. ,he function %etu%ns a pointe% to the fi%st occu%%ence of the cha%acte% it found
in the fi%st st%ing. ,he following p%og%am demonst%ates the use of st%ing function st%pb%7( ).
Dinclude Est%ing.hF
main( )
{
cha% 4st%1 = (5ello8( #
cha% 4st%1 = (Gette%( #
cha% 4p #
p = st%pb%7 ( st%1, st%1 ) #
if ( p )
p%intf ( (,he fi%st cha%acte% found in st%1 is 'c(, 4p ) #
else
p%intf ( (,he cha%acte% not found( ) #
)
,he output of the above p%og%am would be the fi%st cha%acte% found in st%1 is e
0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
A!.
>an we conve%t an unsigned long intege% value to a st%ing?
*ns+ ,he function ultoa( ) can be used to conve%t an unsigned long intege% value to a st%ing. ,his function ta7es th%ee
a%guments, fi%st the value that is to be conve%ted, second the base add%ess of the buffe% in which the conve%ted numbe%
has to be sto%ed (with a st%ing te%minating null cha%acte% 2J02) and the last a%gument specifies the base to be used in
conve%ting the value. Hollowing e3ample demonst%ates the use of this function.
Dinclude Estdlib.hF
void main( )
{
unsigned long ul = !1!9A=?1!1< #
cha% st%1A" #
ultoa ( ul, st%, 10 ) #
p%intf ( (st% = 's unsigned long = 'luJn(, st%, ul ) #
)
0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
A9.
ceil( ) and floo%( )
,he math function ceil( ) ta7es a double value as an a%gument. ,his function finds the smallest possible intege% to
which the given numbe% can be %ounded up. .imila%ly, floo%( ) being a math function, ta7es a double value as an
a%gument and %etu%ns the la%gest possible intege% to which the given double value can be %ounded down. ,he following
p%og%am demonst%ates the use of both the functions.
Dinclude Emath.hF
void main( )
{
double no = 19!?.1!1=? #
double down, up #
down = floo% ( no ) #
www.technicalsymposium.com 22
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 this p%og%am would be,
,he o%iginal numbe% 19!?.1!1=?
,he numbe% %ounded down 19!?.00000
,he numbe% %ounded up 19!@.00000
0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
AA.
5ow do - use function ecvt( ) in a p%og%am?
*ns+ ,he function ecvt( ) conve%ts a floating0point value to a null te%minated st%ing. ,his function ta7es fou% a%guments,
such as, the value to be conve%ted to st%ing, the numbe% of digits to be conve%ted to st%ing, and two intege% pointe%s. ,he
two0intege% pointe% sto%es the position of the decimal point (%elative to the st%ing) and the sign of the numbe%,
%espectively. -f the value in a va%iable, used to sto%e sign is 0, then the numbe% is positive and, if it is non0/e%o, then the
numbe% is negative. ,he function %etu%ns a pointe% to the st%ing containing digits. Hollowing p%og%am demonst%ates the
use of this function.

Dinclude Estdlib.hF
main( )
{
cha% 4st% #
double val #
int dec, sign #
int ndig = 9 #
val = 11 #
st% = ecvt ( val, ndig, Ldec, Lsign ) #
p%intf ( (st%ing = 's dec = 'd sign = 'dJn(, st%, dec, sign ) #
val = 0!9A.=? #
ndig = @ #
st% = ecvt ( val, ndig, Ldec, Lsign ) #
p%intf ( (st%ing = 's dec = 'd sign = 'dJn(, st%, dec, sign ) #
KK numbe% with a scientific notation
val = !.A9=?11eA #
ndig = A #
st% = ecvt ( val, ndig, Ldec, Lsign ) #
p%intf ( (st%ing = 's dec = 'd sign = 'dJn(, st%, dec, sign ) #
)
,he output of this p%og%am would be

st%ing = 1100 dec = 1 sign = 0
st%ing = !9A=?000 dec = ! sign = 1
st%ing = !A9=? dec = = sign = 0

0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
www.technicalsymposium.com 23
A=.
5ow to %un :-U command p%og%ammatically?

*ns+ We can use the system( ) function to e3ecute the :-U command along with its options. Hollowing p%og%am shows
how this can be achieved+

KK mydi%.c

main ( int a%gc, cha% 4a%gv " )
{
cha% st%!0" #

if ( a%gc E 1 )
e3it ( 0 ) #

sp%intf ( st%, (di% 's 's(, a%gv1", a%gv1" ) #
system ( st% ) #
)

-f we %un the e3ecutable file of this p%og%am at command p%ompt passing the command line a%guments as follows+

F mydi% abc.c Ks

,his will sea%ch the file 2abc.c2 in the cu%%ent di%ecto%y.
0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
A?.
.uppose - have a st%uctu%e having fields name, age, sala%y and have passed add%ess of age to a function fun( ). 5ow -
can access the othe% membe% of the st%uctu%e using the add%ess of age?
*ns+
st%uct emp
{
cha% name10" #
int age #
float sala%y #
) #
main( )
{
st%uct emp e #
p%intf ( (JnInte% name+ ( ) #
scanf ( ('s(, e.name ) #
p%intf ( (JnInte% age+ ( ) #
scanf ( ('d(, Le.age ) #
p%intf ( (JnInte% sala%y+ ( ) #
scanf ( ('f(, Le.sala%y ) #
fun ( Le.age ) #
)
fun ( int 4p )
{
st%uct emp 4M #
int offset #
offset = ( cha% 4 ) ( L ( ( st%uct emp 4 ) 0 ) 0F age ) 0 ( cha% 4 ) ( (
st%uct emp4 ) 0 ) #
M = ( st%uct emp 4 ) ( ( cha% 4 ) p 0 offset ) #
p%intf ( (Jnname+ 's(, M 0F name ) #
www.technicalsymposium.com 24
p%intf ( (Jnage+ 'd(, M 0F age ) #
p%intf ( (Jnsala%y+ 'f(, M 0F sala%y ) #
)
0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
A@.
5ow to %est%ict the p%og%am2s output to a specific sc%een %egion?

*ns+ * > function window( ) can be used to %est%ict the sc%een output to a specific %egion. ,he window( ) function
defines a te3t0mode window. ,he pa%amete%s passed to this function defines the uppe%0left and lowe%0%ight co%ne% of the
%egion within which you want the output. -n the following p%og%am, the st%ing 25ello82 gets p%inted within the specified
%egion. ,o p%int the st%ing we must use cp%intf( ) function which p%ints di%ectly 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( ) #
)
0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
AQ.
.ometimes you need to p%ompt the use% fo% a passwo%d. When the use% types in the passwo%d, the cha%acte%s the use%
ente%s should not appea% on the sc%een. * standa%d lib%a%y function getpass( ) can be used to pe%fo%m such function.
Pa3imum numbe% of cha%acte%s that can be ente%ed as passwo%d is @.

main( )
{
cha% 4pwd #

pwd = getpass ( (Inte% Sasswo%d( ) #

if ( st%cmp ( pwd, (o%gcity( ) )
p%intf ( (JnSasswo%d 's is inco%%ect(, pwd ) #
else
p%intf ( (Jn>o%%ect Sasswo%d( ) #
)
0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
=0.
5ow to obtain the cu%%ent d%ive th%ough > ?

*ns+ We can use the function Tgetd%ive( ) to obtain the cu%%ent d%ive. ,he Tgetd%ive( ) function uses :;. function
0\1Q to get the cu%%ent d%ive numbe%

Dinclude Edi%ect.hF
main( )
{
int dis7 #
dis7 = Tgetd%ive( ) $ 2*2 0 1 #
p%intf ( (,he cu%%ent d%ive is+ 'cJn(, dis7 ) #
)
0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
www.technicalsymposium.com 25
=1.
5ow come the output fo% both the p%og%ams is diffe%ent when the logic is same?

main( )
{
int i, O #

fo% ( i = 1, O = 1 # i E= A, O E= 100 # i$$, O$$ )
{
goto3y ( 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$$ )
{
goto3y ( 1, 1 ) #
p%intf ( ('d 'd(, i, O ) #
)
)

;utput 0F A A
Iven if logic of both the p%og%ams is same the output of the fi%st p%og%am comes out to be 100, 100, but of the second
p%og%am it is A, A. ,he comma ope%ato% plays a vital %ole inside the fo% loop. -t always conside%s the value of the latest
va%iable. .o, at the time of testing the condition in fo% loop, the value of O will be conside%ed in the fi%st p%og%am and
value of i in the second.
0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
=1.
>an we get the 3 and y coo%dinate of the cu%%ent cu%so% position ?

*ns + ,he function whe%e3( ) and whe%ey( ) %etu%ns the 30coo%dinate and y0coo%dinate of the cu%%ent cu%so% position
%espectively. Goth the functions %etu%n an intege% value. ,he value %etu%ned by whe%e3( ) is the ho%i/ontal position of
cu%so% and the value %etu%ned by whe%ey( ) is the ve%tical position of the cu%so%. Hollowing p%og%am shows how to use
the whe%e3( ) and whe%ey( ) functions.

Dinclude Econio.hF
main( )
{
p%intf ( (]ustJn ,oJn ,estJn Whe%eJn the cu%so%Jn goes( ) #

p%intf ( (>u%%ent location is \+ 'd V+ 'dJn(, whe%e3( ), whe%ey( ) ) #
)
0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
=!.
5ow do - p%og%ammatically delete lines in the te3t window?

*ns+ While w%iting p%og%ams that pe%fo%m sc%een0based -K;, you may want to0delete the cu%%ent line2s contents,
moving one line up, all of the output that follows. -n such cases a function called delline( ) can be used. Hollowing
code snippet illust%ates the use of function delline( ).

Dinclude Econio.hF
www.technicalsymposium.com 26
main( )
{
int i #
cl%sc%( ) #

fo% ( i = 0# i E= 1!# i$$ )
p%intf ( (<ine 'dJ%Jn(, i ) #

p%intf ( (S%ess a 7ey to continue + ( ) #
getch( ) #

goto3y ( 1, = ) #

fo% ( i = =# i E= 11# i$$ )
delline( ) #

getch( ) #
)
0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
=9.
5ow do - get the time elapsed between two function calls ?

*ns+ ,he function difftime( ) finds the diffe%ence between two times. -t calculates the elapsed time in seconds and
%etu%ns the diffe%ence between two times as a double value.

Dinclude Etime.hF
Dinclude Estdio.hF
Dinclude Edos.hF

main( )
{
int a" = { 1, 0!9, A=, ?@, 111, !!, 0?, 11, 9A, 1Q, = ) #
int s #
timeTt t1, t1 # KK timeTt defines the value used fo% time function

s = si/eof ( a ) K 1 #
t1 = time ( 6B<< ) #
selTso%t ( a, s ) # KK so%t a%%ay by selection so%t
bubTso%t ( a, s ) # KK so%t a%%ay by bubble so%t method
t1 = time ( 6B<< ) #
p%intf ( (Jn,he diffe%ence between two function calls is 'f(, difftime (
t1, t1 ) ) #
)

-n the above p%og%am we have called difftime( ) function that %etu%ns the time elapsed f%om t1 to t1.
0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
=A.
5ow do - use swab( ) in my p%og%am ?

*ns+ ,he function swab( ) swaps the adOacent bytes of memo%y. -t copies the bytes f%om sou%ce st%ing to the ta%get
st%ing, p%ovided that the numbe% of cha%acte%s in the sou%ce st%ing is even. While copying, it swaps the bytes which a%e
then assigned to the ta%get st%ing.

www.technicalsymposium.com 27
Dinclude Estdlib.hF
Dinclude Estdio.hF
Dinclude Est%ing.hF

main ( )
{
cha% 4st%1 = (h. eesll snsiasl not eh es as ohe% ( #
cha% 4st%1 #
cl%sc%( ) #
swab ( st%1, st%1, st%len ( st%1 ) ) #
p%intf ( (,he ta%get st%ing is + 'sJn(, st%1 ) # KK output 00 .he sells
snails on the sea sho%e
getch( ) #
)

0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
==.
,u%bo > p%ovides va%ious command line compile% options which we can use th%ough ,>>. ,he compile% options
include + displaying specific wa%ning messages, gene%ating @0@? ha%dwa%e inst%uctions, using a filename fo%
gene%ating assembly code, etc. -nstead of compile% options being e3ecuted at command line we can use these compile%
options in ou% p%og%am. ,his can be achieved using Dp%agma options. We can use va%ious flags with Dp%agma
options to use the compile% options. *ll these flags a%e available in tu%bo >2s online help.
0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
=?.
- have an a%%ay decla%ed in file 2H1.>2 as,
int a " = { 1, 1, !, 9, A, = ) #
and used in the file 2H1.>2 as,
e3te%n int a " #
-n the file H1.>, why si/eof doesn2t wo%7 on the a%%ay a "?
*ns+ *n e3te%n a%%ay of unspecified si/e is an incomplete type. Vou cannot apply si/eof to it, because si/eof ope%ates
du%ing compile time and it is unable to lea%n the si/e of an a%%ay that is defined in anothe% file. Vou have th%ee ways to
%esolve this p%oblem+
1. -n file 2H1.>2 define as,
int a " = { 1, 1, !, 9, A, = ) #
int si/eTa = si/eof ( a ) #
and in file H1.> decla%e as,
e3te%n int a " #
e3te%n int si/eTa #

1. -n file 2H1.52 define,

Ddefine *UUT.-^ =
-n file H1.> decla%e as,
Dinclude (H1.5(
int a *UUT.-^ " #
and in file H1.> decla%e as,
Dinclude (H1.5(
e3te%n int a *UUT.-^ " #
!. -n file 2H1.>2 define as,
int a " = { 1, 1, !, 9, A, =, 01 ) #

and in file 2H1.>2 decla%e as,
www.technicalsymposium.com 28

e3te%n int a " #
5e%e the element 01 is used as a sentinel value, so the code can
unde%stand the end without any e3plicit si/e.
0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
=@.

5ow to delete a line f%om te3t displayed on the sc%een?

*ns+ .ometimes, specially when we a%e c%eating a te3t edito% li7e p%og%am we may wish to allow use% to delete a line.
We can do so by using two functions namely cl%eol( ) and delline( ). ,he cl%eol( ) function deletes the line f%om the
cu%%ent cu%so% position to the end of line. ,he delline() function deletes the enti%e line at the cu%%ent cu%so% position and
moves up the following line. Hollowing p%og%am shows how to use these functions.

Dinclude Econio.hF

main( )
{
int i #

fo% ( i = 1 # i E= 10 # i$$ )
p%intf ( (,his is <ine 'dJn(, i ) #

getch( ) #
goto3y ( 1, ? ) #
cl%eol( ) #

getch( ) #
goto3y ( 1, 11 ) #
delline( ) #

getch( ) #
)
0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
=Q.
5ow do - p%og%ammatically inse%t lines in the te3t window?

*ns+ We can inse%t a blan7 line in the te3t window using the insline( ) function. ,his function inse%ts line at cu%%ent
cu%so% position. While doing so, it shifts down the lines that a%e below the newly inse%ted line.

Dinclude Econio.hF
void main( )
{
p%intf ( (,he little snail was slowly moving up. .he wantedJ%Jn( ) #
p%intf ( (to %each the top of the t%ee. -t was chillyJ%Jn( ) #
p%intf ( (winte% season. Post of the animals we%e %esting inJ%Jn( ) #
p%intf ( (thei% nests as the%e was a heavy snow fall.J%Jn( ) #
p%intf ( (J%JnS%ess any 7ey to continue+( ) #

goto3y ( 10, 1 ) #
getch( ) #
insline( ) #
getch( ) #
)
0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
www.technicalsymposium.com 29
?0.
What will be the output of the following p%og%am?

main( )
{
unsigned int num #
int i #

p%intf ( (JnInte% any numbe%( ) #
scanf ( ('u(, Lnum ) #

fo% ( i = 0 # i E 1= # i$$ )
p%intf ( ('d(, ( num EE i L 1 EE 1A ) ? 1 + 0 ) #
)

*ns+ ,he output of this p%og%am is the bina%y eMuivalent of the given numbe%. We have used bitwise ope%ato%s to get
the bina%y numbe%.
0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
?1.
W%aphics
Guilding Pouse >u%so%s...
-n te3t mode the mouse cu%so% appea%s as a bloc7, whe%eas in g%aphics mode it appea%s as an a%%ow. -f we wish we can
change the g%aphics cu%so% to any othe% shape the way Windows does. ,he mouse cu%so% in g%aphics mode occupies a
1= by 1= pi3el bo3. Gy highlighting o% dehighlighting some of the pi3els in this bo3 we can get the desi%ed shape. Ho%
e3ample, the following bit0patte%n can be used to gene%ate the cu%so% which loo7s li7e an hou%0glass.
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
Pouse pointe% bitmap .c%een Pas7 the one2s in the mouse pointe% bitmap indicate that the pi3el would be d%awn
whe%eas the /e%os indicate that the pi3el would stand e%ased. -t is impo%tant to note that the mouse pointe% bit patte%n is
!1 bytes long. 5oweve%, while actually w%iting a p%og%am to change the pointe% shape we need a =9 byte bit0map. ,his
p%ovision is made to ensu%e that when the cu%so% %eaches a position on the sc%een whe%e something is al%eady w%itten
o% d%awn only that po%tion should get ove%w%itten which is to be occupied by the mouse cu%so%. ;f the =9 bytes the fi%st
!1 bytes contain a bit mas7 which is fi%st *6:ed with the sc%een image, and then the second !1 bytes bit mas7 is
\;Ued with the sc%een image.
,he following p%og%am changes the mouse cu%so% in g%aphics mode to %esemble an hou% glass.
D include (g%aphics.h(
D include (dos.h(
www.technicalsymposium.com 30
union UIW. i, o #
st%uct .UIW. s #
int cu%so%!1" =
{
K4 5ou%0glass sc%een mas7 4K
030000, 030000, 030000, 030000,
03@001, 03c00!, 03f00f, 03fc!f,
03fc!f, 03f00f, 03c00!, 03@001,
030000, 030000, 030000, 030000,
K4 ,he mouse 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 ( initmouse( ) == 01 )
{
closeg%aph( ) #
p%intf ( (Jn Pouse not installed8( ) #
e3it( ) #
)
goto3y ( 10, 1 ) # p%intf ( (S%ess any 7ey to e3it...( ) #
changecu%so% ( cu%so% ) # showmousept%( ) #
getch( ) #
)
initmouse( )
{
i.3.a3 = 0 # int@= ( 03!!, Li, Lo ) #
%etu%n ( o.3.a3 == 0 ? 01 + 0 ) #
)
showmousept%( )
{
i.3.a3 = 1 # int@= ( 03!!, Li, Lo ) #
)
changecu%so% ( int 4shape )
{
i.3.a3 = Q # K4 se%vice numbe% 4K
i.3.b3 = 0 # K4 actual cu%so% position f%om left 4K
i.3.c3 = 0 # K4 actual cu%so% position f%om top 4K
i.3.d3 = ( unsigned ) shape # K4 offset add%ess of pointe% image4K
seg%ead ( Ls ) #
s.es = s.ds # K4 segment add%ess of pointe% 4K
int@=3 ( 03!!, Li, Li, Ls ) #
)
0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
www.technicalsymposium.com 31
?1.
,owe%s ;f 5anoi
.uppose the%e a%e th%ee pegs labeled *, G and >. Hou% dis7s a%e placed on peg *. ,he bottom0most dis7 is la%gest, and
dis7s go on dec%easing in si/e with the topmost dis7 being smallest. ,he obOective of the game is to move the dis7s
f%om peg * to peg >, using peg G as an au3ilia%y peg. ,he %ules of the game a%e as follows+
;nly one dis7 may be moved at a time, and it must be the top dis7 on one of the pegs. * la%ge% dis7 should neve% be
placed on the top of a smalle% dis7. .uppose we a%e to w%ite a p%og%am to p%int out the seMuence in which the dis7s
should be moved such that all dis7s on peg * a%e finally t%ansfe%%ed to peg >. 5e%e it is...
main( )
{
int n = 9 #
move ( n, 2*2, 2G2, 2>2 ) #
)
move ( n, sp, ap, ep )
int n #
cha% sp, ap, ep #
{
if ( n == 1 )
p%intf ( (Jn Pove f%om 'c to 'c (, sp, ep ) #
else
{
move ( n 0 1, sp, ep, ap ) #
move ( 1, sp, 2 2, ep ) #
move ( n 0 1, ap, sp, ep ) #
)
)
*nd he%e is 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 >
,his p%oblem is the famous ,owe%s of 5anoi p%oblem, whe%ein th%ee pegs a%e to be employed fo% t%ansfe%%ing the dis7s
with the given c%ite%ia. 5e%e2s how we go about it. We have th%ee pegs+ the sta%ting peg, sp, the au3ilia%y peg ap, and the
ending peg, ep, whe%e the dis7s must finally be. Hi%st, using the ending peg as an au3ilia%y o% suppo%ting peg,
we t%ansfe% all but the last dis7 to ap. 6e3t the last dis7 is moved f%om sp to ep. 6ow, using sp as the suppo%ting peg, all
the dis7s a%e moved f%om ap to ep. X*Y, G and > denote the th%ee pegs. ,he %ecu%sive function move( ) is called with
diffe%ent combinations of these pegs as sta%ting, au3ilia%y and ending pegs.
0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
www.technicalsymposium.com 32
?!.
What would be the output of following p%og%am?
st%uct synta3
{
int i #
float g #
cha% c #
)
main( )
{
p%intf ( (- won2t give you any e%%o%( ) #
)
*ns+ ,he above p%og%am would get compiled successfully and on e3ecution it would p%int the message given in
p%intf(). What st%i7es in the above code snippet is the st%uctu%e synta3 which is decla%ed but not te%minated with the
statement te%minato%, the semicolon. ,he compile% would not give any e%%o% message fo% it, as it assumes that main( )
function have a %etu%n type of st%uct synta3 and hence would successfully compile and e3ecute the p%og%am.
0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
?9.
5ow to get the memo%y si/e ?

*ns+ >onside% the following p%og%am
Dinclude Estdio.hF
Dinclude Ebios.hF
main( )
{
int memsi/e#
memsi/e = biosmemo%y( ) #
p%intf ( (U*P si/e = 'dNJn(,memsi/e ) #
%etu%n 0 #
)

,he function biosmemo%y uses G-;. inte%%upt 0311 to %etu%n the si/e of memo%y.
0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
?A.
Hloat Ho%mat
5ow does > compile% sto%es float values ?

*ns+ -n >, the float values a%e sto%ed in a mantissa and e3ponent fo%m. While w%iting a numbe% we specify the e3ponent
pa%t in the fo%m of base 10. Gut, in case of > compile%, the e3ponent fo% floats is sto%ed in the fo%m of base 1.
;bviously, because, compute% sto%es the numbe%s in bina%y fo%m. ,he > compile% follows an -III standa%d to sto%e a
float. ,he -III fo%mat e3p%esses a floating0point numbe% in a bina%y fo%m 7nown as _no%mali/ed2 fo%m. 6o%mali/ation
involves adOusting the e3ponent so that the (bina%y point( (the bina%y analog of the decimal point) in the mantissa
always lies to the %ight of most significant non/e%o digit. -n bina%y %ep%esentation, this means that the most significant
digit of the mantissa is always a 1. ,his p%ope%ty of the no%mali/ed %ep%esentation is e3ploited by the -III fo%mat
when sto%ing the mantissa. <et us conside% an e3ample of gene%ating the no%mali/ed fo%m of a floating point numbe%.
.uppose we want to %ep%esent the decimal numbe% A.!?A. -ts bina%y eMuivalent can be obtained as shown below+
1 R A
.!?A 3 1 = 0.?A0 0
R000000
.?A0 3 1 = 1.A00 1
www.technicalsymposium.com 33
1 R 1 1
.A00 3 1 = 1.000 1
R000000
1 R 1 0
R000000
R 0 1
W%iting %emainde%s in %eve%se w%iting whole pa%ts in the same o%de% we get 101 o%de% in which they a%e obtained
we get 011 thus the bina%y eMuivalent of A.!?A would be 101.011. ,he no%mali/ed fo%m of this bina%y numbe% is
obtained by adOusting the e3ponent until the decimal point is to the %ight of most significant 1. -n this case the %esult is
1.01011 3 11. ,he -III fo%mat fo% floating point sto%age uses a sign bit, a mantissa and an e3ponent fo% %ep%esenting
the powe% of 1. ,he sign bit denotes the sign of the numbe%+ a 0 %ep%esents a positive value and a 1 denotes a negative
value. ,he mantissa is %ep%esented in bina%y. >onve%ting the floating0point numbe% to its no%mali/ed fo%m %esults in a
mantissa whose most significant digit is always 1. ,he -III fo%mat ta7es advantage of this by not sto%ing this bit at all.
,he e3ponent is an intege% sto%ed in unsigned bina%y fo%mat afte% adding a positive intege% bias. ,his ensu%es that the
sto%ed e3ponent is always positive. ,he value of the bias is 11? fo% floats and 101! fo% doubles. ,hus, 1.01011 3 11 is
%ep%esented as shown below+
000 000000000000000 0000000000000000000000000000000000000000000000
R 0 R 100 0000 1 R 010 1100 0000 0000 0000 0000 R
000 0000000000000000 000000000000000000000000000000000000000000000
sign bit e3ponent0 mantissa sto%ed in no%mali/ed fo%m obtained afte% adding a bias
11? to e3ponent 1

:ata .t%uctu%es

Which is the best so%ting method?

*ns+ ,he%e is no so%ting method that is unive%sally supe%io% to all othe%s. ,he p%og%amme% must ca%efully e3amine the
p%oblem and the desi%ed %esults befo%e deciding the pa%ticula% so%ting method. .ome of the so%ting methods a%e given
below+
Gubble so%t + When a file containing %eco%ds is to be so%ted then Gubble so%t is the best so%ting method when so%ting by
add%ess is used.
Gso%t + -t can be %ecommended if the input to the file is 7nown to be nea%ly so%ted.
Peanso%t + -t can be %ecommended only fo% input 7nown to be ve%y nea%ly so%ted.
`uic7 .o%t + -n the vi%tual memo%y envi%onment, whe%e pages of data a%e constantly being swapped bac7 and fo%th
between e3te%nal and inte%nal sto%age. -n p%actical situations, Muic7 so%t is often the fastest available because of its low
ove%head and its ave%age behavio%.
5eap so%t + Wene%ally used fo% so%ting of complete bina%y t%ee. .imple inse%tion so%t and st%aight selection so%t + Goth
a%e mo%e efficient than bubble so%t. .election so%t is %ecommended fo% small files when %eco%ds a%e la%ge and fo% %eve%se
situation inse%tion so%t is %ecommended. ,he heap so%t and Muic7 so%t a%e both mo%e efficient than inse%tion o% selection
fo% la%ge numbe% of data.
.hell so%t + -t is %ecommended fo% mode%ately si/ed files of seve%al hund%ed elements.
Uadi3 so%t + -t is %easonably efficient if the numbe% of digits in the 7eys is not too la%ge.
0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
?=.
>alculating Wasted Gytes ;n :is7
When a file gets sto%ed on the dis7, at a time :;. allocates one cluste% fo% it. * cluste% is nothing but a g%oup of
secto%s. 5oweve%, since all file si/es cannot be e3pected to be a multiple of A11 bytes, when a file gets sto%ed often pa%t
of the cluste% %emains unoccupied. ,his space goes waste unless the file si/e g%ows to occupy these wasted bytes. ,he
www.technicalsymposium.com 34
following p%og%am finds out how much space is wasted fo% all files in all the di%ecto%ies of the cu%%ent d%ive.
Dinclude Edi%.hF
Dinclude Edos.hF
Dinclude Estdio.hF
Dinclude Est%ing.hF
Dinclude Estdlib.hF
unsigned bytesTpe%Tcluste% #
unsigned long wastedTbytes #
unsigned long numTfiles = 0 #
main( )
{
int pt% = 0, flag = 0, fi%st = 0 #
st%uct ffbl7 fA0" #
st%uct df%ee f%ee #
K4 get cluste% info%mation and calculate bytes pe% cluste% 4K
getdf%ee ( 0, Lf%ee ) #
bytesTpe%Tcluste% = f%ee.dfTbsec 4 f%ee.dfTsclus #
chdi% ( (JJ( ) #
K4 chec7 out files in %oot di%ecto%y fi%st 4K
calTwaste( ) #
K4 loop until all di%ecto%ies scanned 4K
while ( pt% 8= 01 )
{
K4 should - do a findfi%st o% a findne3t? 4K
if ( fi%st == 0 )
flag = findfi%st ( (4.4(, Lfpt%", H*T:-UI> ) #
else
flag = findne3t ( Lfpt%" ) #
while ( flag == 0 )
{
K4 ma7e su%e its a di%ecto%y and s7ip ove% . L .. ent%ies 4K
if ( fpt%".ffTatt%ib == H*T:-UI> LL fpt%".ffTname0" 8= 2.2 )
{
flag = chdi% ( fpt%".ffTname ) # K4 t%y changing di%ecto%ies 4K
if ( flag == 0 ) K4 did change di% wo%7? 4K
{
calTwaste( ) #
fi%st = 0 # K4 set fo% findfi%st on ne3t pass 4K
b%ea7 #
)
)
flag = findne3t ( Lfpt%" ) # K4 sea%ch fo% mo%e di%s 4K
)
if ( flag 8= 0 RR pt% == 9Q ) K4 didn2t find any mo%e di%s 4K
{
pt%00 #
chdi% ( (..( ) # K4 go bac7 one level 4K
fi%st = 1 # K4 set to findne3t on ne3t pass 4K
)
else
pt%$$ #
)
p%intf ( (,he%e a%e 'lu bytes wasted in 'lu files.Jn(, wastedTbytes,
numTfiles ) #
)
www.technicalsymposium.com 35
calTwaste( )
{
int flag = 0 #
long fullTcluste% #
st%uct ffbl7 ff #
K4 loo7 fo% all file types 4K
flag = findfi%st ( (4.4(, Lff, H*TU:;6<V R H*T5-::I6 R H*T.V.,IP R H*T*U>5
) #
while ( flag == 0 )
{
numTfiles$$ #
fullTcluste% = ff.ffTfsi/e K bytesTpe%Tcluste% 4 bytesTpe%Tcluste% #
wastedTbytes $= bytesTpe%Tcluste% 0 ( ff.ffTfsi/e 0 fullTcluste% ) #
flag = findne3t ( Lff ) #
)
)
:ata .t%uctu%es
Solish 6otation
,he method of w%iting all ope%ato%s eithe% befo%e thei% ope%ation, o% afte% them, is called Solish notation, in hono% of its
discove%e%, the Solish mathematician ]an <u7asiewic/. When the ope%ato%s a%e w%itten befo%e thei% ope%ands, it is called
the p%efi3 fo%m. When the ope%ato%s come afte% thei% ope%ands. -t is called the postfi3 fo%m, o%, sometimes %eve%se
Solish fo%m o% suffi3 fo%m. -n this conte3t, it is customa%y to use the coined ph%ase infi3 fo%m to denote the usual
custom of w%iting bina%y ope%ato%s between thei% ope%ands. Ho% e3ample, the e3p%ession * $ G becomes $*G in p%efi3
fo%m and *G$ in postfi3 fo%m. -n the e3p%ession * $ G 3 >, the multiplication is done fi%st, so we conve%t it fi%st,
obtaining fi%st * $ ( G>3 ) and then *G>3$ in postfi3 fo%m. ,he p%efi3 fo%m of this e3p%ession is $* 3 G>. ,he p%efi3
and postfi3 fo%ms a%e not %elated by ta7ing mi%%o% images o% othe% such simple t%ansfo%mation. *lso all pa%entheses
have been omitted in the Solish fo%ms.

0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
??.
,he <ongOmp *nd .etOmp
,he > p%og%amming language does not let you nest functions. Vou cannot w%ite a function definition inside anothe%
function definition, as in+
int fun1( )
{
int fun1() K4 such nesting of functions is not allowed 4K
{
.....
)
)
Gecause of this %est%iction it is not possible to hide function names inside a hie%a%chy. *s a %esult all the functions that
you decla%e within a p%og%am a%e visible to each othe%. ,his of cou%se is not a maOo% d%awbac7 since one can limit
visibility by g%ouping functions within sepa%ate > sou%ce files that belong to diffe%ent logical units of the p%og%am. >
does, howeve%, suffe% in anothe% way because of this design decision. -t p%ovides no easy way to t%ansfe% cont%ol out of
a function e3cept by %etu%ning to the e3p%ession that called the function. Ho% the vast maOo%ity of function calls, that is a
desi%able limitation. Vou want the discipline of nested function calls and %etu%ns to help you unde%stand flow of cont%ol
th%ough a p%og%am. 6eve%theless, on some occasions that discipline is too %est%ictive. ,he p%og%am is sometimes easie%
to w%ite, and to unde%stand, if you can Oump out of one o% mo%e function invocations at a single st%o7e. Vou want to
bypass the no%mal function %etu%ns and t%ansfe% cont%ol to somewhe%e in an ea%lie% function invocation.
www.technicalsymposium.com 36
Ho% e3ample, you may want to %etu%n to e3ecute some code fo% e%%o% %ecove%y no matte% whe%e an e%%o% is detected in
you% application. ,he setOmp and the longOmp functions p%ovide the tools to accomplish this. ,he setOmp function saves
the (state( o% the (conte3t( of the p%ocess and the longOmp uses the saved conte3t to %eve%t to a p%evious point in the
p%og%am. What is the conte3t of the p%ocess? -n gene%al, the conte3t of a p%ocess %efe%s to info%mation that enables you
to %econst%uct e3actly the way the p%ocess is at a pa%ticula% point in its flow of e3ecution. -n > p%og%am the %elevant
info%mation includes Muantities such as values of .S, .., H<*W., >., -S, GS, :-, I., .- and :. %egiste%s.
,o save this info%mation ,u%bo > uses the following st%uctu%e, which is defined, in the heade% file 2setOmp.h2.
typedef st%uct
{
unsigned OTsp #
unsigned OTss #
unsigned OTflag #
unsigned OTcs #
unsigned OTip #
unsigned OTbp #
unsigned OTdi #
unsigned OTes #
unsigned OTsi #
unsigned OTds #
) OmpTbuf1" #
,his is a system0dependent data type because diffe%ent systems might %eMui%e diffe%ent amounts of info%mation to
captu%e the conte3t of a p%ocess. -n ,u%bo >, OmpTbuf is simply an a%%ay of ten 10byte intege%s. ,o unde%stand the
mechanics of setOmp and longOmp, loo7 at the following code
f%agment.
Dinclude (setOmp.h(
OmpTbuf buf #
main( )
{
if ( setOmp ( buf ) == 0 )
p%ocess( ) #
else
handleTe%%o%( ) # K4 e3ecuted when longOmp is called 4K
)
p%ocess( )
{
int flag = 0 #
K4 some p%ocessing is done he%e 4K
K4 if an e%%o% occu%s du%ing p%ocessing flag is set up 4K
if ( flag )
longOmp ( buf, 1 ) #
)
Bpon ent%y to setOmp the stac7 contains the add%ess of the buffe% buf and the add%ess of the if statement in the main
function, to which setOmp will %etu%n. ,he setOmp function copies this %etu%n add%ess as well as the cu%%ent values of
%egiste%s, .S, .., H<*W., GS, :-, I., .- and :., into the buffe% buf. ,hen setOmp %etu%ns with a /e%o. -n this case, the
if statement is satisfied and the p%ocess( ) function is called. -f something goes w%ong in p%ocess( ) (indicated by the
flag va%iable), we call longOmp with two a%guments+ the fi%st is the buffe% that contains the conte3t to which we will
%etu%n. When the stac7 %eve%ts bac7 to this saved state, and the %etu%n statement in longOmp is e3ecuted, it will be as if
we we%e %etu%ning f%om the call to setOmp, which o%iginally saved the buffe% buf. ,he second a%gument to longOmp
specifies the %etu%n value to be used du%ing this %etu%n. -t should be othe% than /e%o so that in the if statement we can
tell whethe% the %etu%n is induced by a longOmp.
,he setOmpKlongOmp combination enables you to Oump unconditionally f%om one > function to anothe% without using
the conventional %etu%n statements. Issentially, setOmp ma%7s the destination of the Oump and longOmp is a non0local
goto that e3ecutes the Oump.
www.technicalsymposium.com 37
:ata .t%uctu%es
>ompa%ison ,%ees...
,he compa%ison t%ees also called decision t%ee o% sea%ch t%ee of an algo%ithm, is obtained by t%acing th%ough the actions
of the algo%ithm, %ep%esenting each compa%ison of 7eys by a ve%te3 of the t%ee (which we d%aw as a ci%cle). -nside the
ci%cle we put the inde3 of the 7ey against which we a%e compa%ing the ta%get 7ey. G%anches (lines) d%awn down f%om
the ci%cle %ep%esent the possible outcomes of the compa%ison and a%e labeled acco%dingly. When the algo%ithm
te%minates, we put eithe% H (fo% failu%e) o% the location whe%e the ta%get is found at the end of the app%op%iate b%anch,
which we call a leaf, and d%aw as a sMua%e. <eaves a%e also sometimes called end ve%tices o% e3te%nal ve%tices of the
t%ee. ,he %emaining ve%tices a%e called the inte%nal ve%tices of the t%ee. ,he compa%ison t%ee fo% seMuential sea%ch is
especially simple.
0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
?@.
.uppose we have a floating0point numbe% with highe% p%ecision say 11.11=9@?=@? and we wish it to be p%inted with
only p%ecision up to two decimal places. 5ow can - do this?
*ns. ,his can achieved th%ough the use of supp%ession cha% 242 in the fo%mat st%ing of p%intf( ) which is shown in the
following p%og%am.
main( )
{
int p = 1 #
float n = 11.11=9@?=@? #
p%intf ( ('.4f(,p, n ) #
)
0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
?Q.
.pawning
*ll p%og%ams that we e3ecute f%om :;. p%ompt can be thought of as child%en of >;PP*6:.>;P. ,hus, the
p%og%am that we e3ecute is a child p%ocess, whe%eas >;PP*6:.>;P %unning in memo%y is its pa%ent. ,he p%ocess
of a pa%ent p%ocess giving bi%th to a child p%ocess is 7nown as 2spawning2. -f the spawned p%og%am so desi%es, it may in
tu%n spawn child%en of its own, which then e3ecute and %etu%n cont%ol to thei% pa%ent. Who is the pa%ent of
>;PP*6:.>;P? >;PP*6:.>;P itself. We can t%ace the ancesto%s of ou% p%og%am using the field Sa%ent
S%ocess -: (S-:) p%esent at offset 031= in the S%og%am .egment S%efi3 (S.S). ,o t%ace this ancest%y ou% p%og%am
should fi%st locate its S.S, e3t%act the pa%ent p%ocess -: f%om it and then use this to find S.S of the pa%ent. ,his p%ocess
can be %epeated till we %each >;PP*6:.>;P (p%ocess -: of >;PP*6:.>;P is its own S.S), the fathe% of all
p%ocesses. 5e%e is a p%og%am which achieves this...
K4 .S*W6.> 4K
Dinclude (dos.h(
unsigned oldpsp, newpsp, fa% 4ebTseg, i #
cha% fa% 4ebTpt% #
main( )
{
oldpsp = Tpsp #
while ( 1 )
{
p%intf ( (Jn( ) #
p%intname ( oldpsp ) #
p%intf ( ( spawned by ( ) #
www.technicalsymposium.com 38
newpsp = 4 ( ( unsigned fa% 4 ) PNTHS ( oldpsp, 031= ) ) #
if ( 4 ( ( unsigned 4 ) PNTHS ( newpsp, 031= ) ) == newpsp )
b%ea7 #
else
oldpsp = newpsp #
p%intname ( newpsp ) #
)
p%intf ( ('010s ('09\)(, (>;PP*6:.>;P(, newpsp ) #
)
p%intname ( unsigned lpsp )
{
cha% d%iveA", di%=@", name1!", e3tA" #
ebTseg = ( unsigned fa% 4 ) PNTHS ( lpsp, 031> ) #
ebTpt% = PNTHS ( 4ebTseg, 0 ) #
i = 0 #
while ( 1 )
{
if ( ebTpt%i" == 0 )
{
if ( ebTpt%i $ 1" == 0 LL ebTpt%i $ 1" == 1 )
{
i $= 9 #
b%ea7 #
)
)
i$$ #
)
fnsplit ( ebTpt% $ i, d%ive, di%, name, e3t ) #
st%cat ( name, e3t ) #
p%intf ( ('010s ('09\)(, name, oldpsp ) #
)
;n %unning the p%og%am f%om within ,> the output obtained is shown below. .SW*6.I\I (A@*Q) spawned by
,>.I\I (0=?1) ,>.I\I (0=?1) spawned by >;PP*6:.>;P (0AG@). ,he p%og%am simply copies its own p%ocess
-: in the va%iable oldpsp and then uses it to e3t%act its own filename f%om its envi%onment bloc7. ,his is done by the
function p%intname( ). ,he value in oldpsp is then used to %et%ieve the pa%ent2s S-: in newpsp. H%om the%e the p%og%am
loops %epo%ting the values of oldpsp, newpsp and the co%%esponding file names until the p%og%am %eaches
>;PP*6:.>;P.
,he p%intname( ) function fi%st locates the envi%onment bloc7 of the p%og%am and then e3t%acts the file name f%om the
envi%onment bloc7. ,he fnsplit( ) function has been used to eliminate the path p%esent p%io% to the file name. :o not %un
the p%og%am f%om command line since it would give you only one level of ancest%y.
:ata .t%uctu%es
>hoosing the data st%uctu%es to be used fo% info%mation %et%ieval. Ho% p%oblems of info%mation %et%ieval, conside% the
si/e, numbe%, and location of the %eco%ds along with the type and st%uctu%e of the 7eys while choosing the data
st%uctu%es to be used. Ho% small %eco%ds, high0speed inte%nal memo%y will be used, and bina%y sea%ch t%ees will
li7ely p%ove adeMuate. Ho% info%mation %et%ieval f%om dis7 files, methods employing multiway b%anching, such as t%ees,
www.technicalsymposium.com 39
G0t%ees , and hash tables, will usually be supe%io%. ,%ies a%e pa%ticula%ly suited to applications whe%e the 7eys a%e
st%uctu%ed as a seMuence of symbols and whe%e the set of 7eys is %elatively dense in the set of all possible 7eys. Ho%
othe% applications, methods that t%eat the 7ey as a single unit will often p%ove supe%io%. G0t%ees, togethe% with va%ious
gene%ali/ation and e3tensions, can be usefully applied to many p%oblems conce%ned with e3te%nal info%mation %et%ieval.
0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
@0.
Ca%iably :imensioned *%%ays
While dealing with .cientific o% Inginee%ing p%oblems one is often %eMui%ed to ma7e use of multi0dimensioned a%%ay.
5oweve%, when it comes to passing multidimensional a%%ays to a function > is found wanting. ,his is because the >
compile% wants to 7now the si/e of all but the fi%st dimension of any a%%ay passed to a function. Ho% instance, we can
define a function compute ( int n, float 3" ), but not compute ( int n, 3"").
,hus, > can deal with va%iably dimensioned 10: a%%ays, but when an a%%ay has mo%e than one dimension, the >
compile% has to 7now the si/e of the last dimensions e3p%essed as a constant. ,his p%oblem has long been
%ecogni/ed, and some of the solutions that a%e often used a%e+
:ecla%e the a%%ays in the functions to be big enough to tac7le all possible situations. ,his can lead to a wastage of lot of
p%ecious memo%y in most cases. *nothe% solution is to const%uct multiple0dimension a%%ay as an a%%ay of pointe%s. Ho%
e3ample, a mat%i3 (10: a%%ay) of floats can be decla%ed as a 10: a%%ay of float pointe%s, with each element pointing to
an a%%ay of floats. ,he p%oblem with this method is that the calling function has to define all a%%ays in this fashion. ,his
means that any othe% computations done on the a%%ays must ta7e this special st%uctu%e into account.
*nothe% easy solution, though seldom used, e3ists. ,his is based on the following method+
Sass the a%%ay to the function as though it is a pointe% to an a%%ay of floats (o% the app%op%iate data type), no matte% how
many dimensions the a%%ay actually has, along with the dimensions of the a%%ay. Uefe%ence individual a%%ay elements as
offsets f%om this pointe%.
W%ite you% algo%ithm so that a%%ay elements a%e accessed in sto%age o%de%. ,he following p%og%am fo% multiplying two
mat%ices illust%ates this
p%ocedu%e.
D define P !
D define 6 1
D define S 9
float aP"6", b6"S", cP"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$$ )
ai"O" = i $ O #
fo% ( i = 0 # i E 6 # i$$ )
fo% ( O = 0 # O E S # O$$ )
bi"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(, ci"O" ) #
www.technicalsymposium.com 40
)
)
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 set all elements 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 > sto%es a%%ay elements in a %ow0maOo% o%de%. 5ence to ensu%e that the elements a%e accessed in the
sto%age o%de% the above p%og%am uses a va%iation of the no%mal mat%i30multiplication p%ocedu%e. ,he pseudo code
fo% this is given below+
fo% i = 1 to m
fo% O = 1 to p
ci"O" = 0
end
fo% 7 = 1 to n
fo% O = 1 to p
ci"O" = ci"O" $ ai"7" 4 b7"O"
end
end
end
0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
@1.
Why is it not possible to scan st%ings f%om 7eyboa%d in case of a%%ay of pointe%s to st%ing?
*ns+ When an a%%ay is decla%ed, dimension of a%%ay should be specified so that compile% can allocate memo%y fo% the
a%%ay. When a%%ay of pointe%s to st%ings is decla%ed its elements would contain ga%bage add%esses. ,hese add%esses
would be passed to scanf( ). .o st%ings can be %eceived but they would get sto%ed at un7own locations. ,his is unsafe.
0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
@1.
Git *%%ays
-f in a p%og%am a va%iable is to ta7e only two values 1 and 0, we %eally need only a single bit to sto%e it. .imila%ly, if a
va%iable is to ta7e values f%om 0 to !, then two bits a%e sufficient to sto%e these values. *nd if a va%iable is to ta7e
values f%om 0 th%ough ?, then th%ee bits will be enough, and so on. Why waste an enti%e intege% when one o% two o%
th%ee bits will do? Gecause the%e a%en2t any one bit o% two bit o% th%ee bit data types available in >. 5oweve%, when
www.technicalsymposium.com 41
the%e a%e seve%al va%iables whose ma3imum values a%e small enough to pac7 into a single memo%y location, we can use
_bit fields2 to sto%e seve%al values in a single intege%. Git fields a%e discussed in most standa%d > te3ts. ,hey a%e usually
used when we want to sto%e asso%ted info%mation which can be accommodated in 1, 1, ! bits etc.
Ho% e3ample, the following data about an employee can be easily sto%ed using bit fields.
male o% female
single, ma%%ied, divo%ced o% widowed
have one of the eight diffe%ent hobbies
can choose f%om any of the fifteen diffe%ent schemes p%oposed by the company to pu%sue hisKhe% hobby.
,his means we need one bit to sto%e gende%, two to sto%e ma%ital status, th%ee fo% hobby, and fou% fo% scheme (with one
value used fo% those who a%e not desi%ous of availing any of the schemes). We need ten bits altogethe%, which means we
can pac7 all this info%mation into a single intege%, since an intege% is 1= bits long.
*t times we may need to sto%e seve%al ,%ue o% Halse statuses. -n such cases instead of using bit fields using an a%%ay of
bits would be mo%e sensible. ;n this a%%ay we may be %eMui%ed to pe%fo%m the following ope%ations+
.et a bit (ma7e it 1).
>lea% a bit (ma7e it 0).
,est the status of a bit in the a%%ay.
Ueach the app%op%iate bit slot in the a%%ay.
Wene%ate a bit mas7 fo% setting and clea%ing a bit.
We can implement these ope%ations using mac%os given below+
Ddefine >5*U.-^I @
Ddefine P*.N ( y ) ( 1 EE y ' >5*U.-^I )
Ddefine G-,.<;, ( y ) ( y K >5*U.-^I )
Ddefine .I, ( 3, y ) ( 3G-,.<;,( y )" R= P*.N( y ) )
Ddefine ><I*U ( 3, y ) ( 3G-,.<;,( y )" L= aP*.N( y ) )
Ddefine ,I., ( 3, y ) ( 3G-,.<;,( y )" L P*.N( y ) )
Ddefine 6BP.<;,. ( n ) ( ( n $ >5*U.-^I 0 1) K >5*U.-^I )
Bsing these mac%os we can decla%e an a%%ay of A0 bits be saying,
cha% a%%6BP.<;,.(A0)" #
,o set the 10th bit we can say,
.I,(a%%, 10 ) #
*nd if we a%e to test the status of 90th bit we may say,
if ( ,I., ( a%%, 90 ) )
Bsing bit a%%ays often %esults into saving a lot of p%ecious memo%y. Ho% e3ample, the following p%og%am which
implements the .ieve of I%atosthenes fo% gene%ating p%ime numbe%s smalle% than 100 %eMui%es only 1! bytes. 5ad
we implemented the same logic using an a%%ay of intege%s we would have %eMui%ed an a%%ay of 100 intege%s, that is 100
bytes.
Dinclude Estdio.hF
Dinclude Est%ing.hF
Ddefine P*\ 100
main( )
{
cha% a%%6BP.<;,.( P*\ )" #
int i, O #
memset ( a%%, 0, 6BP.<;,.( P*\ ) ) #
www.technicalsymposium.com 42
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 ) #
)
)
)
0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
@!.
-nfo%mation 5iding in >

,hough > language doesn2t fully suppo%t encapsulation as >$$ does, the%e is a simple techniMue th%ough which we can
implement encapsulation in >. ,he techniMue that achieves this is modula% p%og%amming in >. Podula%
p%og%amming %eMui%es a little e3t%a wo%7 f%om the p%og%amme%, but pays fo% itself du%ing maintenance. ,o unde%stand
this techniMue let us ta7e the e3ample of the popula% stac7 data st%uctu%e. ,he%e a%e many methods of implementing a
stac7 (a%%ay, lin7ed list, etc.). -nfo%mation hiding teaches that use%s should be able to push and pop the stac72s elements
without 7nowing about the stac72s implementation. * benefit of this so%t of info%mation hiding is that use%s don2t have
to change thei% code even if the implementation details change.
>onside% the following scena%io+
,o be able to app%eciate the benefits of modula% p%og%amming and the%eby info%mation hiding, would fi%st show a
t%aditional implementation of the stac7 data st%uctu%e using pointe%s and a lin7ed list of st%uctu%es. ,he main( ) function
calls the push( ) and pop( ) functions.
Dinclude Ealloc.hF
typedef int element #
void initiali/eTstac7 ( st%uct node 44 ) #
void push ( st%uct node 44, element ) #
element pop ( st%uct node 4 ) #
int isempty ( st%uct node 4 ) #
st%uct node
{
element data #
st%uct node 4ne3t #
) #
void main( )
{
st%uct node 4top #
element num #
initiali/eTstac7 ( Ltop ) #
push ( Ltop, 10 ) #
push ( Ltop, 10 ) #
push ( Ltop, !0 ) #
if ( isempty ( top ) )
p%intf ( (Jn.tac7 is empty( ) #
else
{
num = pop ( top ) #
p%intf ( (Jn Sopped 'd(, num ) #
)
)
void initiali/eTstac7 ( st%uct node 44p )
www.technicalsymposium.com 43
{
4p = 6B<< #
)
void push ( st%uct node 44p, element n )
{
st%uct node 4% #
% = ( st%uct node 4) malloc ( si/eof ( st%uct node ) ) #
% 0F data = n #
if ( 4p == 6B<< )
% 0F ne3t = 6B<< #
else
% 0F ne3t = 4p #
4p = % #
)
element pop ( st%uct node 4p )
{
element n #
st%uct node 4% #
n = p 0F data #
% = p #
p = p 0F ne3t #
f%ee ( % ) #
%etu%n ( n ) #
)
int isempty ( st%uct node 4p )
{
if ( p == 6B<< )
%etu%n ( 01 ) #
else
%etu%n ( 0 ) #
)
6otice how the specific implementation of the data st%uctu%e is st%ewn th%oughout main( ). main( ) must see the
definition of the st%uctu%e node to use the push( ), pop( ), and othe% stac7 functions. ,hus the implementation is not
hidden, but is mi3ed with the abst%act ope%ations.

:ata .t%uctu%es
Uadi3 .o%t

,his so%ting techniMue is based on the values of the actual digits in the positional %ep%esentations of the numbe%s being
so%ted. Bsing the decimal base, fo% e3ample, whe%e the %adi3 is 10, the numbe%s can be pa%titioned into ten g%oups on
the so%te%. Ho% e3ample, to so%t a collection of numbe%s whe%e each numbe% is a fou%0digit numbe%, then, *ll the
numbe%s a%e fi%st so%ted acco%ding to the the digit at unit2s place.
-n the second pass, the numbe%s a%e so%ted acco%ding to the digit at tenth place. -n the thi%d pass, the numbe%s
a%e so%ted acco%ding to the digit at hund%edth place. -n the fo%th and last pass, the numbe%s a%e so%ted acco%ding to the
digit at thousandth place.
:u%ing each pass, each numbe% is ta7en in the o%de% in which it appea%s in pa%titions f%om unit2s place
onwa%ds. When these actions have been pe%fo%med fo% each digit, sta%ting with the least significant and ending with
most significant, the numbe%s a%e so%ted. ,his so%ting method is called the %adi3 so%t.
<et us ta7e anothe% e3ample. .uppose we have a list of names. ,o so%t these names using %adi3 so%t method we will
have to classify them into 1= g%oups ,he list is fi%st so%ted on the fi%st lette% of each name, i.e. the names a%e a%%anged in
1= classes, whe%e the fi%st class consists of those names that begin with alphabet 2*2, the second class consists of those
names that begin with alphabet 2G2 and so on. :u%ing the second pass each class is alphabeti/ed acco%ding to the second
lette% of the name, and so on.
0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
www.technicalsymposium.com 44
@9.
I3ception 5andling in >

>onside% the following p%og%am+

Dinclude Emath.hF
void main( )
{
float i #
i = pow ( 01, ! ) #
p%intf ( ('f(, i ) #
)

int mathe%% ( st%uct e3ception 4a )
{
if ( a 0F type == :;P*-6 )
{
if ( 8st%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 pass a negative value in pow( ) function a %un time e%%o% occu%s. -f we wish to get the p%ope% output even afte%
passing a negative value in the pow( ) function we must handle the %un time e%%o%. Ho% this, we can define a function
mathe%%( ) which is decla%ed in the 2math.h2 file. -n this function we can detect the %un0time e%%o% and w%ite ou% code to
co%%ect the e%%o%. ,he elements of the e3ception st%uctu%e %eceives the function name and a%guments of the function
causing the e3ception.

:ata .t%uctu%es
*C< ,%ees

Ho% ideal sea%ching in a bina%y sea%ch t%ee, the heights of the left and %ight sub0t%ees of any node should be eMual. Gut,
due to %andom inse%tions and deletions pe%fo%med on a bina%y sea%ch t%ee, it often tu%ns out to be fa% f%om ideal. * close
app%o3imation to an ideal bina%y sea%ch t%ee is achievable if it can be ensu%ed that the diffe%ence between the heights of
the left and the %ight sub t%ees of any node in the t%ee is at most one. * bina%y sea%ch t%ee in which the diffe%ence of
heights of the %ight and left sub0t%ees of any node is less than o% eMual to one is 7nown as an *C< t%ee. *C< t%ee is
also called as Galanced ,%ee. ,he name (*C< ,%ee( is de%ived f%om the names of its invento%s who a%e *delson0
Ceils7ii and <andi. * node in an *C< t%ee have a new field to sto%e the (balance facto%( of a node which denotes the
diffe%ence of height between the left and the %ight sub0t%ees of the t%ee %ooted at that node. *nd it can assume one of the
th%ee possible values {01,0,1).
0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
@A.
BniMue combinations fo% a given numbe%

5ow do - w%ite a p%og%am which can gene%ate all possible combinations of numbe%s f%om 1 to one less than the given
numbe% ?
main( )
{
long steps, fval, bstp, cnt1 #
www.technicalsymposium.com 45
int num, unit, bo31"1!", cnt1, cnt!, cnt9 #
p%intf ( (Inte% 6umbe% ( ) #
scanf ( ('d(, Lnum ) #
num = num E 1 ? 1 + num F 11 ? 11 + num #
fo% ( steps = 1, cnt1 = 1 # cnt1 E= num # steps 4= cnt1$$ ) #
fo% ( cnt1 = 1 # cnt1 E= steps # cnt1$$ )
{
fo% ( cnt1 = 1 # cnt1 E= num # cnt1$$ )
bo30"cnt1" = cnt1 #
fo% ( fval = steps, bstp = cnt1, cnt1 = 1 # cnt1 E= num # cnt1$$ )
{
if ( bstp == 0 )
{
cnt9=num #
while ( bo30"cnt9" == 0 )
cnt900 #
)
else
{
fval K= num 0 cnt1 $ 1 #
unit = ( bstp $ fval 0 1 ) K fval #
bstp '= fval #
fo% ( cnt9 = 0, cnt! = 1 # cnt! E= unit # cnt!$$ )
while ( bo30"$$cnt9" == 0 ) #
)
bo31"cnt1" = bo30"cnt9" #
bo30"cnt9" = 0 #
)
p%intf ( (Jn.eM.6o.'ld+(, cnt1 ) #
fo% ( cnt1 = 1 # cnt1 E= num # cnt1$$ )
p%intf ( ( 'd(, bo31"cnt1" ) #
)
)
,his p%og%am computes the total numbe% of steps. Gut instead of ente%ing into the loop of the fi%st and last combination
to be gene%ated it uses a loop of 1 to numbe% of combinations. Ho% e3ample, in case of input being A the numbe% of
possible combinations would be facto%ial A, i.e. 110. ,he p%og%am suffe%s f%om the limitation that it cannot gene%ate
combinations fo% input beyond 11 since a long int cannot handle the %esulting combinations.

:ata .t%uctu%es

5ashing...

5ashing o% hash add%essing is a sea%ching techniMue. Bsually, sea%ch of an element is ca%%ied out via a seMuence of
compa%isons. 5ashing diffe%s f%om this as it is independent of the numbe% of elements n in the collection of data. 5e%e,
the add%ess o% location of an element is obtained by computing some a%ithmetic function. 5ashing is usually used in file
management. ,he gene%al idea is of using the 7ey to dete%mine the add%ess of a %eco%d. Ho% this, a function fun( ) is
applied to each 7ey, called the hash function. .ome of the popula% hash functions a%e+ 2:ivision2 method, 2PidsMua%e2
method, and 2Holding2 method. ,wo %eco%ds cannot occupy the same position. .uch a situation is called a hash collision
o% a hash clash. ,he%e a%e two basic methods of dealing with a hash clash. ,he fi%st techniMue, called %ehashing,
involves using seconda%y hash function on the hash 7ey of the item. ,he %ehash function is applied successively until an
empty position is found whe%e the item can be inse%ted. -f the hash position of the item is found to be occupied du%ing a
sea%ch, the %ehash function is again used to locate the item. ,he second techniMue, called chaining, builds a lin7ed
list of all items whose 7eys hash to the same values. :u%ing sea%ch, this sho%t lin7ed list is t%ave%sed seMuentially fo% the
desi%ed 7ey. ,his techniMue involves adding an e3t%a lin7 field to each table position.
0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
www.technicalsymposium.com 46
@=.
,he following p%og%am demonst%ates how to get input f%om the use% in g%aphics mode, echoed in the cu%%ent colo%s and
font si/e and font style.

Ddefine ;6 1
Ddefine ;HH 0
Dinclude Eg%aphics.hF
main( )
{
cha% name.t%ing@0", age.t%ing@0" #
int age, gd = :I,I>,, gm #
initg%aph ( Lgd, Lgm, (c+JJtcJJbgi( ) #
setb7colo% ( G<BI ) #
setcolo% ( VI<<;W ) #
sette3tstyle ( W;,5->TH;6,, 5;U-^T:-U, 0 ) #
moveto ( 0, 0 ) #
outte3t ( (Inte% you% name+ ( ) #
getW%.t%ing ( name.t%ing ) #
moveto ( 0, gety( ) $ te3theight ( (*( ) ) #
outte3t ( (6ame+ ( ) #
outte3t ( name.t%ing ) #
moveto ( 0, gety( ) $ te3theight ( (*( ) ) #
outte3t ( (S%ess 7ey to e3it8 ( ) #
getch( ) #
closeg%aph( ) #
%esto%ec%tmode( ) #
)
getW%.t%ing ( cha% 4input.t%ing )
{
int st%ing-nde3 = 0, old>olo% #
cha% ch, out.t%ing1" #
K4 3Cal will sto%e the sc%een position fo% each cha% 4K
int 3Cal1AA" #
out.t%ing1" = 0 #
3Cal0" = get3( ) #
do
{
cu%so% ( ;6 ) #
ch = getch( ) #
cu%so% ( ;HH ) #
if ( ch == 0 ) K4 avoid dealing with all special 7eys 4K
getch( ) #
else
{
if ( ch == @ ) K4 bac7space 4K
{
old>olo% = getcolo%( ) #
00st%ing-nde3 #
if ( st%ing-nde3 E 0 )
st%ing-nde3 = 0 #
K4 move to ( old ho%/ position, cu%%ent ve%t position ) 4K
moveto ( 3Calst%ing-nde3", gety( ) ) #
setcolo% ( getb7colo%( ) ) #
out.t%ing0" = input.t%ingst%ing-nde3" #
outte3t ( out.t%ing ) #
www.technicalsymposium.com 47
moveto ( 3Cal st%ing-nde3", gety( ) ) #
setcolo% ( old>olo% ) #
)
else
{
input.t%ingst%ing-nde3" = ch #
out.t%ing0" = ch #
outte3t ( out.t%ing ) #
$$st%ing-nde3 #
3Calst%ing-nde3" = get3( ) #
)
)
) while ( ch 8= 1! LL ch 8= 10 ) #
input.t%ingst%ing-nde3" = 0 #
)
cu%so% ( int on )
{
int cu%\, old>olo% #
K4 we2ll use an unde%sco%e as a cu%so% 4K
cha% uGa%.t%1" = { 2T2, 0 ) #
if ( 8on )
{
old>olo% = getcolo%( ) #
setcolo% ( getb7colo%( ) ) #
)
K4 save ho%i/ontal position befo%e d%awing cu%so% 4K
cu%\ = get3( ) #
outte3t ( uGa%.t% ) #
moveto ( cu%\, gety( ) ) #
K4 if we changed the colo% to e%ase cu%so%, change it bac7 4K
if ( 8on )
setcolo% ( old>olo% ) #
)
,he function getW%.t%ing( ) echoes g%aphically the use% input and sto%es it in a buffe%, and the function cu%so%( ) handles
the cu%so% position.

.ystem Btility
What is ga%bage collection?

*ns+ .uppose some memo%y space becomes %eusable because a node is %eleased f%om a lin7ed list. 5ence, we want the
space to be available fo% futu%e use. ;ne way to b%ing this about is to immediately %einse%t the space into the f%ee0
sto%age list. 5oweve%, this method may be too time0consuming fo% the ope%ating system. ,he ope%ating system may
pe%iodically collect all the deleted space onto the f%ee0sto%age list. ,he techniMue that does this collection is called
Wa%bage >ollection. Wa%bage >ollection usually ta7es place in two steps+ Hi%st the Wa%bage >ollecto% %uns th%ough all
lists, tagging whose cells a%e cu%%ently in use, and then it %uns th%ough the memo%y, collecting all untagged space onto
the f%ee0sto%age list. ,he Wa%bage >ollection may ta7e place when the%e is only some minimum amount of space o% no
space at all left in the f%ee0sto%age list, o% when the >SB is idle and has time to do the collection. Wene%ally spea7ing,
the Wa%bage >ollection is invisible to the p%og%amme%.
0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
@?.
5ow do - get the time elapsed between two function calls ?

*ns+ ,he function difftime( ) finds the diffe%ence between two times. -t calculates the elapsed time in seconds and
%etu%ns the diffe%ence between two times as a double value.
www.technicalsymposium.com 48

Dinclude Etime.hF
Dinclude Estdio.hF
Dinclude Edos.hF

main( )
{
int a" = { 1, 0!9, A=, ?@, 111, !!, 0?, 11, 9A, 1Q, = ) #
int s #
timeTt t1, t1 # KK timeTt defines the value used fo% time function

s = si/eof ( a ) K 1 #
t1 = time ( 6B<< ) #
selTso%t ( a, s ) # KK so%t a%%ay by selection so%t
bubTso%t ( a, s ) # KK so%t a%%ay by bubble so%t method
t1 = time ( 6B<< ) #
p%intf ( (Jn,he diffe%ence between two function calls is 'f(, difftime (
t1, t1 ) ) #
)

-n the above p%og%am we have called difftime( ) function that %etu%ns the time elapsed f%om t1 to t1.
0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
@@.
Wene%al
main( )
{
cha% 4s #
s = fun ( 11@, 1 ) #
p%intf ( (Jn's(, s ) #
)
fun ( unsigned int num, int base )
{
static cha% buff!!" #
cha% 4pt% #
pt% = Lbuff si/eof ( buff ) 0 1 " #
4pt% = 2J02 #
do
{
400pt% = (011!9A=?@Qabcdef( num ' base " #
num K= base #
) while ( num 8= 0 ) #
%etu%n pt% #
)
,he above p%og%am would conve%t the numbe% 11@ to the base 1. Vou can conve%t a numbe% to a he3adecimal o% octal
fo%m by passing the numbe% and the base, to the function fun( ).
:ata .t%uctu%es

What is a p%io%ity Mueue?

*ns+ *s we 7now in a stac7, the latest element is deleted and in a Mueue the oldest element is deleted. -t may be
%eMui%ed to delete an element with the highest p%io%ity in the given set of values and not only the oldest o% the newest
one. * data st%uctu%e that suppo%ts efficient inse%tions of a new element and deletions of elements with the highest
p%io%ity is 7nown as p%io%ity Mueue. ,he%e a%e two types of p%io%ity Mueues+ an ascending p%io%ity Mueue is a
www.technicalsymposium.com 49
collection of items into which items can be inse%ted a%bit%a%ily and f%om which only the smallest item can be %emoved.
* descending o%de% p%io%ity Mueue is simila% but allows only the la%gest item to be deleted.
0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
@Q.
What is the diffe%ence between const cha% 4p, cha% const 4p, and cha%4 const p ?
2const cha% 4p2 and 2cha% const 4p2 a%e the same, i.e. p points to a constant cha%acte%. ;n the othe% hand, 2cha%4 const p2
means p is a constant pointe% pointing to a cha%acte% which means we cannot change the pointe% p but we can change
the cha%acte% which p is pointing to.
0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
Q0.
What2s the diffe%ence between a null pointe%, a 6B<< mac%o, the *.>-- 6B< cha%acte% and a null st%ing?

*ns+ * null pointe% is a pointe% which doesn2t point anywhe%e. * 6B<< mac%o is used to %ep%esent the null pointe% in
sou%ce code. -t has a value 0 associated with it. ,he *.>-- 6B< cha%acte% has all its bits as 0 but doesn2t have any
%elationship with the null pointe%. ,he null st%ing is Oust anothe% name fo% an empty st%ing ((.

.ystem Btility
.pa%se Pat%i3...

* spa%se mat%i3 is one whe%e most of its elements a%e /e%o. ,he%e is no p%ecise definition as to 7now whethe% a mat%i3
is spa%sed o% not, but it is a concept which we all can %ecogni/e intuitively. ,he natu%al method of %ep%esenting mat%ices
in memo%y as two0dimensional a%%ays may not be suitable fo% spa%se mat%ices. ,hat is one may save space by sto%ing
only those ent%ies which may be non/e%o. -f this is done, then the mat%i3 may be thought of as an o%de%ed list of non0
/e%o elements only. -nfo%mation about a non0/e%o element has th%ee pa%ts+
an intege% %ep%esenting its %ow,
an intege% %ep%esenting its column and
the data associated with this element.
,hat is, each element of a mat%i3 is uniMuely cha%acte%i/ed by its %ow and column position, say i, O. We might sto%e that
mat%i3 as a list of !0tuples of the fo%m (i, O, data), as shown below,
*lthough the non0/e%o elements may be sto%ed in the a%%ay in any o%de%, 7eeping them o%de%ed in some fashion may be
advantageous fo% fu%the% p%ocessing. 6ote that above a%%ay is a%%anged in inc%easing o%de% of the %ow numbe% of non0
/e%o elements. Po%eove%, fo% elements in the same %ow numbe%, the a%%ay is a%%anged in o%de% of inc%easing column
numbe%.
0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
Q1.
Sointe%s
What does the e%%o% (6ull Sointe% *ssignment( mean and what causes this e%%o%?
*ns+ ,he 6ull Sointe% *ssignment e%%o% is gene%ated only in small and medium memo%y models. ,his e%%o% occu%s in
p%og%ams which attempt to change the bottom of the data segment. -n Go%land2s > o% >$$ compile%s, Go%land places
fou% /e%o bytes at the bottom of the data segment, followed by the Go%land copy%ight notice (Go%land >$$ 0 >opy%ight
1QQ1 Go%land -ntl.(. -n the small and medium memo%y models, a null pointe% points to :.+0000. ,hus assigning a value
to the memo%y %efe%enced by this pointe% will ove%w%ite the fi%st /e%o byte in the data segment. *t p%og%am
te%mination, the fou% /e%os and the copy%ight banne% a%e chec7ed. -f eithe% has been modified, then the 6ull Sointe%
*ssignment e%%o% is gene%ated. 6ote that the pointe% may not t%uly be null, but may be a wild pointe% that %efe%ences
these 7ey a%eas in the data segment.
www.technicalsymposium.com 50
:ata .t%uctu%es
5ow to build an e3p%ession t%ees ?
*ns+ *n e3p%ession t%ee is a bina%y t%ee which is built f%om simple ope%ands and ope%ato%s of an (a%ithmetic o% logical )
e3p%ession by placing simple ope%ands as the leaves of a bina%y t%ee and the ope%ato%s as the inte%io% nodes. -f an
ope%ato% is bina%y , then it has two nonempty subt%ees, that a%e its left and %ight ope%ands (eithe% simple ope%ands o%
sub e3p%essions). -f an ope%ato% is una%y, then only one of its subt%ees is nonempty, the one on the left o% %ight
acco%ding as the ope%ato% is w%itten on the %ight o% left of its ope%and. We t%aditionally w%ite some una%y ope%ato%s to
the left of thei% ope%ands, such as (0( ( una%y negation) o% the standa%d functions li7e log( ), sin( ) etc. ;the%s a%e w%itten
on the %ight, such as the facto%ial function ()8. -f the ope%ato% is w%itten on the left, then in the e3p%ession t%ee we ta7e its
left subt%ee as empty. -f it appea%s on the %ight, then its %ight subt%ee will be empty. *n e3ample of an e3p%ession
t%ee is shown below fo% the e3p%ession ( 0a E b ) o% ( c $ d ) .
0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
Q1.
>an we get the %emainde% of a floating point division ?

*ns + Ves. *lthough the ' ope%ato% fails to wo%7 on float numbe%s we can still get the %emainde% of floating point
division by using a function fmod( ). ,he fmod( ) function divides the two float numbe%s passed to it as pa%amete%s and
%etu%ns the %emainde% as a floating0point value. Hollowing p%og%am shows fmod( ) function at wo%7.

Dinclude Emath.hF

main( )
{
p%intf ( ('f(, fmod ( A.1A, !.0 ) ) #
)

,he above code snippet would give the output as 1.1A0000.
0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
Q!.
5ow to e3t%act the intege% pa%t and a f%actional pa%t of a floating point numbe%?

*ns+ > function modf( ) can be used 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
0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
www.technicalsymposium.com 51
Q9.
5ow do - define a pointe% to a function which %etu%ns a cha% pointe%?
*ns+
cha% 4 ( 4p )( ) #
o%
typedef cha% 4 ( 4 pt%tofun )( ) #
pt%tofun p #
5e%e is a sample p%og%am which uses this definition.
main( )
{
typedef cha% 4 ( 4 pt%tofun ) ( ) #
cha% 4 fun( ) #
pt%tofun fpt% #
cha% 4cpt% #
fpt% = fun #
cpt% = (4fpt%) ( ) #
p%intf ( (JnUetu%ned st%ing is J('sJ((, cpt% ) #
)
cha% 4 fun( )
{
static cha% s " = (5ello8( #
p%intf ( (Jn's(, s ) #
%etu%n s #
)
0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
QA.
What2s w%ong with the following decla%ation+ cha%4 pt%1, pt%1 # get e%%o%s when - t%y to use pt%1 as a pointe%.
*ns+ cha% 4 applies only to pt%1 and not to pt%1. 5ence pt%1 is getting decla%ed as a cha% pointe%, whe%eas, pt%1 is being
decla%ed me%ely as a cha%. ,his can be %ectified in two ways +
cha% 4pt%1, 4pt%1 #
typedef cha%4 >5*US,U # >5*US,U pt%1, pt%1 #
0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
Q=.
5ow to use scanf( ) to %ead the date in the fo%m of dd0mm0yy?
*ns+ ,o %ead the date in the fo%m of dd0mm0yy one possible way is,
int dd, mm, yy #
cha% ch # K4 fo% cha% 202 4K
p%intf ( (JnInte% the date in the fo%m of dd0mm0yy + ( ) #
scanf( ('d'c'd'c'd(, Ldd, Lch, Lmm, Lch, Lyy ) #
*nothe% way is to use supp%ession cha%acte% 4 is as follows+
int dd, mm, yy #
scanf( ('d'4c'd'4c'd(, Ldd, Lmm, Lyy ) #
,he supp%ession cha%acte% 242 supp%esses the input %ead f%om the standa%d input buffe% fo% the assigned cont%ol cha%acte%.
0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
Q?.
Why the output of si/eof ( 2a2 ) is 1 and not 1 ?
*ns+ >ha%acte% constants in > a%e of type int, hence si/eof ( 2a2 ) is eMuivalent to si/eof ( int ), i.e. 1. 5ence the output
comes out to be 1 bytes.
0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
www.technicalsymposium.com 52
Q@.
>an we use scanf( ) function to scan a multiple wo%ds st%ing th%ough 7eyboa%d?

*ns+ Ves. *lthough we usually use scanf( ) function to %eceive a single wo%d st%ing and gets( ) to %eceive a multi0wo%d
st%ing f%om 7eyboa%d we can also use scanf( ) function fo% scanning a multi0wo%d st%ing f%om 7eyboa%d. Hollowing
p%og%am shows how to achieve this.

main( )
{
cha% buff1A" #
scanf ( ('bJn"s(, buff ) #
puts ( buff ) #
)

-n the scanf( ) function we can specify the delimite% in b%ac7ets afte% the b cha%acte%. We have specified 2Jn2 as the
delimite%. 5ence scanf( ) te%minates only when the use% hits Inte% 7ey.
0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
QQ.
5ow to set the system date th%ough a > p%og%am ?

*ns+ We can set the system date using the setdate( ) function as shown in the following p%og%am. ,he function assigns
the cu%%ent time to a
st%uctu%e date.

Dinclude (stdio.h(
Dinclude (dos.h(

main( )
{
st%uct date newTdate #

newTdate.daTmon = 10 #
newTdate.daTday = 19 #
newTdate.daTyea% = 1QQ! #

setdate ( LnewTdate ) #
)
0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
100.
5ow can - w%ite a gene%al0pu%pose swap without using templates?
*ns+ Wiven below is the p%og%am which uses the st%ingi/ing p%ep%ocesso% di%ective DD fo% building a gene%al pu%pose
swap mac%o which can swap two intege%s, two floats, two cha%s, etc.
Ddefine swap( 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 = !.19 #
www.technicalsymposium.com 53
swap ( a, b, int ) #
p%intf ( (Jna = 'd b = 'd(, a, b ) #
swap ( ch1, ch1, cha% ) #
p%intf ( (Jnch1 = 'c ch1 = 'c(, ch1, ch1 ) #
swap ( f1, f1, float ) #
p%intf ( (Jnf1 = '9.1f f1 = '9.1f(, f1, f1 ) #
)
swap ( a, b, int ) would e3pand to,
( gint = ( a ), ( a ) = ( b ), ( b ) = gint )
0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
101.
What is a heap ?

*ns + 5eap is a chun7 of memo%y. When in a p%og%am memo%y is allocated dynamically, the > %un0time lib%a%y gets the
memo%y f%om a collection of unused memo%y called the heap. ,he heap %esides in a p%og%am2s data segment. ,he%efo%e,
the amount of heap space available to the p%og%am is fi3ed, and can va%y f%om one p%og%am to anothe%.
0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
101.
5ow to obtain a path of the given file?

*ns+ ,he function sea%chpath( ) sea%ches fo% the specified file in the subdi%ecto%ies of the cu%%ent path. Hollowing
p%og%am shows how to ma7e use of the sea%chpath( ) function.
Dinclude (di%.h(

void main ( int a%gc, cha% 4a%gv" )
{
cha% 4path #
if ( path = sea%chpath ( a%gv 1 " ) )
p%intf ( (Sathname + 'sJn(, path ) #
else
p%intf ( (Hile not foundJn( ) #
)
0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
10!.
>an we get the p%ocess identification numbe% of the cu%%ent p%og%am?
*ns+ Ves8 ,he mac%o getpid( ) gives us the p%ocess identification numbe% of the p%og%am cu%%ently %unning. ,he
p%ocess id. uniMuely identifies a p%og%am. Bnde% :;., the getpid( ) %etu%ns the S%og%am .egment S%efi3 as the p%ocess
id. Hollowing p%og%am illust%ates the use of this mac%o.
Dinclude Estdio.hF
Dinclude Ep%ocess.hF
void main( )
{
p%intf ( (,he p%ocess identification numbe% of this p%og%am is '\Jn(,
getpid( ) ) #
)
0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
www.technicalsymposium.com 54
109.
5ow do - w%ite a function that ta7es va%iable numbe% of a%guments?
*ns+ ,he following p%og%am demonst%ates this.
Dinclude Estdio.hF
Dinclude Estda%g.hF
void main( )
{
int i = 10 #
float f = 1.A #
cha% 4st% = (5ello8( #
vfpf ( ('d 'f 'sJn(, i, f, st% ) #
vfpf ( ('s 's(, st%, (5i8( ) #
)
void vfpf ( cha% 4fmt, ... )
{
vaTlist a%gpt% #
vaTsta%t ( a%gpt%, fmt ) #
vfp%intf ( stdout, fmt, a%gpt% ) #
vaTend ( a%gpt% ) #
)
5e%e, the function vfpf( ) has called vfp%intf( ) that ta7e va%iable a%gument lists. vaTlist is an a%%ay that holds
info%mation %eMui%ed fo% the mac%os vaTsta%t and vaTend. ,he mac%os vaTsta%t and vaTend p%ovide a po%table way to
access the va%iable a%gument lists. vaTsta%t would set up a pointe% a%gpt% to point to the fi%st of the va%iable a%guments
being passed to the function. ,he mac%o vaTend helps the called function to pe%fo%m a no%mal %etu%n.
0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
10A.
>an we change the system date to some othe% date?
*ns+ Ves, We can8 ,he function stime( ) sets the system date to the specified date. -t also sets the system time. ,he
time and date is measu%ed in seconds f%om the 00+00+00 WP,, ]anua%y 1, 1Q?0. ,he following p%og%am shows how to
use this function.
Dinclude Estdio.hF
Dinclude Etime.hF
void main( )
{
timeTt tm #
int d #
tm = time ( 6B<< ) #
p%intf ( (,he .ystem :ate + 's(, ctime ( Ltm ) ) #
p%intf ( (Jn5ow many days ahead you want to set the date + ( ) #
scanf ( ('d(, Ld ) #
tm $= ( 19< 4 d ) 4 =0< 4 =0< #
stime ( Ltm ) #
p%intf ( (Jn6ow the new date is + 's(, ctime ( Ltm ) ) #
)
www.technicalsymposium.com 55
-n this p%og%am we have used function ctime( ) in addition to function stime( ). ,he ctime( ) function conve%ts time
value to a 1=0cha%acte% long st%ing that contains date and time.
0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
10=.
5ow to use function st%dup( ) in a p%og%am?
*ns + ,he st%ing function st%dup( ) copies the given st%ing to a new location. ,he function uses malloc( ) function to
allocate space %eMui%ed fo% the duplicated st%ing. -t ta7es one a%gument a pointe% to the st%ing to be duplicated. ,he total
numbe% of cha%acte%s p%esent in the given st%ing plus one bytes get allocated fo% the new st%ing. *s this function uses
malloc( ) to allocate memo%y, it is the p%og%amme%Ys %esponsibility to deallocate the memo%y using f%ee( ).
Dinclude Estdio.hF
Dinclude Est%ing.hF
Dinclude Ealloc.hF
void main( )
{
cha% 4st%1, 4st%1 = (double(#
st%1 = st%dup ( st%1 ) #
p%intf ( ('sJn(, st%1 ) #
f%ee ( st%1 ) #
)
0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
10?.
;n including a file twice - get e%%o%s %epo%ting %edefinition of function.
5ow can - avoid duplicate inclusion?
*ns+ Uedefinition e%%o%s can be avoided by using the following mac%o definition. -nclude this definition in the heade%
file.
Dif 8defined filenameTh
Ddefine filenameTh
K4 function definitions 4K
Dendif
Ueplace filenameTh with the actual heade% file name. Ho% e3ample, if name of file to be included is 2goto.h2 then %eplace
filenameTh with 2gotoTh2.
0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
10@.
5ow to w%ite a swap( ) function which swaps the values of the va%iables using bitwise ope%ato%s.
*ns+ 5e%e is the swap( ) function.
swap ( int 43, int 4y )
{
43 b= 4y #
4y b= 43 #
43 b= 4y #
)
,he swap( ) function uses the bitwise \;U ope%ato% and does not %eMui%e any tempo%a%y va%iable fo% swapping.
0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000

You might also like