You are on page 1of 61

C Interview questions 1

C Interview questions with Examples


- version 1.0
By,
Girish Kumar S
Sotware En!ineer
Glo"al E#!e Sotware $t#.
Ban!alore,%&000'
(o") **+&0&*+10
(ailI#)!irish,umar.s-!lo"ale#!esot..om
GESL confidential
C Interview questions 2
CONTENTS
1. Compilation steps
2. Memory Layout of C program
3. Stack Frame
. !it"ise #rograms
$. String #rograms
%. #ointers &elate' #rograms
(. )ata structure #rograms
*.
GESL confidential
C Interview questions 3
1. Compilation Steps
GESL confidential
C program
Preprossor
Compiler:
Leical !nal"#er
Parser
$ptimi#er
Code generator
!ssem%ler
Li%rar"
&li%c'a(
Lin)er
Eecuta%le *ile
$t+er $%,ect
modules
test'c
test'o
a'out
test's
test'i
C Interview questions -
2. Memory Layout of C program.
GESL confidential
!rguments and
Environment varia%les
Stac)
&grow downwards(
.eap
&grow upwards(
/ata
&initiali#ed static data(
0SS
&uninitiali#ed static data(
Code
&program instructions(
Low !ddr
.ig+ !ddr
C Interview questions 1
3. Frame Pointer(FP) and Stack Pointer(SP)
T+ese t"o register +ol's t+e a''ress of a memory location on t+e stack. ,+ile t+e
stack pointer is a mo-ing reference to a single cell.it/s mo'ifie' "it+ eac+ pus+
or pop0. T+e frame pointer is use' for maintaining a su1routine/s -aria1les. 2ll
local -aria1les are maintaine' on t+e stack in a contiguous 1lock create' "+en a
su1routine is entere'.at run3time0. T+e su1routine/s arguments are also kept on t+e
stack4 in t+e same memory 1lock as t+e -aria1les. T+is 1lock of memory is a
su1routine/s stack frame4 an' t+e frame pointer is use' to reference t+e -arious
memory locations comprise' 1y t+e stack frame.
Stack frames are create' "+en a su1routine is entere'4 t+ey are 'elete' "+en t+e
su1routine returns. Conse5uently t+e same area of memory.t+e stack0 is constantly
recycle' as -arious su1routines e6ecute. T+at is4 se-eral su1routines use t+e same
part of t+e stack for t+eir local -aria1les.
E67
int p1 8 14 p2 8 24 p3 8 39
foo.int p14 int p24 int p30
:
int -14 -24 -39
return ;9
<
main.0
:
foo.p14 p24 p309
<
=alf of t+e stack frame is create' 1y t+e calling su1routine3main.0 in t+is case
an' ot+er +alf 1y foo.0 itself. Main.0 passes arguments to foo.0 1y pus+ing t+em
onto t+e stack9 t+ese pus+e' arguments are t+e first part of t+e ne" stack frame.
T+e arguments are pus+e' from rig+t to left.p3 is pus+e' first. >n C 4 all argument
passing .e6cept for arrays0 is 'one 1y -alue. T+at is 4 t+e -alue of t+e -aria1le
p3 is passe' to foo.04 not p3 itself.
T+e stack7
GESL confidential
2333 SP 1444:
1442:
144-:
1445:
1
3
2
C Interview questions 5
Fig7 T+e partially create' stack frame for foo.0. T+e contents of t+e original
-aria1les +a-e 1een copie' +ere.
Ne6t4 t+e su1routine foo.0 is actually calle'. T+is call puts t+e return a''ress on
to t+e stack.
Fig 7 T+e stack imme'iately after foo.0 is Entere'.
."e are no" in su1routine foo.0 1ut +a-e not yet e6ecute' any co'e in foo.0.0
T+e first t+ing t+at foo.0 'oes is preser-e t+e e6isting frame pointer 1y pus+ing
it. T+en it copies t+e current stack pointer into t+e frame pointer.
Fig 7 T+e stack after t+e ol' stack frame is sa-e'.
Ne6t4 foo.0 allocates space for its o"n local -aria1les. T+is space is car-e' out
of t+e stack 1y su1tracting a suita1ly large constant from t+e stack pointer.
GESL confidential
2333 SP
1444:
1442:
144-:
1445:
1
3
2
6eturn address
778:
2333 SP 2333 *P
1444:
1442:
144-:
1445:
1
3
2
$ld frame pointer
778:
6eturn address
775:
C Interview questions 9
Fig7 T+e &emain'er of t+e stack frame is create'
,+en t+e su1routine returns. Foo.0 +as to 'elete all t+e parts of t+e stack frame
t+at it create'. Local -aria1les are 'elete' 1y copying t+e current -alue of t+e
frame pointer into t+e stack pointer4 making t+e stack pointer point to t+e same
place as t+e frame pointer.
Fig7 T+e local -aria1les are 'elete'.
GESL confidential
2333 *P
1444:
1442:
144-:
1445:
1
3
2
$ld frame pointer
778:
6eturn address
775:
v1
v3
v2
2333 SP
77-:
772:
774:
2333 SP 2333 *P
1444:
1442:
144-:
1445:
1
3
2
$ld frame pointer
778:
6eturn address
775:
C Interview questions 8
Ne6t4 t+e ol' frame pointer is restore' 1y popping its ol' -alue off t+e stack.
Fig7 T+e ol' frame pointer is restore' "it+ a pop
T+en foo.0 e6ecutes a &ET instruction4 returning to main.0 an' popping its return
a''ress off t+e stack. T+e stack no" looks ?ust as it 'i' 1efore foo.0 "as calle'.
Fig7 T+e stack after foo.0 returns.
No"4 main.0 t+ro"s a"ay t+e stack -aria1les associate' "it+ t+e arguments. Since
t+e arguments to a calle' su1routine aren/t use' 1y main.04 it 'oesn/t 1ot+er to
pop t+em off t+e stack 1ack into t+e original -aria1les.
GESL confidential
2333 SP
1444:
1442:
144-:
1445:
1
3
2
6eturn address
778:
2333 SP 1444:
1442:
144-:
1445:
:
:
:
C Interview questions 7
Entire stack frame 'iagram of main.0 program.
&eturn -alue from t+e stack is returne' 1y register.accumulator0. >f you are not
returning any t+ing t+e gar1age "ill 1e store' in register.
GESL confidential
v3
v2
v1
$ld *P
6eturn address
p2
$ld *P
6eturn address
p3
p1
argc
argv
2333 SP
2333 *P
C Interview questions 14
!. "it#ise programs
1.a''ing t"o integer
int a''.int a4 int 10
:
int temp 8 ;9
"+ile .10 :
temp 8 a @ 19
1 8 .a A 10 BB 19
a 8 temp9
<
return a9
<
333333333333333333333333333333333333333333
2. Su1traction of t"o integer
int 1itCsu1 .int a4 int 10 :
"+ile .10 :
int t19
t1 8 a @ 19
1 8 .Da A 10 BB 19
a 8 t19
<
return a9
<
3333333333333333333333333333333333333333333
3. Multiplication
int 1itCmul .int a4 int 10 :
int res 8 ;9
int i9
for .i 8 ;9 i B siEeof .10 F * 3 1 9 iGG0
if . 1 A . 1 BB i00
res G8 a BB i9
return res9
<
int multiply.int a4 int 10
:
int result 8 ;9
"+ile .10 :
if .1 A ;10
result G8 a9
a BB8 19
GESL confidential
C Interview questions 11
1 HH8 19
<
return result9
<
333333333333333333333333333333333333333333333333333333
. !its siEe
int main.0
:
unsigne' c+ar i 8 ;9
int count 8 ;9
i 8 Di9
"+ile .i0 :
i 8 i HH 19
countGG9
<
printf.I J' KnI4 count09
e6it.EL>TCSMCCESS09
<
33333333333333333333333333333333333333333333333333333333
$. !its re-erse
int 1itCre-.int num0
:
unsigne' int temp 8 ;9
int i9
for .i 8 ..siEeof.num0 F *00 3 19 i9 i330 :
temp 8 temp N .num A ;109
temp BB8 19
num HH8 19
<
temp 8 temp N .num A ;109
return temp9
<
3333333333333333333333333333333333333333333333333333333333
%. !its count
int fnC1its1.int num0
:
int i9
for .i 8 ;9 num O8 ;9 num HH8 10 :
if .num A 10
iGG9
<
return i9
<
GESL confidential
C Interview questions 12
33333333333333333333333333333333333333333333333333333333333
(. atoi
int atoiC1.c+ar strPQ0
:
int i 8 ;9
"+ile .Fstr0 :
i 8 .i BB 30 G .i BB 10 G .Fstr 3 /;/09
strGG9
<
return i9
<
333333333333333333333333333333333333333333333333333333333333
*. Setting nt+ 1it.
int setCfn.int num4 int pos0
:
printf.I J' KnI4.num N8 .1 BB pos0009
return .num A8 .1 BB pos009
<
3333333333333333333333333333333333333333333333333333
R. po"er of 2
int po"eroft"o.int num0
:
if.num A .num3100
return T&ME9
else
return F2LSE9
<
3333333333333333333333333333333333333333333333333
1;. !yte 'isassem1ly
input 7 $F
output 7 ;$;F
main.0
:
int -al 8 ;6$f9
int mask 8 ;6;f9
int tmp 8 ;4 result 8 ;9
tmp 8 -al HH 9
tmp 8 tmp BB *9
-al 8 -al A mask9
-al 8 -al N tmp9
result 8 -al9
printf.IJ6KnI4 result09
GESL confidential
C Interview questions 13
<
3333333333333333333333333333333333333
11.con-ert from one En'ian to anot+er.
int myre-ersefunc.int num0
:
int 1yte;4 1yte14 1yte24 1yte39
1yte; 8 .num A 6;;;;;;FF0 HH ; 9
1yte1 8 .num A 6;;;;FF;;0 HH * 9
1yte2 8 .num A 6;;FF;;;;0 HH 1% 9
1yte3 8 .num A 6FF;;;;;;0 HH 2 9
return..1yte; BB 20 N .1yte1 BB 1%0 N .1yte2 BB *0 N .1yte3 BB ;009
<
333333333333333333333333333333333333333333333333333
SSSSS Teneral programs SSSSSSSSSSSSSSSSSSSSS
1.pointer allocation
S'efine &O, $
S'efine COL $
int main.0
:
int FFFp9
int i4 ?9
p 8 .int FFF0 malloc.siEeof.intF009
for .i 8 ;9 i B &O,9 iGG0
pPiQ 8 .intFF0 malloc.siEeof.intF0 F COL09
for .i 8 ;9 i B &O,9 iGG0
for.? 8 ;9 ? B COL9 ?GG0
pPiQP?Q 8 .intF0 malloc.siEeof.int0 F &O, F COL09
<
3333333333333333333333333333333333333333333333333333
2. S"apping t"o num1ers
-oi' s"ap3.int a4 int 10
:
printf.Ia 8 J' 1 8 J' KnI4 a4 109
a 8 a G 19
1 8 a 3 19
a 8 a 3 19
printf.Ia 8 J' 1 8 J' KnI4 a4 109
<
-oi' s"ap1.int a4 int 10
GESL confidential
C Interview questions 1-
:
a@81@8a@819
printf.Ia 8 J' 1 8 J' KnI4 a4 109
<
-oi' s"ap2.int a4 int 10
:
a 8 a @ 19
1 8 a @ 19
a 8 a @ 19
printf.Ia 8 J' 1 8 J' KnI4 a4 109
<
3333333333333333333333333333333333333333333333333333
3. String re-erse
-oi' re-erse.c+ar Fs0
:
fnCre-erse.s4 ;4 strlen.s009
<
-oi' fnCre-erse.c+ar sPQ4 int i4 int len0
:
int c4 ?9
? 8 len 3 .iG 109
if .i B ?0 :
c 8 sPiQ9
sPiQ 8 sP?Q9
sP?Q 8 c9
fnCre-erse.s4 GGi4 len09
<
<
333333333333333333333333333333333333333333333333333
. E-en or o'' num1ers
S'efine C=ECU.60 ..6 A ;10 V ; 7 10
3333333333333333333333333333333333333333333333333333
$. !iggest of t"o num1ers
S'efine M2L.a4 10 ...a0 H .100 V .a0 7 .100
333333333333333333333333333333333333333333333333333333333333
%.
S'efine myisalnum.c0 ..c H8 /a/ AA c B8 /E/0NN.c H8 /2/ AA c B8 /W/0 NN . c H8 /;/
AA c B8 /R/00 V SMCCESS 7 F2>LM&E
S'efine myisalp+a.c0 ..c H8 /a/ AA c B8 /E/0 NN .c H8 /2/ AA c B8 /W/00 V SMCCESS 7
F2>LM&E
GESL confidential
C Interview questions 11
S'efine myis'igit.c0 ..c H8 /;/ AA c B8 /R/00 V SMCCESS 7 F2>LM&E
S'efine myis6'igit.C0 ..c H8 /;/ AA c B8 /R/0 NN .c H8 /a/ AA c B8 /E/0 NN .c H8
/2/ AA c B8 /W/00 V SMCCESS 7 F2>LM&E
S'efine myisprint.c0 ..c H8 ;62;0 AA .c B8 ;6(E00 V SMCCESS 7 F2>LM&E
S'efine myisupper.c0 .c H8 /2/ AA c B8 /W/0 V SMCCESS 7 F2>LM&E
S'efine myiscntrl.c0 ...c H8 /K;/0 AA .c B8 ;61F00 NN .c 88 ;6(F00 V SMCCESS 7
F2>LM&E
S'efine myislo"er.c0 .c H8 /a/ AA c B8 /E/0 V SMCCESS 7 F2>LM&E
333333333333333333333333333333333333333333333333333333333333333333333333333333
(.
S'efine litC1ig.n0 .F.c+ar F0 An H8 10 V 1 7 ;
S'efine litC1ig.n0 . F.c+ar F0 An0 V ; 7 1
S'efine larg.a414c4'0 ..a H 10 V ..a H c0 V ..a H '0 V a 7 '0 7 ..c H '0 V c 7 '00
7 ..1 H c0 V ..1 H '0 V 1 7 '0 7 ..c H '0
V c 7 '000
S'efine mysiEeof.60 ..int0.A6 G 10 3 .int0A60
S'efine mystrin'e6.s4t4i0 : K
int ?4 k4 flag 8 ;9K
for.i 8 ;9 sPiQ O8 /K;/9 iGG0 :K
for.? 8 i4 k 8 ;9 tPkQ O8 /K;/ AA sP?Q 88 tPkQ9
?GG4 kGG0K
9K
if.k H ; AA tPkQ 88 /K;/0 : flag 8 19 1reak9 <
K
<K
if.Oflag0 i 8 319 K
<K
33333333333333333333333333333333333333333333333333333333333333333333
*. Factorial of a num1er
int fact.int num0
:
if .num 88 10
return 19
else
return .num F fact.num 3 1009
<
33333333333333333333333333333333333333333333333333333
R. count lines4 "or's4 an' c+aracters in input
main.0
:
int c4 nl4 n"4 nc4 state9
state 8 OMT9
GESL confidential
C Interview questions 15
nl 8 n" 8 nc 8 ;9
"+ile ..c 8 getc+ar.00 O8 EOF0 :
GGnc9
if .c 88 /Kn/0
GGnl9
if .c 88 / / NN c 88 /Kn/ NN c 8 /Kt/0
state 8 OMT9
else if .state 88 OMT0 :
state 8 >N9
GGn"9
<
<
printf.IJ' J' J'KnI4 nl4 n"4 nc09
<
33333333333333333333333333333333333333333333333333333333
R. String lengt+
int strlen.c+ar sPQ0
:
int i9
"+ile .sPiQ O8 /K;/0
GGi9
return i9
<
int strlen.c+ar Fs0
:
int n9
for .n 8 ;9 Fs O8 /K;/4 sGG0
nGG9
return n9
<
int strlen.c+ar Fs0
:
c+ar Fp 8 s9
"+ile .Fp O8 /K;/0
pGG9
return p 3 s9
<
33333333333333333333333333333333333333333333333333333333
1;. lo"er7 con-ert c to lo"er case9 2SC>> only
int lo"er.int c0
GESL confidential
C Interview questions 19
:
if .c H8 /2/ AA c B8 /W/0
return c G /a/ 3 /2/9
else
return c9
<
333333333333333333333333333333333333333333333333333333
11. strcat7 concatenate t to en' of s9 s must 1e 1ig enoug+
-oi' strcat.c+ar sPQ4 c+ar tPQ0
:
int i4 ?9
i 8 ? 8 ;9
"+ile .sPiQ O8 /K;/0 XF fin' en' of s FX
iGG9
"+ile ..sPiGGQ 8 tP?GGQ0 O8 /K;/0 XF copy t FX
9
<
33333333333333333333333333333333333333333333333333333333
12. 1insearc+7 fin' 6 in -P;Q B8 -P1Q B8 ... B8 -Pn31Q
int 1insearc+.int 64 int -PQ4 int n0
:
int lo"4 +ig+4 mi'9
lo" 8 ;9
+ig+ 8 n 3 19
"+ile .lo" B8 +ig+0 :
mi' 8 .lo"G+ig+0X29
if .6 B -Pmi'Q0
+ig+ 8 mi' G 19
else if .6 H -Pmi'Q0
lo" 8 mi' G 19
else XF foun' matc+ FX
return mi'9
<
return 319 XF no matc+ FX
<
333333333333333333333333333333333333333333333333333
13. atoi7 con-ert s to integer
int atoi.c+ar sPQ0
:
int i4 n4 sign9
for .i 8 ;9 isspace.sPiQ09 iGG0 XF skip "+ite space FX
9
sign 8 .sPiQ 88 /3/0 V 31 7 19
GESL confidential
C Interview questions 18
if .sPiQ 88 /G/ NN sPiQ 88 /3/0 XF skip sign FX
iGG9
for .n 8 ;9 is'igit.sPiQ09 iGG0
n 8 1; F n G .sPiQ 3 /;/09
return sign F n9
<
333333333333333333333333333333333333333333333333333333
1. itoa
-oi' re-erse.c+ar sPQ0
:
int c4 i4 ?9
for .i 8 ;4 ? 8 strlen.s0319 i B ?9 iGG4 ?330 :
c 8 sPiQ9
sPiQ 8 sP?Q9
sP?Q 8 c9
<
<
-oi' itoa.int n4 c+ar sPQ0
:
int i4 sign9
if ..sign 8 n0 B ;0 XF recor' sign FX
n 8 3n9 XF make n positi-e FX
i 8 ;9
'o : XF generate 'igits in re-erse or'er FX
sPiGGQ 8 n J 1; G /;/9 XF get ne6t 'igit FX
< "+ile ..n X8 1;0 H ;09 XF 'elete it FX
if .sign B ;0
sPiGGQ 8 /3/9
sPiQ 8 /K;/9
re-erse.s09
<
333333333333333333333333333333333333333333333333333333
1$. trim7 remo-e trailing 1lanks4 ta1s4 ne"lines
int trim.c+ar sPQ0
:
int n9
for .n 8 strlen.s0319 n H8 ;9 n330
if .sPnQ O8 / / AA sPnQ O8 /Kt/ AA sPnQ O8 /Kn/0
1reak9
sPnG1Q 8 /K;/9
return n9
GESL confidential
C Interview questions 17
<
333333333333333333333333333333333333333333333333333333333333
1%. 5sort7 sort -PleftQ...-Prig+tQ into increasing or'er
-oi' 5sort.int -PQ4 int left4 int rig+t0
:
int i4 last9
-oi' s"ap.int -PQ4 int i4 int ?09
if .left H8 rig+t0 XF 'o not+ing if array contains FX
return9 XF fe"er t+an t"o elements FX
s"ap.-4 left4 .left G rig+t0X209 XF mo-e partition elem FX
last 8 left9 XF to -P;Q FX
for .i 8 left G 19 i B8 rig+t9 iGG0 XF partition FX
if .-PiQ B -PleftQ0
s"ap.-4 GGlast4 i09
s"ap.-4 left4 last09 XF restore partition elem FX
5sort.-4 left4 last3109
5sort.-4 lastG14 rig+t09
<
-oi' s"ap.int -PQ4 int i4 int ?0
:
int temp9
temp 8 -PiQ9
-PiQ 8 -P?Q9
-P?Q 8 temp9
<
33333333333333333333333333333333333333333333333333333333333
1(. strcpy7 copy t to s9 array su1script -ersion
-oi' strcpy.c+ar Fs4 c+ar Ft0
:
int i9
i 8 ;9
"+ile ..sPiQ 8 tPiQ0 O8 /K;/0
iGG9
<
-oi' strcpy.c+ar Fs4 c+ar Ft0
:
int i9
i 8 ;9
"+ile ..Fs 8 Ft0 O8 /K;/0 :
GESL confidential
C Interview questions 24
sGG9
tGG9
<
<
-oi' strcpy.c+ar Fs4 c+ar Ft0
:
"+ile ..FsGG 8 FtGG0 O8 /K;/0
9
<
-oi' strcpy.c+ar Fs4 c+ar Ft0
:
"+ile .FsGG 8 FtGG0
9
<
33333333333333333333333333333333333333333333333333333333
1*. strcmp7 return B; if sBt4 ; if s88t4 H; if sHt
int strcmp.c+ar Fs4 c+ar Ft0
:
int i9
for .i 8 ;9 sPiQ 88 tPiQ9 iGG0
if .sPiQ 88 /K;/0
return ;9
return sPiQ 3 tPiQ9
<
int strcmp.c+ar Fs4 c+ar Ft0
:
for . 9 Fs 88 Ft9 sGG4 tGG0
if .Fs 88 /K;/0
return ;9
return Fs 3 Ft9
<
int strcmp.const c+ar F cs4const c+ar F ct0
:
register signe' c+ar CCres9
"+ile .10 :
if ..CCres 8 Fcs 3 FctGG0 O8 ; NN OFcsGG0
1reak9
<
return CCres9
<
GESL confidential
C Interview questions 21
33333333333333333333333333333333333333333333333333333333333333
1R. 1insearc+7 fin' "or' in ta1P;Q...ta1Pn31Q
int 1insearc+.c+ar F"or'4 struct key ta1PQ4 int n0
:
int con'9
int lo"4 +ig+4 mi'9
lo" 8 ;9
+ig+ 8 n 3 19
"+ile .lo" B8 +ig+0 :
mi' 8 .lo"G+ig+0 X 29
if ..con' 8 strcmp."or'4 ta1Pmi'Q."or'00 B ;0
+ig+ 8 mi' 3 19
else if .con' H ;0
lo" 8 mi' G 19
else
return mi'9
<
return 319
<
3333333333333333333333333333333333333333333333333333333333333333
2;.
XFF
F strnicmp 3 Case insensiti-e4 lengt+3limite' string comparison
F Ys17 One string
F Ys27 T+e ot+er string
F Ylen7 t+e ma6imum num1er of c+aracters to compare
FX
int strnicmp.const c+ar Fs14 const c+ar Fs24 siEeCt len0
:
XF Zes4 [irginia4 it +a' 1etter 1e unsigne' FX
unsigne' c+ar c14 c29
c1 8 ;9 c2 8 ;9
if .len0 :
'o :
c1 8 Fs19 c2 8 Fs29
s1GG9 s2GG9
if .Oc10
1reak9
if .Oc20
1reak9
if .c1 88 c20
continue9
c1 8 tolo"er.c109
c2 8 tolo"er.c209
GESL confidential
C Interview questions 22
if .c1 O8 c20
1reak9
< "+ile .33len09
<
return .int0c1 3 .int0c29
<
333333333333333333333333333333333333333333333333333333333
21.strncpy
c+ar F strncpy.c+ar F 'est4const c+ar Fsrc4siEeCt count0
:
c+ar Ftmp 8 'est9
"+ile .count33 AA .F'estGG 8 FsrcGG0 O8 /K;/0
XF not+ing FX9
return tmp9
<
333333333333333333333333333333333333333333333333333333333333
22.
c+ar F strcat.c+ar F 'est4 const c+ar F src0
:
c+ar Ftmp 8 'est9
"+ile .F'est0
'estGG9
"+ile ..F'estGG 8 FsrcGG0 O8 /K;/0
9
return tmp9
<
33333333333333333333333333333333333333333333333333333333
23.
c+ar F strncat.c+ar F'est4 const c+ar Fsrc4 siEeCt count0
:
c+ar Ftmp 8 'est9
if .count0 :
"+ile .F'est0
'estGG9
"+ile ..F'estGG 8 FsrcGG00 :
if .33count 88 ;0 :
F'est 8 /K;/9
1reak9
<
<
<
return tmp9
<
333333333333333333333333333333333333333333333333333333333333
2.
GESL confidential
C Interview questions 23
XFF
F strc+r 3 Fin' t+e first occurrence of a c+aracter in a string
F Ys7 T+e string to 1e searc+e'
F Yc7 T+e c+aracter to searc+ for
FX
c+ar F strc+r.const c+ar F s4 int c0
:
for.9 Fs O8 .c+ar0 c9 GGs0
if .Fs 88 /K;/0
return NMLL9
return .c+ar F0 s9
<
33333333333333333333333333333333333333333333333333333333
2$.
XFF
F strrc+r 3 Fin' t+e last occurrence of a c+aracter in a string
F Ys7 T+e string to 1e searc+e'
F Yc7 T+e c+aracter to searc+ for
FX
c+ar F strrc+r.const c+ar F s4 int c0
:
const c+ar Fp 8 s G strlen.s09
'o :
if .Fp 88 .c+ar0c0
return .c+ar F0p9
< "+ile .33p H8 s09
return NMLL9
<
3333333333333333333333333333333333333333333333333333333333
2%.
siEeCt strnlen.const c+ar F s4 siEeCt count0
:
const c+ar Fsc9
for .sc 8 s9 count33 AA Fsc O8 /K;/9 GGsc0
XF not+ing FX9
return sc 3 s9
<
333333333333333333333333333333333333333333333333333
2(.
XFF
F strspn 3 Calculate t+e lengt+ of t+e initial su1string of Ys "+ic+ only
F contain letters in Yaccept
F Ys7 T+e string to 1e searc+e'
F Yaccept7 T+e string to searc+ for
FX
siEeCt strspn.const c+ar Fs4 const c+ar Faccept0
:
const c+ar Fp9
const c+ar Fa9
GESL confidential
C Interview questions 2-
siEeCt count 8 ;9
for .p 8 s9 Fp O8 /K;/9 GGp0 :
for .a 8 accept9 Fa O8 /K;/9 GGa0 :
if .Fp 88 Fa0
1reak9
<
if .Fa 88 /K;/0
return count9
GGcount9
<
return count9
<
3333333333333333333333333333333333333333333333333333333333333333333333333
2*.
XFF
F strp1rk 3 Fin' t+e first occurrence of a set of c+aracters
F Ycs7 T+e string to 1e searc+e'
F Yct7 T+e c+aracters to searc+ for
FX
c+ar F strp1rk.const c+ar F cs4const c+ar F ct0
:
const c+ar Fsc14Fsc29
for. sc1 8 cs9 Fsc1 O8 /K;/9 GGsc10 :
for. sc2 8 ct9 Fsc2 O8 /K;/9 GGsc20 :
if .Fsc1 88 Fsc20
return .c+ar F0 sc19
<
<
return NMLL9
<
33333333333333333333333333333333333333333333333333333333333333
2R.
XFF
F strtok 3 Split a string into tokens
F Ys7 T+e string to 1e searc+e'
F Yct7 T+e c+aracters to searc+ for
F
F ,2&N>NT7 strtok is 'eprecate'4 use strsep instea'.
FX
c+ar F CCCstrtok9
c+ar F strtok.c+ar F s4const c+ar F ct0
:
c+ar Fs1egin4 Fsen'9
s1egin 8 s V s 7 CCCstrtok9
if .Os1egin0 :
return NMLL9
GESL confidential
C Interview questions 21
<
s1egin G8 strspn.s1egin4ct09
if .Fs1egin 88 /K;/0 :
CCCstrtok 8 NMLL9
return. NMLL 09
<
sen' 8 strp1rk. s1egin4 ct09
if .sen' AA Fsen' O8 /K;/0
Fsen'GG 8 /K;/9
CCCstrtok 8 sen'9
return .s1egin09
<
33333333333333333333333333333333333333333333333333333333333333333
3;.
XFF
F strsep 3 Split a string into tokens
F Ys7 T+e string to 1e searc+e'
F Yct7 T+e c+aracters to searc+ for
F
F strsep.0 up'ates Ys to point after t+e token4 rea'y for t+e ne6t call.
F
F >t returns empty tokens4 too4 1e+a-ing e6actly like t+e li1c function
F of t+at name. >n fact4 it "as stolen from gli1c2 an' 'e3fancy3fie'.
F Same semantics4 slimmer s+ape. 90
FX
c+ar F strsep.c+ar FFs4 const c+ar Fct0
:
c+ar Fs1egin 8 Fs4 Fen'9
if .s1egin 88 NMLL0
return NMLL9
en' 8 strp1rk.s1egin4 ct09
if .en'0
Fen'GG 8 /K;/9
Fs 8 en'9
return s1egin9
<
33333333333333333333333333333333333333333333333333333
31.
-oi' F memset.-oi' F s4int c4siEeCt count0
:
c+ar F6s 8 .c+ar F0 s9
"+ile .count330
F6sGG 8 c9
return s9
<
333333333333333333333333333333333333333333333
GESL confidential
C Interview questions 25
32.
-oi' F memcpy.-oi' F 'est4const -oi' Fsrc4siEeCt count0
:
c+ar Ftmp 8 .c+ar F0 'est4 Fs 8 .c+ar F0 src9
"+ile .count330
FtmpGG 8 FsGG9
return 'est9
<
33333333333333333333333333333333333333333333333333
33.
-oi' F memmo-e.-oi' F 'est4const -oi' Fsrc4siEeCt count0
:
c+ar Ftmp4 Fs9
if .'est B8 src0 :
tmp 8 .c+ar F0 'est9
s 8 .c+ar F0 src9
"+ile .count330
FtmpGG 8 FsGG9
<
else :
tmp 8 .c+ar F0 'est G count9
s 8 .c+ar F0 src G count9
"+ile .count330
F33tmp 8 F33s9
<
return 'est9
<
3333333333333333333333333333333333333333333333333333333333
3.
int memcmp.const -oi' F cs4const -oi' F ct4siEeCt count0
:
const unsigne' c+ar Fsu14 Fsu29
int res 8 ;9
for. su1 8 cs4 su2 8 ct9 ; B count9 GGsu14 GGsu24 count330
if ..res 8 Fsu1 3 Fsu20 O8 ;0
1reak9
return res9
<
333333333333333333333333333333333333333333333333333333333333333333
3$.
XFF
F memscan 3 Fin' a c+aracter in an area of memory.
F Ya''r7 T+e memory area
F Yc7 T+e 1yte to searc+ for
GESL confidential
C Interview questions 29
F YsiEe7 T+e siEe of t+e area.
F
F returns t+e a''ress of t+e first occurrence of Yc4 or 1 1yte past
F t+e area if Yc is not foun'
FX
-oi' F memscan.-oi' F a''r4 int c4 siEeCt siEe0
:
unsigne' c+ar F p 8 .unsigne' c+ar F0 a''r9
"+ile .siEe0 :
if .Fp 88 c0
return .-oi' F0 p9
pGG9
siEe339
<
return .-oi' F0 p9
<
3333333333333333333333333333333333333333333333333333333333333333333333
3%.
XFF
F strstr 3 Fin' t+e first su1string in a JNML terminate' string
F Ys17 T+e string to 1e searc+e'
F Ys27 T+e string to searc+ for
FX
c+ar F strstr.const c+ar F s14const c+ar F s20
:
int l14 l29
l2 8 strlen.s209
if .Ol20
return .c+ar F0 s19
l1 8 strlen.s109
"+ile .l1 H8 l20 :
l1339
if .Omemcmp.s14s24l200
return .c+ar F0 s19
s1GG9
<
return NMLL9
<
33333333333333333333333333333333333333333333333333333333333333333333333
3(.
XFF
F memc+r 3 Fin' a c+aracter in an area of memory.
F Ys7 T+e memory area
F Yc7 T+e 1yte to searc+ for
F Yn7 T+e siEe of t+e area.
F
F returns t+e a''ress of t+e first occurrence of Yc4 or JNMLL
F if Yc is not foun'
FX
-oi' Fmemc+r.const -oi' Fs4 int c4 siEeCt n0
GESL confidential
C Interview questions 28
:
const unsigne' c+ar Fp 8 s9
"+ile .n33 O8 ;0 :
if ..unsigne' c+ar0c 88 FpGG0 :
return .-oi' F0.p3109
<
<
return NMLL9
<
333333333333333333333333333333333333333333333333333333333
3*. TC)
int gc'.int m4 int n0
:
"+ile.m O8 n0 XF !e in t+e loop till m A n are e5ual FX
:
if.m H n0
m 8 m 3 n9
else
n 8 n 3 m9
<
return m9
<
int gc'.int m4 int n0
:
int r9
"+ile.n O8 ;0
:
r 8 m J n9
m 8 n9
n 8 r9
<
return m9
<
33333333333333333333333333333333333333333333333
3R. Fi1onacci num1er
;4 14 14 24 34 $4 *4 134...........
int fi1.int n0
:
if.n 88 10 return ;9
if.n 88 20 return 19
return fi1.n 3 10 G fi1.n 3 209
GESL confidential
C Interview questions 27
<
int fi1.int n0
:
int fPnG1Q9
fP1Q 8 fP2Q 8 19
printf.IKnfP1Q 8 J'I4 fP1Q09
printf.IKnfP2Q 8 J'I4 fP2Q09
for .int i 8 39 i B8 n9 iGG0
:
fPiQ 8 fPi31Q G fPi32Q9
printf.IKnfPJ'Q 8 PJ'QI4i4fPiQ09
<
return fPnQ9
<
-oi' main.0
:
printf.Ifi1 8 J'KnI4 n4 fi1.n009
<
333333333333333333333333333333333333333333333333333333333333333333333333
;. &e-erse a num1er
-oi' re-erse.int n0
:
printf.IJ'I4 n J 1;09
if.n X 1; 88 ;0 return9
re-erse.n X 1;09
<
3333333333333333333333333333333333333333333333
1. &e-erse a linke' list.
type'ef struct no'e
:
int -alue9
struct no'e Fne6t9
< myno'e9
-oi' iterati-eCre-erse.0
:
myno'e Fp4 F54 Fr9
GESL confidential
C Interview questions 34
if.+ea' 88 .myno'e F0;0
:
return9
<
p 8 +ea'9
5 8 p3Hne6t9
p3Hne6t 8 .myno'e F0;9
"+ile .5 O8 .myno'e F0;0
:
r 8 53Hne6t9
53Hne6t 8 p9
p 8 59
5 8 r9
<
+ea' 8 p9
<
myno'eF re-erseCrecurse.myno'e Froot0
:
if.root3Hne6tO8.myno'e F0;0
:
re-erseCrecurse.root3Hne6t09
root3Hne6t3Hne6t8root9
return.root09
<
else
:
+ea'8root9
<
<
333333333333333333333333333333333333333333
2. #rogram to fin' prime num1er
main.0
:
int num4 tmp4 count4 i9

printf.Ienter t+e num1er to 1e c+ecke'I09
scanf.IJ'I4An09
for.i819iB8n9iGG0
:
tmp 8 num J i9
if.tmp 8 ;0
:
count G8 19
<
<

if .count 88 20
GESL confidential
C Interview questions 31
:
printf.It+e gi-en num1er is primeI09
<
else
printf.It+e gi-en num1er is not primeI09
<
333333333333333333333333333333333333333333333
3. Fin' loop in a linke' list
[isite' flag7
=a-e a -isite' flag in eac+ no'e of t+e linke' list.
Flag it as -isite' "+en you reac+ t+e no'e. ,+en you reac+ a no'e
an' t+e flag is alrea'y flagge' as -isite'4 t+en you kno" t+ere
is a loop in t+e linke' list.
Fastest met+o'7
=a-e 2 pointers to start of t+e linke' list. >ncrement one
pointer 1y 1 no'e an' t+e ot+er 1y 2 no'es. >f t+ere/s a loop4
t+e 2n' pointer "ill meet t+e 1st pointer some"+ere. >f it 'oes4
t+en you kno" t+ere/s one.
=ere is some co'e
p8+ea'9
58+ea'3Hne6t9
"+ile.pO8NMLL AA 5O8NMLL0
:
if.p8850
:
XXLoop 'etecte'O
e6it.;09
<
p8p3Hne6t9
58.53Hne6t0V.53Hne6t3Hne6t0753Hne6t9
<
XX No loop.
3333333333333333333333333333333333333333333333333333333
. S"apping a ni11les
unsigne' c+ar s"apCni11les.unsigne' c+ar c0
:
unsigne' c+ar temp14 temp29
temp1 8 c A ;6;F9
temp2 8 c A ;6F;9
temp18 temp1 BB 9
temp28 temp2 HH 9
return .temp2Ntemp109 XXa''ing t+e 1its
<
333333333333333333333333333333333333333333333333333333
GESL confidential
C Interview questions 32
$.
float a83.19
float FFE9
float FFy9
float FFF69
float FFFF-9
float FFFF"9
float FFfun1.float F09
float FFFFfun2.float FFF09
main.0
:
E 8 fun1.Aa09
printf.IJfI4 FFE09
<
float FFfun1.float FE0
:
y 8 AE9
- 8 func2.Ay09
return .FF-09
<
float FFFFfun2.float FFF60
:
" 8 A69
return ."09
<
33333333333333333333333333333333333333333333333
%. #ointers an' t"o 'imensional arrays
stu'P$QP2Q 8 : :1234 $%<4
:12124 33<4
:134 *;<4
:13124 (*<4
:12;34 ($<
<9
for.i 8 ;9 i B8 9 iGG0
:
for.? 8 ;9 ? B8 19 ?GG0
:
printf.IJ'I4 F.F.stu' G i0 G ?009
<
<
GESL confidential
C Interview questions 33
aP3QPQ 8 : 1424344
$4%4(4*4
R4;414%
<9
>f > call 'isplay.a4 34 0 or s+o".a4 34 09
'isplay.int F54 int ro"4 int col0
:
for.i 8 ;9 i B ro"9 iGG0
:
for.? 8 ;9 ? B col9 ?GG0
:
printf.IJ'I4 F.5 G i F col G ?009
<
<
<
s+o".int .F50PQ4 int ro" 4 int col0
:
int Fp9
for.i 8 ;9 i B ro"9 iGG0
:
p 8 5 G 19
for.? 8 ;9 ? B col9 ?GG0
:
printf.IJ'I4 F.p G ?009
<
<
<
print.int 5PQPQ4 int ro"4 int col09
3333333333333333333333333333333333333333333333333333333
(. T+ree )imensional arrays
aP2QP3QP2Q 8 :
:
:24 <4
:(4 *<4
:34 <4
<4
:
:24 2<4
:24 3<4
:34 <4
<
<9
GESL confidential
C Interview questions 3-
>f > call 'isplay.a4 24 34 20 or s+o".a4 24 34 209
'isplay.int F54 int ii4 int ??4 int kk0
:
for.i 8 ;9 iBii9 iGG0
:
for.? 8 ;9 ? B??9 ?GG0
:
for.k 8 ;9 k B kk9 kGG0
printf.IJ'I4 F.5 G i F ?? F kk G ? F kk G k009
<
<
<
s+o".int .F50P3QPQ4 int ii4 int ??4 int kk0
:
int Fp9
for.i 8 ;9 iBii9 iGG0
:
for.? 8 ;9 ? B??9 ?GG0
:
p 8 5PiQP?Q9
for.k 8 ;9 k B kk9 kGG0
printf.IJ'I4 F.5 G i F ?? F kk G ? F kk G k009
<
<
<
print.int 5PQP3QP2Q4 int ii4 int ??4 int kk09
3333333333333333333333333333333333333333333333333333333333333
&eturning 2) array from function7
&O, 8 3 COL 8
int Fa9
int Ffun1.09
int Ffun1.0
:
static int aP&O,QPCOLQ 8 : 14 24 34 4
$4 %4 (4 *4
R4 ;4 14 %
<9
return .int F0a9
<
int .F10PCOLQ9
GESL confidential
C Interview questions 31
int .Ffun2.00PCOLQ9
int Fp9
int .Ffun2.00PCOLQ
:
static int 1P&O,QPCOLQ 8 : 14 24 34 4
$4 %4 (4 *4
R4 ;4 14 %
<9
return 19
<
int .Fc0P&O,QPCOLQ9
int .Ffun3.00:&O,QPCOLQ9
int .Ffun3.00P&O,QPCOLQ
:
static int cP&O,QPCOLQ 8 : 14 24 34 4
$4 %4 (4 *4
R4 ;4 14 %
<9

return .int.F0P&O,QPCOLQ0 c9
<
333333333333333333333333333333333333333333333333333333333
&eturning 3) array from function7
SET 8 2 &O, 8 3 COL 8
int Fa9
int Ffun1.09
int Ffun1.0
:
static int aPSETQP&O,QPCOLQ 8 : 14 24 34 4
$4 %4 (4 *4
R4 ;4 14 %
<9
return .int F0a9
<
int .F10PCOLQ9
int .Ffun2.00PCOLQ9
int Fp9
int .Ffun2.00PCOLQ
:
static int 1PSETQP&O,QPCOLQ 8 : 14 24 34 4
$4 %4 (4 *4
R4 ;4 14 %
GESL confidential
C Interview questions 35
<9
return .int.F0PCOLQ0 19
<
int .Fc0P&O,QPCOLQ9
int .Ffun3.00:&O,QPCOLQ9
int .Ffun3.00P&O,QPCOLQ
:
static int cPSETQP&O,QPCOLQ 8 : 14 24 34 4
$4 %4 (4 *4
R4 ;4 14 %
<9

return .int.F0P&O,QPCOLQ0 c9
<
int .F'0PSETQP&O,QPCOLQ9
int .Ffun.00PSETQP&O,QPCOLQ9
int .Ffun.00PSETQP&O,QPCOLQ
:
static int cPSETQP&O,QPCOLQ 8 : 14 24 34 4
$4 %4 (4 *4
R4 ;4 14 %
<9
return .int .F0PSETQP&O,QPCOLQ0'9
<
3333333333333333333333333333333333333333333333333333
c+ar str1PQ 8 I=elloI9
c+ar str2P1;Q9
c+ar Fs 8 IToo' morningI9
c+ar F59
str2 8 str19 XF error FX
5 8 s9 XF "orks FV
333333333333333333333333333
c+ar Fp 8 I=elloI 9 XF pointer is -aria1le4 so is string FX
F# 8 /M/9 XF "orks FX
p 8 I!yeI9 XF "orks FX
const c+ar F5 8 I=elloI9 XF string is constant pointer is not FX
F5 8 /M/9 XF error FX
5 8 I!yeI9 XF "orks FX
c+ar const Fs 8 I=elloI 9 XF string is constant pointer is not FX
Fs 8 /M/9 XF error FX
s 8 I!yeI9 XF "orks FX
GESL confidential
C Interview questions 39
c+ar Fconst t 8 I=elloI9 XF pointer is constant string is not FX
Ft 8 /M/9 XF "orks FX
t 8 I!yeI9 XF error FX
const c+ar F const u 8 I=elloI9 XF string is constant 4 so is pointer FX
Fu 8 /M/9 XF error FX
u 8 I!yeI9 XF error FX
333333333333333333333333333333333333333333333
2rray of pointer to strings7
c+ar FnamesPQ 8 : Igiris+I4
Ima+es+I4
Ira-iI4
IarunI4
IgopalI
<9
333333333333333333333333333333333333333333333
333333333333333333333333333333333333333333333333333
SSSSSSSSS Function pointers SSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSS
Function #ointers are pointers4 i.e. -aria1les4 "+ic+ point to t+e a''ress of a
function.
XF function returning pointer to int FX
int Ffunc.int a4 float 109
XF pointer to function returning int FX
int .Ffunc0.int a4 float 109
Zou can call t+e function using one of t"o forms7
.Ffunc0.14209
XF or FX
func.14209
33333333333333333333333333333333333
-oi' func.int09
main.0:
-oi' .Ffp0.int09
fp 8 func9
.Ffp0.109
fp.209
e6it.EL>TCSMCCESS09
<
-oi'
func.int arg0:
printf.IJ'KnI4 arg09
GESL confidential
C Interview questions 38
<
33333333333333333333333333333333
you can +a-e an array of pointers to functions4 "it+ 'eclaration an' use like t+is7
-oi' .FfparrPQ0.int4 float0 8 :
XF initialiEers FX
<9
XF t+en call one FX
fparrP$Q.14 3.09
333333333333333333333333333333333333333
type'ef -oi' .Ffn0.-oi'09
-oi' a.0
:
printf.I=> 222KnI09
<
int main.0
:
fn fun9
fun 8 a9
fun.09
printf.I => KnI09
e6it.EL>TCSMCCESS09
<
333333333333333333333333333333333333
int .Ffun0.int4 int09
int a''.int a4 int 10
:
return a G 19
<
int su1.int a4 int 10
:
return a 3 19
<
int function.int .Ffun0 .int4 int04 int 64 int y0
:
return fun.64 y09
<
int main.0
:
printf.I a'' 8 J' KnI4 function.a''4 1;4 2;009
printf.I su1 8 J' KnI4 function.su14 1;4 2;009
e6it.EL>TCSMCCESS09
<
333333333333333333333333333333333333333333333333333
GESL confidential
C Interview questions 37
type'ef int .Ffun0.int4 int09
int a''.int a4 int 10
:
return a G 19
<
int su1.int a4 int 10
:
return a 3 19
<
int function.fun fun14 int 64 int y0
:
return fun1.64 y09
<
int main.0
:
printf.I a'' 8 J' KnI4 function.a''4 1;4 2;009
printf.I su1 8 J' KnI4 function.su14 1;4 2;009
e6it.EL>TCSMCCESS09
<
3333333333333333333333333333333333333333333333333333333
type'ef -oi' .Ffun0.c+ar strPQ09
-oi' fnCre-.c+ar strPQ09
int main.0
:
c+ar strP1;Q9
fun fun19
printf.I Enter name KnI09
scanf.IJsI4 str09
fun1 8 fnCre-9
.Ffun10.str09
printf.I string 8 Js KnI4 str09
e6it.EL>TCSMCCESS09
<
-oi' fnCre-.c+ar strPQ0
:
int c4 i4 ?9
for .i 8 ;4 ? 8 strlen.str0 3 19 i B ?9 iGG4 ?330 :
c 8 strPiQ9
strPiQ 8 strP?Q9
strP?Q 8 c9
<
<
3333333333333333333333333333333333333333333333333
int sum.int a4 int 109
GESL confidential
C Interview questions -4
int su1tract.int a4 int 109
int mul.int a4 int 109
int 'i-.int a4 int 109
int .FpPQ0 .int 64 int y09
or
XF initialiEe t+e pointer array FX
int .FpPQ0 .int 64 int y0 8 :
sum4 su1tract4 mul4 'i-
< 9
int main.-oi'0
:
int result9
int i4 ?4 op9
pP;Q 8 sum9 XF a''ress of sum.0 FX
pP1Q 8 su1tract9 XF a''ress of su1tract.0 FX
pP2Q 8 mul9 XF a''ress of mul.0 FX
pP3Q 8 'i-9 XF a''ress of 'i-.0 FX
printf.IEnter t"o num1ers7 I09
scanf.IJ' J'I4 Ai4 A?09

printf.I;7 2''4 17 Su1tract4 27 Multiply4 37 )i-i'eKnI09
'o :
printf.IEnter num1er of operation7 I09
scanf.IJ'I4 Aop09
< "+ile.opB; NN opH309
result 8 .FpPopQ0 .i4 ?09
printf.IJ'I4 result09
return ;9
<
33333333333333333333333333333333333333333333333333333333333333
-oi' c+eck.c+ar Fa4 c+ar F14int .Fcmp0.const c+ar F4 const c+ar F009
int main.-oi'0
:
c+ar s1P*;Q8 Ias'fI4 s2P*;Q8Ias'fI9
int .Fp0.const c+ar F4 const c+ar F09
p 8 strcmp9
c+eck.s14 s24 p09
return ;9
<
GESL confidential
C Interview questions -1
-oi' c+eck.c+ar Fa4 c+ar F14int .Fcmp0.const c+ar F4 const c+ar F00
:
printf.ITesting for e5uality.KnI09
if.O.Fcmp0.a4 100 :
printf.IE5ualI09
<else :
printf.INot E5ualI09
<
<
33333333333333333333333333333333333333333333333333333333333333333
c+ar FFarg-
arg-7 pointer to c+ar
int .F'ayta10P13Q
'ayta17 pointer to arrayP13Q of int
int F'ayta1P13Q
'ayta17 arrayP13Q of pointer to int
-oi' Fcomp.0
comp7 function returning pointer to -oi'
-oi' .Fcomp0.0
comp7 pointer to function returning -oi'
c+ar .F.F6.00PQ0.0
67 function returning pointer to arrayPQ of
pointer to function returning c+ar
c+ar .F.F6P3Q0.00P$Q
67 arrayP3Q of pointer to function returning
pointer to arrayP$Q of c+ar
33333333333333333333333333333333333333333333333333333333333333333
SSSSSSSSSS [arargs SSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSS
Sinclu'e Bst'io.+H
Sinclu'e Bst'arg.+H
e6tern c+ar Fitoa.int4 c+ar F4 int09
-oi' myprintf.const c+ar Ffmt4 ...0
:
const c+ar Fp9
-aClist argp9
int i9
c+ar Fs9
c+ar fmt1ufP2$%Q9
-aCstart.argp4 fmt09
for.p 8 fmt9 Fp O8 /K;/9 pGG0
:
GESL confidential
C Interview questions -2
if.Fp O8 /J/0
:
putc+ar.Fp09
continue9
<
s"itc+.FGGp0
:
case /c/7
i 8 -aCarg.argp4 int09
putc+ar.i09
1reak9
case /'/7
i 8 -aCarg.argp4 int09
s 8 itoa.i4 fmt1uf4 1;09
fputs.s4 st'out09
1reak9
case /s/7
s 8 -aCarg.argp4 c+ar F09
fputs.s4 st'out09
1reak9
case /6/7
i 8 -aCarg.argp4 int09
s 8 itoa.i4 fmt1uf4 1%09
fputs.s4 st'out09
1reak9
case /J/7
putc+ar./J/09
1reak9
<
<
-aCen'.argp09
<
13H -aClist argp9
T+is line 'eclares a -aria1le4 argp
23H -aCstart.argp4 fmt09
T+is line initialiEes argp an' initiates t+e processing of t+e argument list
33H -aCarg.0 fetc+es t+e ne6t argument from t+e argument list.
3H -aCen'.04 "+ic+ performs any necessary cleanup
333333333333333333333333333333333333333333333333333333333333333333333333
SSSSSSSSSSSS Call1ack routine SSSSSSSSSSSSSSSSSSSSS
GESL confidential
C Interview questions -3
2 call1ack routine is a routine .function or proce'ure0 in your program t+at
,in'o"s calls.
More generally4 a call1ack is a means of sen'ing a function as a parameter into
anot+er function.
,+en t+e call1ack function +as complete'4 control is passe' 1ack to t+e original
function.
33333333333333333333333333333333333333333333333
>n computer programming4 a call1ack is e6ecuta1le co'e t+at is passe' as an
argument
to ot+er co'e. >t allo"s a lo"er3le-el soft"are layer to call a function 'efine' in
a
+ig+er3le-el layer.Msually4 t+e +ig+er3le-el co'e starts 1y calling a function
"it+in
t+e lo"er3le-el co'e passing to it a pointer or +an'le to anot+er function. ,+ile
t+e
lo"er3le-el function e6ecutes4 it may call t+e passe'3in function any num1er of
times
to perform some su1task. >n anot+er scenario4 t+e lo"er3le-el function registers
t+e
passe'3in function as a +an'ler t+at is to 1e calle' async+ronously 1y t+e lo"er3
le-el
at a later time in reaction to somet+ing.
2 call1ack can 1e use' as a simpler alternati-e to polymorp+ism an' generic
programming4
in t+at t+e e6act 1e+a-ior of a function can 1e 'ynamically 'etermine' 1y passing
'ifferent .yet compati1le0 function pointers or +an'les to t+e lo"er3le-el
function.
T+is can 1e a -ery po"erful tec+ni5ue for co'e reuse.
Call1ack functions separate t+e caller from t+e callee4 t+e caller 'oes/nt care "+o
t+e callee is
3333333333333333333333333333333333333333333333333333
SSSSSSSSSS &eentrant co'e SSSSSSSSSSSSSSSSSSSS
2 programming routine t+at can 1e use' 1y multiple programs simultaneously.
>t is use' in operating systems an' ot+er system soft"are as "ell as in
multit+rea'ing4
"+ere concurrent e-ents are taking place. >t is "ritten so t+at none of its co'e
is mo'ifia1le .no -alues are c+ange'0 an' it 'oes not keep track of anyt+ing.
T+e calling programs keep track of t+eir o"n progress .-aria1les4 flags4 etc.04
t+us one copy of t+e reentrant routine can 1e s+are' 1y any num1er of users or
processes.
Conceptually4 it is as if se-eral people "ere eac+ 1aking a cake from a single copy
of a recipe on t+e "all. E-eryone looks at t+e master recipe4 1ut keeps track of
t+eir
o"n progress 1y ?otting 'o"n t+e step t+ey are at on t+eir o"n scratc+pa' so t+ey
can
pick up "+ere t+ey left off. T+e master recipe is ne-er 'istur1e'.
3333333333333333333333333333333333333333333333333
GESL confidential
C Interview questions --
2 computer program or routine is 'escri1e' as reentrant if it can 1e safely
e6ecute'
concurrently9 t+at is4 t+e routine can 1e re3entere' "+ile it is alrea'y running.
To 1e reentrant4 a function must +ol' no static non3constant 'ata4 must not return
t+e a''ress to static non3constant 'ata4 must "ork only on t+e 'ata pro-i'e' to it
1y t+e caller4 must not rely on locks to singleton resources4 an' must not call
non3reentrant functions.
Multiple le-els of /userXo1?ectXprocess priority/ an'Xor multiprocessing usually
complicate t+e control of reentrant co'e. 2lso4 >O co'e is usually not reentrant
1ecause it relies on s+are'4 singleton resources suc+ as 'isks.
3333333333333333333333333333333333333333333
>n t+e follo"ing piece of C co'e4 neit+er functions f nor g are reentrant.
int gC-ar 8 19
int f.0
:
gC-ar 8 gC-ar G 29
return gC-ar9
<
int g.0
:
return f.0 G 29
<
int main.0
:
g.09
return ;9
<
33333333333333333333333333333333333333333
T+ese slig+tly3altere' -ersions are reentrant7
int f.int i0
:
int pri- 8 i9
pri- 8 pri- G 29
return pri-9
<
int g.int i0
:
int pri- 8 i9
return f.pri-0 G 29
<
int main.0
:
g.109
return ;9
<
GESL confidential
C Interview questions -1
333333333333333333333333333333333333
&e3entrant co'e is co'e "ritten to safely allo" more t+an one t+rea' of control to
access it at once. T+is mainly occurs in multi3t+rea'e' situations4 suc+ as
in ser-er3si'e co'e t+at may 1e s+are' 1y multiple remote users.
To make co'e safely re3entrant takes a lot of care4 an' usually re5uires t+e
use of locking tec+ni5ues to pre-ent t+rea's mo'ifying local 'ata .e.g. -aria1les0
in use 1y anot+er t+rea'4 or t+e a-oi'ance
3333333333333333333333333333333333333333333333333333
2 reentrant function 'oes not +ol' static 'ata o-er successi-e calls4
nor 'oes it return a pointer to static 'ata. 2ll 'ata is pro-i'e' 1y t+e
caller of t+e function. 2 reentrant function must not call non3reentrant functions.
T+e use of glo1al 'ata is t+rea'3unsafe. >t s+oul' 1e maintaine' per t+rea' or
encapsulate'4 so t+at its access can 1e serialiEe'.

Many non3reentrant functions return a pointer to static 'ata.
T+is can 1e a-oi'e' in t"o "ays7
&eturning 'ynamically allocate' 'ata. >n t+is case4 it "ill 1e t+e caller/s
responsi1ility to free t+e storage. T+e 1enefit is t+at t+e interface 'oes not
nee' to 1e mo'ifie'. =o"e-er4 1ack"ar' compati1ility is not ensure'9
e6isting single3t+rea'e' programs using t+e mo'ifie' functions "it+out c+anges
"oul' not free t+e storage4 lea'ing to memory leaks.
Msing caller3pro-i'e' storage. T+is met+o' is recommen'e'4 alt+oug+ t+e interface
nee's to 1e mo'ifie'.
33333333333333333333333333333333333333333333333333333333333333333333333333333333333
SSSS gco- 3 co-erage testing tool SSSSSSSS
gco- is a test co-erage program. Mse it in concert "it+ TCC to analyEe
your programs to +elp create more efficient4 faster running co'e. Zou
can use gco- as a profiling tool to +elp 'isco-er "+ere your optimiEa3
tion efforts "ill 1est affect your co'e. Zou can also use gco- along
"it+ t+e ot+er profiling tool4 gprof4 to assess "+ic+ parts of your
co'e use t+e greatest amount of computing time.
#rofiling tools +elp you analyEe your co'e\s performance. Msing a pro3
filer suc+ as gco- or gprof4 you can fin' out some 1asic performance
statistics4 suc+ as7
]^ +o" often eac+ line of co'e e6ecutes
]^ "+at lines of co'e are actually e6ecute'
]^ +o" muc+ computing time eac+ section of co'e uses
Once you kno" t+ese t+ings a1out +o" your co'e "orks "+en compile'4 you
can look at eac+ mo'ule to see "+ic+ mo'ules s+oul' 1e optimiEe'. gco-
+elps you 'etermine "+ere to "ork on optimiEation.
Soft"are 'e-elopers also use co-erage testing in concert "it+ test3
suites4 to make sure soft"are is actually goo' enoug+ for a release.
GESL confidential
C Interview questions -5
Testsuites can -erify t+at a program "orks as e6pecte'9 a co-erage pro3
gram tests to see +o" muc+ of t+e program is e6ercise' 1y t+e test3
suite. )e-elopers can t+en 'etermine "+at kin's of test cases nee' to
1e a''e' to t+e testsuites to create 1ot+ 1etter testing an' a 1etter
final pro'uct.
Zou s+oul' compile your co'e "it+out optimiEation if you plan to use
gco- 1ecause t+e optimiEation4 1y com1ining some lines of co'e into one
function4 may not gi-e you as muc+ information as you nee' to look for
\+ot spots\ "+ere t+e co'e is using a great 'eal of computer time.
Like"ise4 1ecause gco- accumulates statistics 1y line .at t+e lo"est
resolution04 it "orks 1est "it+ a programming style t+at places only
one statement on eac+ line. >f you use complicate' macros t+at e6pan'
to loops or to ot+er control structures4 t+e statistics are less +elp3
ful333t+ey only report on t+e line "+ere t+e macro call appears. >f
your comple6 macros 1e+a-e like functions4 you can replace t+em "it+
inline functions to sol-e t+is pro1lem.
gco- creates a logfile calle' sourcefile.gco- "+ic+ in'icates +o" many
times eac+ line of a source file sourcefile.c +as e6ecute'. Zou can
use t+ese logfiles along "it+ gprof to ai' in fine3tuning t+e perfor3
mance of your programs. gprof gi-es timing information you can use
along "it+ t+e information you get from gco-.
gco- "orks only on co'e compile' "it+ TCC. >t is not compati1le "it+
any ot+er profiling or test co-erage mec+anism.
_ gcc 3fprofile3arcs 3ftest3co-erage tmp.c
_ a.out
_ gco- tmp.c
*(.$;J of * source lines e6ecute' in file tmp.c
Creating tmp.c.gco-.
_ -i tmp.c.gco-
333333333333333333333333333333333333333333333333333333333333333333333333
SSSSS [algrin' SSSSSSSSS
_-algrin' 3- 33tool8memc+eck 33leak3c+eck8full 33log3file8logfile.t6t .Xa.out
SSSSS =o" to take patc+ SSSS
_'iff 3Naur1! orgC'ir mo'C'ir H result.patc+
33333333333333333333333333333333333333333333333333333333333333333333
SSSSS Make SSSSSSSSSSSSSS
prog1 7 file1.o file2.o file3.o
CC 3o prog1 file1.o file2.o file3.o
file1.o 7 file1.cc my'efs.+
GESL confidential
C Interview questions -9
CC 3c file1.cc
file2.o 7 file2.cc my'efs.+
CC 3c file2.cc
file3.o 7 file3.cc
CC 3c file3.cc
clean 7
rm file1.o file2.o file3.o
33333333333333333333333333333333333333333333333333333333
O!`S 8 file1.o file2.o file3.o
prog1 7 _.O!`S0
CC 3o prog1 _.O!`S0
file1.o 7 file1.cc my'efs.+
CC 3c file1.cc
file2.o 7 file2.cc my'efs.+
CC 3c file2.cc
file3.o 7 file3.cc
CC 3c file3.cc
clean 7
rm _.O!`S0
333333333333333333333333333333333333333333
O!`S 8 file1.o file2.o file3.o
prog1 7 _:O!`S<
_:CLL< 3o _Y _:O!`S<
file1.o file2.o 7 my'efs.+
clean 7
rm _:O!`S<
3333333333333333333333333333333333333333333333333333333333
make 3H use t+e 'efault 'escriptor file4 1uil' t+e first target in t+e file
make myprog 3H use t+e 'efault 'escriptor file4 1uil' t+e target myprog
make 3f mymakefile 3H use t+e file mymakefile as t+e 'escriptor file4
1uil' t+e first target in t+e file
make 3f mymakefile 3H myprog use t+e file mymakefile as t+e 'escriptor file4
1uil' t+e target myprog
33333333333333333333333333333333333333333333333333333333333333333
L>!S 8 3lm
O!`S 8 file1.o file2.o _.moreCo1?s0
moreCo1?s 8 file3.o
CLL 8 CC
)E!MTCFL2T 8 S assign 3g for 'e1ugging
GESL confidential
C Interview questions -8
prog1 7 _:o1?s<
_:CLL< _.)E!MTCFL2T0 3o prog1 _:o1?s< _:L>!S<
33333333333333333333333333333333333333333333333333333333
.cc.o7
_.CLL0 _.CLLFL2TS0 3c _B
3333333333333333333333333333333333333
SS 2 simple makefile
CC 8 gcc
CFL2TS 8 3g 3>XusrXclassXcs1;(Xinclu'e
L)FL2TS 8 3LXusrXclassXcs1;(Xli1 3lgrap+
#&OT 8 program
=)&S 8 1inky.+ ak1ar.+ 'efs.+
S&CS 8 main.c 1inky.c ak1ar.c
SS T+is incantation says t+at t+e o1?ect files
SS +a-e t+e same name as t+e .c files4 1ut "it+ .o
O!`S 8 _.S&CS7.c8.o0
SS T+is is t+e first rule .t+e 'efault0
SS !uil' t+e program from t+e t+ree .o/s
_.#&OT0 7 _.O!`S0
_.CC0 _.L)FL2TS0 _.O!`S0 3o _.#&OT0
main.o 7 main.c 1inky.+ ak1ar.+ 'efs.+
1inky.o 7 1inky.c 1inky.+
ak1ar.o 7 ak1ar.c ak1ar.+ 'efs.+
clean 7
rm 3f core _.#&OT0 _.O!`S0
T2TS 7 _.S&CS0 _.=)&S0
etags 3t _.S&CS0 _.=)&S0
333333333333333333333333333333333333333333333333333333333333333333333333
.#=ONZ7clean
CFL2TS G8 3,all 3g 3o2
O!` 8 1.o 2.o 3.o
OMT#MT 8 result
_.OMT#MT0 7 _.O!`0
gcc 3o _Y _@
GESL confidential
C Interview questions -7
clean7
rm 3rf _.O!`0 _.OMT#MT0
333333333333333333333333333333333333333333333333333
SSSSSSS T)! SSSSSSSSSSSSSSSSSSSSS
_gcc 3g giri.c
_g'1 a.out
+elp 3H to 'isplay a list of g'1 comman's

+elp comman' 3H to get +elp on a specifie' g'1 comman'
run 3H to runXe6ecute t+e program starting from t+e 1eginning
continue 3H to resume runningXe6ecuting t+e program
ne6t 3H to e6ecute t+e current statement an' stop at t+e ne6t
statement
step 3H same as ne6t4 1ut step into a function
list 66 3H list source lines starting at line 66
list 3H to list t+e ne6t source lines
list 664yy 3H to list sources lines from line 66 to line yy
list filename766 3H to list source lines in t+e specifie' file starting at line 66
5uit 3H to 5uit g'1 an' re-ert to t+e uni6 comman' le-el
1reak functionname 3H to set a 1reakpoint at t+e start of a function
1reak filename766 3H to set a 1reakpoint at line 66 in t+e specifie' file
1reak 66 3H to set a 1reakpoint at line 66 in t+e current file
1reak 1 3H to set a 1reakpoint at t+e first line in t+e current file
.'eclaration or e6ecuta1le statement0
info 1reak 3H to list all 1reakpoints .inclu'ing t+ose 'isa1le'09
1reakpoints are num1ere' S14 S24 S34 etc.
'isa1le 66 3H to 'isa1le 1reakpoint S66
ena1le 66 3H to ena1le 1reakpoint S66
print -1 3H to print t+e -alue of a specifie' -aria1le
GESL confidential
C Interview questions 14
info source 3H to s+o" t+e name of t+e current source file
info sources 3H to list t+e name of all source files in use
set -aria1le 8 -alue 3H to assign a ne" -alue to a specifie' -aria1le
.return0 3H to re3e6ecute t+e pre-ious g'1 comman'9
t+is is particularly useful if t+e pre-ious g'1 comman' "as ne6t or
step
333333333333333333333333333333333333333333333333333333333333333333333333333333333
)ifference 1et"een Type'ef an' S'efine7
Type'ef
3333333333
1. +an'le' 1y c compliler an' is an actual 'efinition of a ne" type.
2.type'ef 'eclaration 'oes not create a ne" type in any sense9
it merely a''s a ne" name for some e6isting type.type'ef 'eclaration 'oes not
create a ne" type in any sense9
it merely a''s a ne" name for some e6isting type.
S'efine
333333333333
1. +an'le' 1y preprocessor an' "orks like replacement.

2. S'efine creates a ne" name in t+e te6t t+at "ill 1e processe'
into C source co'e4 "+ile type'ef creates a ne" name into
C source co'e itself.
For e6ample4
type'ef c+ar FStringCt9
S'efine StringC' c+ar F
StringCt s14 s29 StringC' s34 s9
s14 s24 an' s3 are all 'eclare' as c+ar F4
1ut s is 'eclare' as a c+ar4 "+ic+ is pro1a1ly not t+e intention.
33333333333333333333333333333333333333333333333333333333333333333333333333333333
C program Limits7
3 ;R$ e6ternal i'entifiers in one translation unit
a $11 i'entifiers "it+ 1lock scope 'eclare' in one 1lock
a ;R$ macro i'entifiers simultaneously 'efine' in one preprocessing translation
unit
a 12( parameters in one function 'efinition
GESL confidential
C Interview questions 11
a 12( arguments in one function call
a 12( parameters in one macro 'efinition
a 12( arguments in one macro in-ocation
a ;R$ c+aracters in a logical source line
a ;R$ c+aracters in a c+aracter string literal or "i'e string literal .after
concatenation0
a %$$3$ 1ytes in an o1?ect .in a +oste' en-ironment only0
a 1$ nesting le-els for Sinclu'e' files
a 1;23 case la1els for a s"itc+ statement .e6clu'ing t+ose for any neste' s"itc+
statements0
a 1;23 mem1ers in a single structure or union
a 1;23 enumeration constants in a single enumeration
a %3 le-els of neste' structure or union 'efinitions in a single struct3
'eclaration3list
333333333333333333333333333333333333333333333333333333333333333333333333333
)ifferece 1et"een ne" an' malloc7
malloc7
1. malloc re5uires a special ItypecastingI "+en it allocates memory.
2. free is t+e key"or' use' to free t+e memory "+ile using malloc.
3. malloc is a C function "+ic+ "ill allocate t+e amount of memory you ask an'
t+ats it.
. free is a C function "+ic+ "ill free up t+e memory allocate'.
$. malloc can fail4 an' returns a NMLL pointer if memory is e6+auste'.
ne"7
1. ne" 'oes not re5uires any typecasting.
2. 'elete t+e key"or' to free memory "+ile using ne".
3. ne" is a CGG operator "+ic+ "ill allocate memory 2N) call
t+e constructor of t+e class for "+ic+/s o1?ect memory is 1eing allocate'.
. 'elete is a CGG operator "+ic+ "ill free up t+e allocate' memory 2N) call
t+e 'estructor of t+e o1?ect.
$. Operator ne" ne-er returns a NMLL pointer4 1ut in'icates failure 1y t+ro"ing
an e6ception instea'.
33333333333333333333333333333333333333333333333333333333333333333333333333
)ifferece 1et"een mute6 an' semap+ore7
mute67
1. Mute6es are typically use' to serialise access to a section of re3entrant co'e
t+at cannot 1e e6ecute' concurrently 1y more t+an one t+rea'.
2. Mute6es are typically use' to serialise access to a section of re3entrant co'e
GESL confidential
C Interview questions 12
t+at cannot 1e e6ecute' concurrently 1y more t+an one t+rea'.
3. 2 mute6 is really a semap+ore "it+ -alue 1.
semap+ore7
1.
333333333333333333333333333333333333333333333333333333
)ifference 1et"een Static an' 'ynamic li1rary.
static7
1. Static li1raries are simply a collection of or'inary o1?ect files.
2. static li1raries en' "it+ t+e bb.a// suffi6.
3. Static li1raries permit users to link to programs "it+out +a-ing to recompile
its co'e4 sa-ing recompilation time.
. T+e program runs faster.
$. T+e program takes more memory.T+is is 1ecause linking to static li1raries
inclu'es
t+e actual co'e for t+e li1rary function.s0Xproce'ure.s0 "it+ t+e e6ecuta1le4
%. steps7
_ cc 3c a''.c su1.c mul.c
_ ar rcs myCli1rary.a a''.o su1.o mul.o
a static li1rary or statically3linke' li1rary is a set of routines4
e6ternal functions an' -aria1les "+ic+ are resol-e' in a caller at compile3time
an' copie' into a target application 1y a compiler4 linker4 or 1in'er4 pro'ucing
an o1?ect file an' a stan'3alone e6ecuta1le. T+is e6ecuta1le an' t+e process of
compiling it are 1ot+ kno"n as a static 1uil' of t+e program.
Static li1raries are eit+er merge' "it+ ot+er static li1raries an' o1?ect files
'uring 1uil'ingXlinking to form a single e6ecuta1le4 or t+ey may 1e loa'e' at run3
time
into t+e a''ress space of t+e loa'e' e6ecuta1le at a static memory offset
'etermine' at
compile3timeXlink3time.
s+are'7
1. S+are' li1raries are li1raries t+at are loa'e' 1y programs "+en t+ey start.
2.
1. steps7
_ cc 3F#>C 3c a''.c su1.c mul.c
_ cc 3s+are' 3o myli1.so a''.o su1.o mul.o
)ynamic linking means t+at t+e su1routines of a li1rary are loa'e' into an
application
program at runtime4 rat+er t+an 1eing linke' in at compile time4 an' remain as
separate
files on 'isk. Only a minimal amount of "ork is 'one at compile time 1y t+e linker9
it only recor's "+at li1rary routines t+e program nee's an' t+e in'e6 names or
num1ers
of t+e routines in t+e li1rary. T+e ma?ority of t+e "ork of linking is 'one at t+e
GESL confidential
C Interview questions 13
time
t+e application is loa'e' .loa'time0 or 'uring e6ecution .runtime0. T+e necessary
linking co'e4 calle' a loa'er4 is actually part of t+e un'erlying operating system.
2t t+e appropriate time t+e loa'er fin's t+e rele-ant li1raries on 'isk an' a''s
t+e
rele-ant 'ata from t+e li1raries to t+e process/s memory space.
Some operating systems can only link in a li1rary at loa'time4 1efore t+e process
starts e6ecuting9 ot+ers may 1e a1le to "ait until after t+e process +as starte' to
e6ecute an' link in t+e li1rary ?ust "+en it is actually reference' .i.e.4 'uring
runtime0.
T+e latter is often calle' I'elay loa'ingI. >n eit+er case4 suc+ a li1rary is
calle' a
'ynamically linke' li1rary.
'ynamic loa'e' li1raries7
1.)ynamically loa'e' .)L0 li1raries are li1raries t+at are loa'e' at times ot+er
t+an
'uring t+e startup of a program.
Sinclu'e B'lfcn.+H
-oi' F'lopen.const c+ar Ffilename4 int flag09
const c+ar F'lerror.-oi'09
-oi' F'lsym.-oi' F+an'le4 c+ar Fsym1ol09
int 'lclose.-oi' F+an'le09
'lopen loa's a 'ynamic li1rary from t+e file name' 1y t+e null termi3
nate' string filename an' returns an opa5ue I+an'leI for t+e 'ynamic
li1rary.
flag must 1e eit+er &TL)CL2WZ4 meaning resol-e un'efine' sym1ols as
co'e from t+e 'ynamic li1rary is e6ecute'4 or &TL)CNO,4 meaning resol-e
all un'efine' sym1ols 1efore 'lopen returns4 an' fail if t+is cannot 1e
'one. Optionally4 &TL)CTLO!2L may 1e or\e' "it+ flag4 in "+ic+ case
t+e e6ternal sym1ols 'efine' in t+e li1rary "ill 1e ma'e a-aila1le to
su1se5uently loa'e' li1raries.
'lsym takes a I+an'leI of a 'ynamic li1rary returne' 1y 'lopen an' t+e
null terminate' sym1ol name4 returning t+e a''ress "+ere t+at sym1ol is
loa'e'. >f t+e sym1ol is not foun'4 'lsym returns NMLL9 +o"e-er4 t+e
correct "ay to test for an error from 'lsym is to sa-e t+e result of
'lerror into a -aria1le4 an' t+en c+eck if sa-e' -alue is not NMLL.
T+is is 1ecause t+e -alue of t+e sym1ol coul' actually 1e NMLL. >t is
also necessary to sa-e t+e results of 'lerror into a -aria1le 1ecause
if 'lerror is calle' again4 it "ill return NMLL.
'lclose 'ecrements t+e reference count on t+e 'ynamic li1rary +an'le
+an'le. >f t+e reference count 'rops to Eero an' no ot+er loa'e'
li1raries use sym1ols in it4 t+en t+e 'ynamic li1rary is unloa'e'.
EL2M#LE
GESL confidential
C Interview questions 1-
Loa' t+e mat+ li1rary4 an' print t+e cosine of 2.;7
Sinclu'e Bst'io.+H
Sinclu'e B'lfcn.+H
int main.int argc4 c+ar FFarg-0 :
-oi' F+an'le9
'ou1le .Fcosine0.'ou1le09
c+ar Ferror9
+an'le 8 'lopen .Ili1m.soI4 &TL)CL2WZ09
if .O+an'le0 :
fprintf .st'err4 IJsKnI4 'lerror.009
e6it.109
<
cosine 8 'lsym.+an'le4 IcosI09
if ..error 8 'lerror.00 O8 NMLL0 :
fprintf .st'err4 IJsKnI4 error09
e6it.109
<
printf .IJfKnI4 .Fcosine0.2.;009
'lclose.+an'le09
return ;9
<
gcc 3r'ynamic 3o foo foo.c 3l'l
33333333333333333333333333333333333333333333333333
)ifference 1et"een a ct+rea'd an' a cprocessd
2 process is a collection of -irtual memory space4 co'e4 'ata4 an' system
resources.
2 t+rea' is co'e t+at is to 1e serially e6ecute' "it+in a process. 2 processor
e6ecutes t+rea's4 not processes4 so eac+ application +as at least one process4 an'
a
process al"ays +as at least one t+rea' of e6ecution4 kno"n as t+e primary t+rea'.
2 process can +a-e multiple t+rea's in a''ition to t+e primary t+rea'. #rior to t+e
intro'uction of multiple t+rea's of e6ecution4 applications "ere all 'esigne' to
run
on a single t+rea' of e6ecution.
,+en a t+rea' 1egins to e6ecute4 it continues until it is kille' or until it is
interrupte' 1y a t+rea' "it+ +ig+er priority .1y a user action or t+e kernelds
t+rea'
sc+e'uler0. Eac+ t+rea' can run separate sections of co'e4 or multiple t+rea's can
e6ecute t+e same section of co'e. T+rea's e6ecuting t+e same 1lock of co'e maintain
separate stacks. Eac+ t+rea' in a process s+ares t+at processds glo1al -aria1les
an'
resources.
2 t+rea' is a semi3process4 t+at +as its o"n stack4 an' e6ecutes a gi-en piece of
co'e.
GESL confidential
C Interview questions 11
Mnlike a real process4 t+e t+rea' normally s+ares its memory "it+ ot+er t+rea's
."+ere as for processes "e usually +a-e a 'ifferent memory area for eac+ one of
t+em0.
2 T+rea' Troup is a set of t+rea's all e6ecuting insi'e t+e same process.
T+ey all s+are t+e same memory4 an' t+us can access t+e same glo1al -aria1les4
same +eap memory4 same set of file 'escriptors4 etc. 2ll t+ese t+rea's e6ecute
in parallel .i.e. using time slices4 or if t+e system +as se-eral processors4 t+en
really in parallel0.
T+e a'-antage of using a t+rea' group o-er using a process group is t+at conte6t
s"itc+ing 1et"een t+rea's is muc+ faster t+an conte6t s"itc+ing 1et"een processes.
communications 1et"een t"o t+rea's is usually faster an' easier to implement t+en
communications 1et"een t"o processes.!ecause t+rea's in a group all use t+e same
memory
space4 if one of t+em corrupts t+e contents of its memory4 ot+er t+rea's mig+t
suffer as
"ell. ,it+ processes4 t+e operating system normally protects processes from one
anot+er4
an' t+us if one corrupts its o"n memory space4 ot+er processes "on/t suffer.
3333333333333333333333333333333333333333333333333333333333333333333333333333333
Linu6 !ooting proce'ure7
T+e moment after a computer is po"ere' on4 it is practically useless 1ecause t+e
&2M c+ips
contain ran'om 'ata an' no operating system is running. To 1egin t+e 1oot4 a
special
+ar'"are circuit raises t+e logical -alue of t+e &ESET pin of t+e C#M. 2fter &ESET
is t+us
asserte'4 some registers of t+e processor .inclu'ing cs an' eip0 are set to fi6e'
-alues4 an'
t+e co'e foun' at p+ysical a''ress ;6fffffff; is e6ecute'. T+is a''ress is mappe'
1y t+e
+ar'"are to some rea'3only4 persistent memory c+ip4 a kin' of memory often calle'
&OM
.&ea'3Only Memory0. T+e set of programs store' in &OM is tra'itionally calle' !>OS
.!asic
>nputXOutput System04 since it inclu'es se-eral interrupt3'ri-en lo"3le-el
proce'ures use' 1y
some operating systems4 inclu'ing Microsoft/s MS3)OS4 to +an'le t+e +ar'"are
'e-ices t+at
make up t+e computer.
Once initialiEe'4 Linu6 'oes not make any use of !>OS 1ut pro-i'es its o"n 'e-ice
'ri-er for
e-ery +ar'"are 'e-ice on t+e computer. >n fact4 t+e !>OS proce'ures must 1e
e6ecute' in real
mo'e4 "+ile t+e kernel e6ecutes in protecte' mo'e.
!>OS uses &eal Mo'e a''resses 1ecause t+ey are t+e only ones a-aila1le "+en t+e
computer
is turne' on. 2 &eal Mo'e a''ress is compose' of a seg segment an' an off offset9
t+e
GESL confidential
C Interview questions 15
correspon'ing p+ysical a''ress is gi-en 1y seg F1%Goff.
Linu6 is force' to use !>OS in t+e 1ootstrapping p+ase4 "+en it must retrie-e t+e
kernel
image from 'isk or from some ot+er e6ternal 'e-ice.
T+e !>OS 1ootstrap proce'ure
essentially performs t+e follo"ing four operations7
1. E6ecutes a series of tests on t+e computer +ar'"are4 in or'er to esta1lis+ "+ic+
'e-ices are present an' "+et+er t+ey are "orking properly. T+is p+ase is often
calle'
#OST .#o"er3On Self3Test0. )uring t+is p+ase4 se-eral messages4 suc+ as t+e !>OS
-ersion 1anner4 are 'isplaye'.
2. >nitialiEes t+e +ar'"are 'e-ices. T+is p+ase is crucial in mo'ern #C>31ase'
arc+itectures4 since it guarantees t+at all +ar'"are 'e-ices operate "it+out
conflicts on
t+e >&e lines an' >XO ports. 2t t+e en' of t+is p+ase4 a ta1le of installe' #C>
'e-ices is
'isplaye'.
3. Searc+es for an operating system to 1oot. 2ctually4 'epen'ing on t+e !>OS
setting4 t+e
proce'ure may try to access .in a pre'efine'4 customiEa1le or'er0 t+e first sector
.1oot
sector0 of any floppy 'isk4 any +ar' 'isk4 an' any C)3&OM in t+e system.
. 2s soon as a -ali' 'e-ice is foun'4 copies t+e contents of its first sector into
&2M4
starting from p+ysical a''ress ;6;;;;(c;;4 t+en ?umps into t+at a''ress an'
e6ecutes
t+e co'e ?ust loa'e'.
!oot Loa'er7
T+e 1oot loa'er is t+e program in-oke' 1y t+e !>OS to loa' t+e image of an
operating system
kernel into &2M.
>n or'er to 1oot from a floppy 'isk4 t+e instructions store' in its first sector
are loa'e' in
&2M an' e6ecute'9 t+ese instructions copy all t+e remaining sectors containing t+e
kernel
image into &2M.
!ooting from a +ar' 'isk is 'one 'ifferently. T+e first sector of t+e +ar' 'isk4
name' t+e
Master !oot &ecor' .M!&04 inclu'es t+e partition ta1lePEac+ partition ta1le entry
typically inclu'es
t+e starting an' en'ing sectors of a partition an' t+e kin' of operating system
t+at +an'les it.Q
an' a small program4 "+ic+ loa's t+e first sector of t+e partition containing t+e
operating
GESL confidential
C Interview questions 19
system to 1e starte'. Some operating systems suc+ as Microsoft ,in'o"s R* i'entify
t+is partition 1y
means of an acti-e flag inclu'e' in t+e partition ta1le9P!Q follo"ing t+is
approac+4 only t+e operating system "+ose
kernel image is store' in t+e acti-e partition can 1e 1oote'. 2s "e s+all see
later4 Linu6 is
more fle6i1le since it replaces t+e ru'imentary program inclu'e' in t+e M!& "it+ a
sop+isticate' program calle' L>LO t+at allo"s users to select t+e operating system
to 1e
1oote'.
!ooting Linu6 from Floppy )isk7
T+e only "ay to store a Linu6 kernel on a single floppy 'isk is to compress t+e
kernel image.
2s "e s+all see4 compression is 'one at compile time an' 'ecompression 1y t+e
loa'er.
>f t+e Linu6 kernel is loa'e' from a floppy 'isk4 t+e 1oot loa'er is 5uite simple.
>t is co'e' in
t+e arc+Xi3*%X1ootX1ootsect.S assem1ly language file. ,+en a ne" kernel image is
pro'uce'
1y compiling t+e kernel source4 t+e e6ecuta1le co'e yiel'e' 1y t+is assem1ly
language file is
place' at t+e 1eginning of t+e kernel image file. T+us4 it is -ery easy to pro'uce
a 1oota1le
floppy containing t+e Linu6 kernel. T+e floppy can 1e create' 1y copying t+e kernel
image
starting from t+e first sector of t+e 'isk. ,+en t+e !>OS loa's t+e first sector of
t+e floppy
'isk4 it actually copies t+e co'e of t+e 1oot loa'er.
T+e 1oot loa'er4 "+ic+ is in-oke' 1y t+e !>OS 1y ?umping to p+ysical a''ress
;6;;;;(c;;4
performs t+e follo"ing operations7
1. Mo-es itself from a''ress ;6;;;;(c;; to a''ress ;6;;;R;;;;.
2. Sets up t+e &eal Mo'e stack4 from a''ress ;6;;;;3ff. 2s usual4 t+e stack "ill
gro"
to"ar' lo"er a''resses.
3. Sets up t+e 'isk parameter ta1le4 use' 1y t+e !>OS to +an'le t+e floppy 'e-ice
'ri-er.
. >n-okes a !>OS proce'ure to 'isplay a ILoa'ingI message.
$. >n-okes a !>OS proce'ure to loa' t+e setup. 0 co'e of t+e kernel image from t+e
floppy 'isk an' puts it in &2M starting from a''ress ;6;;;R;2;;.
%. >n-okes a !>OS proce'ure to loa' t+e rest of t+e kernel image from t+e floppy
'isk
an' puts t+e image in &2M starting from eit+er lo" a''ress ;6;;;1;;;; .for small
kernel images compile' "it+ make E>mage0 or +ig+ a''ress ;6;;1;;;;; .for 1ig
kernel images compile' "it+ make 1E>mage0. >n t+e follo"ing 'iscussion4 "e "ill say
t+at t+e kernel image is Iloa'e' lo"I or Iloa'e' +ig+I in &2M4 respecti-ely.
Support
for 1ig kernel images "as intro'uce' 5uite recently7 "+ile it uses essentially t+e
same
1ooting sc+eme as t+e ol'er one4 it places 'ata in 'ifferent p+ysical memory
GESL confidential
C Interview questions 18
a''resses
to a-oi' pro1lems "it+ t+e >S2 +ole mentione' in Section 2.$.3 in C+apter 2.
(. `umps to t+e setup. 0 co'e.
!ooting Linu6 from =ar' )isk7
>n most cases4 t+e Linu6 kernel is loa'e' from a +ar' 'isk4 an' a t"o3stage 1oot
loa'er is
re5uire'. T+e most commonly use' Linu6 1oot loa'er on >ntel systems is name' L>LO
.L>nu6
LOa'er09 correspon'ing programs e6ist for ot+er arc+itectures. L>LO may 1e
installe' eit+er
on t+e M!&4 replacing t+e small program t+at loa's t+e 1oot sector of t+e acti-e
partition4 or
in t+e 1oot sector of a .usually acti-e0 'isk partition. >n 1ot+ cases4 t+e final
result is t+e same7
"+en t+e loa'er is e6ecute' at 1oot time4 t+e user may c+oose "+ic+ operating
system to loa'.
T+e L>LO 1oot loa'er is 1roken into t"o parts4 since ot+er"ise it "oul' 1e too
large to fit into
t+e M!&. T+e M!& or t+e partition 1oot sector inclu'es a small 1oot loa'er4 "+ic+
is loa'e'
into &2M starting from a''ress ;6;;;;(c;; 1y t+e !>OS. T+is small program mo-es
itself to
t+e a''ress ;6;;;Ra;;;4 sets up t+e &eal Mo'e stack .ranging from ;6;;;R1;;; to
;6;;;Ra2;;04 an' loa's t+e secon' part of t+e L>LO 1oot loa'er into &2M starting
from
a''ress ;6;;;R1;;;. >n turn4 t+is latter program rea's a map of a-aila1le operating
systems
from 'isk an' offers t+e user a prompt so s+e can c+oose one of t+em. Finally4
after t+e user
+as c+osen t+e kernel to 1e loa'e' .or let a time3out elapse so t+at L>LO c+ooses a
'efault04
t+e 1oot loa'er may eit+er copy t+e 1oot sector of t+e correspon'ing partition into
&2M an'
e6ecute it or 'irectly copy t+e kernel image into &2M.
2ssuming t+at a Linu6 kernel image must 1e 1oote'4 t+e L>LO 1oot loa'er4 "+ic+
relies on
!>OS routines4 performs essentially t+e same operations as t+e 1oot loa'er
integrate' into t+e
kernel image 'escri1e' in t+e pre-ious section a1out floppy 'isks. T+e loa'er
'isplays t+e
ILoa'ing Linu6I message9 t+en it copies t+e integrate' 1oot loa'er of t+e kernel
image to
a''ress ;6;;;R;;;;4 t+e setup. 0 co'e to a''ress ;6;;;R;2;;4 an' t+e rest of t+e
kernel
image to a''ress ;6;;;1;;;; or ;6;;1;;;;;. T+en it ?umps to t+e setup. 0 co'e.
T+e setup. 0 Function7
T+e co'e of t+e setup. 0 assem1ly language function is place' 1y t+e linker
imme'iately
GESL confidential
C Interview questions 17
after t+e integrate' 1oot loa'er of t+e kernel4 t+at is4 at offset ;62;; of t+e
kernel image file.
T+e 1oot loa'er can t+us easily locate t+e co'e an' copy it into &2M starting from
p+ysical
a''ress ;6;;;R;2;;.
T+e setup. 0 function must initialiEe t+e +ar'"are 'e-ices in t+e computer an' set
up t+e
en-ironment for t+e e6ecution of t+e kernel program. 2lt+oug+ t+e !>OS alrea'y
initialiEe'
most +ar'"are 'e-ices4 Linu6 'oes not rely on it 1ut reinitialiEes t+e 'e-ices in
its o"n
manner to en+ance porta1ility an' ro1ustness. setup. 0 essentially performs t+e
follo"ing
operations7
1. >n-okes a !>OS proce'ure to fin' out t+e amount of &2M a-aila1le in t+e system.
2. Sets t+e key1oar' repeat 'elay an' rate. .,+en t+e user keeps a key presse' past
a
certain amount of time4 t+e key1oar' 'e-ice sen's t+e correspon'ing keyco'e o-er
an' o-er to t+e C#M.0
3. >nitialiEes t+e -i'eo a'apter car'.
. &einitialiEes t+e 'isk controller an' 'etermines t+e +ar' 'isk parameters.
$. C+ecks for an >!M Micro C+annel 1us .MC20.
%. C+ecks for a #SX2 pointing 'e-ice .1us mouse0.
(. C+ecks for 2'-ance' #o"er Management .2#M0 !>OS support.
*. >f t+e kernel image "as loa'e' lo" in &2M .at p+ysical a''ress ;6;;;1;;;;04
mo-es
it to p+ysical a''ress ;6;;;;1;;;. Con-ersely4 if t+e kernel image "as loa'e' +ig+
in
&2M4 'oes not mo-e it. T+is step is necessary 1ecause4 in or'er to 1e a1le to store
t+e
kernel image on a floppy 'isk an' to sa-e time "+ile 1ooting4 t+e kernel image
store'
on 'isk is compresse'4 an' t+e 'ecompression routine nee's some free space to use
as
a temporary 1uffer follo"ing t+e kernel image in &2M.
R. Sets up a pro-isional >nterrupt )escriptor Ta1le .>)T0 an' a pro-isional Tlo1al
)escriptor Ta1le .T)T0.
1;. &esets t+e floating point unit .F#M04 if any.
11. &eprograms t+e #rogramma1le >nterrupt Controller .#>C0 an' maps t+e 1% +ar'"are
interrupts .>&e lines0 to t+e range of -ectors from 32 to (. T+e kernel must
perform
t+is step 1ecause t+e !>OS erroneously maps t+e +ar'"are interrupts in t+e range
from
to 1$4 "+ic+ is alrea'y use' for C#M e6ceptions .
12. S"itc+es t+e C#M from &eal Mo'e to #rotecte' Mo'e 1y setting t+e #E 1it in t+e
cr;
status register. T+e pro-isional kernel page
ta1les containe' in s"apperCpgC'ir an' pg; i'entically map t+e linear a''resses to
t+e same p+ysical a''resses. T+erefore4 t+e transition from &eal Mo'e to #rotecte'
Mo'e goes smoot+ly.
13. `umps to t+e startupC32. 0 assem1ly language function.
T+e startupC32. 0 Functions7
GESL confidential
C Interview questions 54
T+ere are t"o 'ifferent startupC32. 0 functions9 t+e one "e refer to +ere is co'e'
in t+e
arc+Xi3*%X1ootXcompresse'X+ea'.S file. 2fter setup. 0 terminates4 t+e function +as
1een
mo-e' eit+er to p+ysical a''ress ;6;;1;;;;; or to p+ysical a''ress ;6;;;;1;;;4
'epen'ing
on "+et+er t+e kernel image "as loa'e' +ig+ or lo" in &2M.
T+is function performs t+e follo"ing operations7
1. >nitialiEes t+e segmentation registers an' a pro-isional stack.
2. Fills t+e area of uninitialiEe' 'ata of t+e kernel i'entifie' 1y t+e Ce'ata an'
Cen'
sym1ols "it+ Eeros .see Section 2.$.3 in C+apter 20.
3. >n-okes t+e 'ecompressCkernel. 0 function to 'ecompress t+e kernel image. T+e
IMncompressing Linu6 . . . I message is 'isplaye' first. 2fter t+e kernel image +as
1een 'ecompresse'4 t+e IO U4 1ooting t+e kernel.I message is s+o"n. >f t+e kernel
image "as loa'e' lo"4 t+e 'ecompresse' kernel is place' at p+ysical a''ress
;6;;1;;;;;. Ot+er"ise4 if t+e kernel image "as loa'e' +ig+4 t+e 'ecompresse' kernel
is place' in a temporary 1uffer locate' after t+e compresse' image. T+e
'ecompresse'
image is t+en mo-e' into its final position4 "+ic+ starts at p+ysical a''ress
;6;;1;;;;;.
. `umps to p+ysical a''ress ;6;;1;;;;;.
T+e 'ecompresse' kernel image 1egins "it+ anot+er startupC32. 0 function inclu'e'
in t+e
arc+Xi3*%XkernelX+ea'.S file. Msing t+e same name for 1ot+ t+e functions 'oes not
create any
pro1lems .1esi'es confusing our rea'ers04 since 1ot+ functions are e6ecute' 1y
?umping to
t+eir initial p+ysical a''resses.
T+e secon' startupC32. 0 function essentially sets up t+e e6ecution en-ironment for
t+e
first Linu6 process .process ;0. T+e function performs t+e follo"ing operations7
1. >nitialiEes t+e segmentation registers "it+ t+eir final -alues.
2. Sets up t+e Uernel Mo'e stack for process .
3. >n-okes setupCi't. 0 to fill t+e >)T "it+ null interrupt +an'lers .
. #uts t+e system parameters o1taine' from t+e !>OS an' t+e parameters passe' to
t+e
operating system into t+e first page frame .
$. >'entifies t+e mo'el of t+e processor.
%. Loa's t+e g'tr an' i'tr registers "it+ t+e a''resses of t+e T)T an' >)T ta1les.
(. `umps to t+e startCkernel. 0 function.
T+e startCkernel. 0 Function7
T+e startCkernel. 0 function completes t+e initialiEation of t+e Linu6 kernel.
Nearly e-ery
kernel component is initialiEe' 1y t+is function9 "e mention ?ust a fe" of t+em7
f T+e page ta1les are initialiEe' 1y in-oking t+e pagingCinit. 0 function.
f T+e page 'escriptors are initialiEe' 1y t+e memCinit. 0 function .
f T+e final initialiEation of t+e >)T is performe' 1y in-oking trapCinit. 0 an'
initC>&e. 0.
GESL confidential
C Interview questions 51
f T+e sla1 allocator is initialiEe' 1y t+e kmemCcac+eCinit. 0 an'
kmemCcac+eCsiEesCinit. 0 functions .
f T+e system 'ate an' time are initialiEe' 1y t+e timeCinit. 0 function.
f T+e kernel t+rea' for process 1 is create' 1y in-oking t+e kernelCt+rea'. 0
function. >n turn4 t+is kernel t+rea' creates t+e ot+er kernel t+rea's an' e6ecutes
t+e
Xs1inXinit program.
!esi'es t+e ILinu6 -ersion 2.2.1 . . . I message4 "+ic+ is 'isplaye' rig+t after
t+e 1eginning
of startCkernel. 04 many ot+er messages are 'isplaye' in t+is last p+ase 1ot+ 1y
t+e init
functions an' 1y t+e kernel t+rea's. 2t t+e en'4 t+e familiar login prompt appears
on t+e
console .or in t+e grap+ical screen if t+e L ,in'o" System is launc+e' at startup04
telling t+e
user t+at t+e Linu6 kernel is up an' running.
GESL confidential

You might also like