0% found this document useful (0 votes)
3K views76 pages

C Programming Quiz: Questions & Answers

The document contains a mid-semester examination with multiple choice questions related to C programming. The questions cover topics like switch statements, loops, operators, functions, arrays and more. The correct answers are provided at the end for reference.

Uploaded by

Raj Sneha
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3K views76 pages

C Programming Quiz: Questions & Answers

The document contains a mid-semester examination with multiple choice questions related to C programming. The questions cover topics like switch statements, loops, operators, functions, arrays and more. The correct answers are provided at the end for reference.

Uploaded by

Raj Sneha
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 76

Mid-Semester Examination

1. main()
{
int i =1;
switch(i)
{
i++;
case 1 : printf ("");
break;
case 2 : printf("");
break;
default: printf("");
break;
}
}
hich case will be e!ecuted here"
a. #$nta! err%r b. &ase 1 c. &ase 2 d. default case
'n$ statement which is n%t part %f an$ case is called unreachable statement and it will
ne(er )ets e!ecuted. #uch statements will be i)n%red b$ the c%mpiler and n% err%r %r
warnin) )ets )enerated.
'*#: b
2. main( )
{
unsi)ned int i=+;
while( i ,=-)
printf( ".d"/ i00);
}
h%w man$ times will the printf statement be e!ecuted"
a.- b.+ c.1 d. infinite
's 2i2 is declared as unsi)ned inte)er it ne(er )ets decremented bel%w 2-2 and the l%%p
ne(er terminates.
'*#: d/ infinite.
+. main( )
{
int !/$/ 3;
!=2;
$=4;
3= !+++$;
printf(".d .d .d"/ !/ $ 3);
}
5utput %f the pr%)ram will be :
a) + 4 6 b) 2 7 8 c) &%mpiler 9rr%r d) :untime 9rr%r
3 = !+++$; )ets e!panded as 3 = !++ + $; n%w 3 will be 6 and then ! )ets incremented t% +.
'*#: a
1. hich %f the f%ll%win) can be used t% print . t% the screen"
a) printf(";."); b)printf(";;."); c)printf(".."); d) <%th a) and c)
.. can be used specificall$. <ut ;. w%rks in m%st cases with )cc (n%t alwa$s).
'*#: c
4. =n &/ !0=$+1 h%w will u represent it..
a) !=!0$01 b) ! = 0!0$01 c) !=0!+$+1 d) ! = !0$+1
>his sh%rt hand %perat%r ( 0= ) will be e!panded as f%ll%ws ! = ! 0 ($+1) which is e?ui(alent
t% ! = !0$01
'*#: a
7. printf(".u"/01)
hat is the (alue printed here"
a) 01 b) 1 c) (@'A %f unsi)ned int) 0 1 d) @'A %f unsi)ned int
01 is internall$ represented in binar$ f%rmat as 'll 212 (22s c%mplement n%tati%n). hen all
212 is c%n(erted int% unsi)ned inte)er will )i(e us ma! (alue %f unsi)ned inte)er.
'*#: d
6. main()
{
printf("he
ll%");
}
hat is the e!pected %utc%me %f this pr%)ram"
a) he b) hell% c) ll% d) c%mpilati%n err%r
ll%
#$nta! err%r. e cann%t break a strin) acr%ss sentences. =f $%u want t% break it then d% it
this wa$/
printf("he ;
ll%");
'*#: d
8. main()
{
int i=01/ B=01/ k=-/ l=2/ m;
m = i++ CC B++ CC k++ DD l++;
printf(".d .d .d .d .d .d;n"/ i/ B/ k/ l/ m/ E(E-FF1));
}
5utput %f the pr%)ram will be:
'. - - 1 + 1 - <. - - 1 2 1 1 &. - - 1 2 - - G. - - 1 + 1 14
:elati%nal %perat%rs )et e(aluated fr%m left t% ri)ht. >his e!pressi%n c%ntinues t% )et
e(aluated full$ as (alue %f the e!pressi%n cann%t be determined an$ where in the middle.
E is bit wise n%t which in(erts all bits %f a number passed as ar)ument. "ar)1 FF ar)2" shifts
ar)2 number %f bits %f number ar)2 t% left/ ar)2 3er%es are added t% number %n ri)ht.
*%w E(E- FF 1) is 14.
'*#: G
H. main() {
int i=2-;
unsi)ned int B=01;
printf(".d/ .d;n"/ si3e%f(B)0si3e%f(i)/ B0i);
}
5utput %f the pr%)ram will be:
'. -/ 021 <. 2/ 74414 &. -/ 74414 G. 2/ 021
hen an inte)er is addedIsubtractedImultipliedIdi(ided with an unsi)ned inte)er then
unsi)ned inte)er will be c%n(erted t% inte)er and the %perati%n is perf%rmed. #i)ned and
unsi)ned are %f same si3e in b$tes. :emember that si3e%f %perat%r will return the si3e in
b$tes re?uired t% st%re the %bBect passed as ar)ument/ here inte)er and unsi)ned inte)er.
'*#: a) -/ 021
1-. main() {
int i = 1-;
i = fun(i);
printf(".d/ .d;n"/ i/ 01FF1);
}
fun(int i) {
return ++i;
}
'. 1-/ 017 <. 1-/ 01 &. &%mpiler 9rr%r G. 11/ 017
fun(i) takes a inte)er and returns a (alue %ne )reater than i. #%/ i will be 11 after the
functi%n call. 01 FF 1 will be 017 (recall fr%m ?uesti%n 8).
'*#: d
11. main() {
int c=0 02;
printf("c = .d;n"/ c);
}
'. l(alue 9rr%r <. r(alue 9rr%r &. Jarba)e (alue G. c = 2 9. c = 1
Answer: D
c=2;
Explanation:
Kere unar$ minus (%r ne)ati%n) %perat%r is used twice. #ame maths rules applies/ ie. minus L
minus= plus.
Note: K%we(er $%u cann%t )i(e like 002. <ecause 00 %perat%r can %nl$ be applied t% (ariables
as a decrement %perat%r (e). i00). 2 is a c%nstant and n%t a (ariable.
12. main() {
printf("ab");
printf(";bsi;;");
printf(";rha");
printf(";n");
}
5utput %f the pr%)ram will be :
'. asi;ha <. hai;; &. hai; G. hai
Mrints hai;
reas%n: ;; 0 prints a ;
;r m%(es curs%r t% the be))in) %f the line (the same line).
>hen the ne!t printed characters will replace the pre(i%usl$ printed characters.
'*#: c
1+. main() {
int i=4;
printf(".d .d .d .d .d;n"/ i++/ i00/ ++i/ 00i/ i);
}
5utput %f the pr%)ram will be :
'. 4 4 4 1 4 <. 1 4 4 1 4 &. 4 4 4 4 4 G. 4 4 7 4 4
=n Nuncti%n ar)uments )et e(aluated fr%m ri)ht t% left. #tart d%in) it fr%m ri)ht and fi)ure
%ut.
'*#: <
11. main() {
int a/ b=+/ c=2;
a = b++Lc;
c = ++bLa;
printf(".d/ .d/ .d;n"/ a/ b/ c);
}
5utput %f the pr%)ram will be:
'. 8/ 1/ 21 <. 8/ 4/ +2 &. 7/ 4/ +- G. 7/ 1/ 21
Oust n%te difference between b++ and ++b. if an e!pressi%n c%ntains b++/ then in e!pressi%n
e(aluati%n %f (alue %f b is used and after e!pressi%n e(aluati%n b )ets incremented. where
as in case %f ++b/ b )ets incremented first and then used in the e!pressi%n e(aluati%n.
'*#: &
14. main() {
int i/ B;
f%r(i=-/ B=-; iF4/ BF24 ;i++/ B++);
printf("i=.d/ B=.d;n"/ i/ B);
}
5utput %f the pr%)ram will be:
'. i=1/ B=21 <. i=21/ B=21 &. i=24/ B=24 G. i=4/ B=24
:ecall that 2/2(c%mma %perat%r) will return the last e(aluated e!pressi%n (alue. #% e(er$
time the l%%p c%nditi%n )ets e(aluated we will )et the (alue %f B F 24 as c%nditi%n. hich
makes the l%%p run fr%m 24 iterati%ns" N%r each iterati%n i/ B are incremented in increment
secti%n %f f%r l%%p. hich makes i = 24/ B = 24
'*#: &
17. main() {
int i=12;
f%r(printf(".d "/ i); printf(".d "/ 00i)02; printf(".d "/ i)) {
printf(".d "/ 00i);
}
}
'. 5utput is "12 11 1- 1- H" <. &%mpiler 9rr%r
&. 5utput is "12 11 1- 1- H " G. 5utput is "12 11 11 1- H "
printf fucti%ns returns number %f characters printed %n the screen. #ee the l%%p as n%rmal
l%%p/ instead %f i %r B we ha(e a printf statement. >he c%nditi%n will be false when i bec%mes
H. >ill then "12 "(fr%m initiali3ati%n)/ "11 " (fr%m first e(aluati%n %f c%nditi%n)/ "11 " (fr%m
increment)/ "1- " (fr%m 2nd e(aluati%n %f c%nditi%n)/ "1- " (fr%m increment secti%n)/ "H "
(fr%m c%nditi%n). *5 c%nditi%n bec%mes false and l%%p e!it.
'*#: &
16. main()
{
int !/$=2/3/a;
if(!=$.2) 3=2;
a=2;
printf(".d .d "/3/!);
}
'. 5utput is "2 -" <. 'fter printf ! is - and 3 has )arba)e (alue.
&. 'fter printf 3 is 2 and ! has )arba)e (alue. G. &%mpiler 9rr%r
! = $.2 will return the (alue assi)ned t% !/ which is 3er%. #% if b%d$ is n%t entered. 3 left
uninitiali3ed.
'*#: <
18. main()
{
int i=4/B=6;
B = B DD i++ CC printf("P5Q &'*");
printf(".d .d"/ i/ B);
}
5utput will be
'. P5Q &'*4 1 <. 4 1 &. 4 6 G. P5Q &'*4 6
B DD i++ CC printf("P5Q &'*"). =n this e!pressi%n after seein) B the (alue %f e!pressi%n is
kn%wn t% be true. as B is true. #% >rue (1) )ets assi)ned t% B.
'*#: <
1H. main() {
int i;
i = (2++/ 1,+/ 1);
printf("i = .d;n"/ i);
}
5utput will be
'. i = 4 <. i = 1 &. i = 1 G. &%mpiler 9rr%r
&%mma %perat%r returns the (alue %f last e(aluated e!pressi%n. #%/ 1 is returned and )ets
assi)ned t% i.
'*#: <
2-. main() {
int i=2;
switch(i) {
default:printf("3er%");
break;
case 1: printf("%ne");
break;
case 2:printf("tw%");
break;
case +: printf("three");
break;
}
}
#tatements e!ecuted will be
'. default and case 2 <. default %nl$ &. default/ case 1/case 2 and case +; G. case 2 %nl$
default case can be placed an$ where in switch statement but it will be e!ecuted %nl$ if all
cases are failed.
'*#: G
21. Ka(e a l%%k at functi%n bel%w
functi%n()
{
}
hat is the return t$pe %f the functi%n"
'. (%id <. int &. =ts n%t specified/ hence c%mpiler err%r. G. =ts n%t specified/ hence a
warnin).
Gefault return %f (alue %f a functi%n in c is int.
'*#: <
22. main(){
int a= -;
int b = 2-;
char ! =1;
char $ =1-;
if(a/b/!/$)
printf("hell% ");
printf("w%rld");
}
hat will be the %utput
'. hell% w%rld <. hell% &. w%rld G. &%mpiler 9rr%r
&%mma %perat%r will return (alue %f last e(aluated e!pressi%n. Kere it is $. #% c%nditi%n is
true and "Kell% w%rld" )ets printed.
'*#: '
2+. Ret 2int aS1-TS2-T2 be a 2 dimensi%nal inte)er arra$. 5n a t$pical 'pple @acint%sh
machine the si3e %f inte)er (ariable is 1 b$tes. Ji(en that base address %f the 2G arra$ 2a2
is 2--4/ the address %f element aS1TS4T is
(a) 2-H- (b) 2-H1 (c) 2+14 (d) 2+17 (e) n%ne %f the ab%(e
=n &/ &++/ Oa(a the hi)her dimensi%nal arra$s are st%red in r%w maB%r %rder meanin) the
elements will be st%red c%nsecuti(el$/ %ne r%w f%ll%wed b$ the %ther. >he f%rmula t%
calculate the address %f the element aSiTSBT = a + ( i L c%lUsi3e + B ) L si3e%f(datat$pe). <$
substitutin) the (alues )i(en in the pr%blem we w%uld )et aS1TS4T= 2--4 + ( 1 L 2- + 4 ) L 1 =
2+14
'*#: &
21. >he reas%n f%r includin) header files at the t%p %f e(er$ pr%)ram is/
(a) >he$ c%ntain b%th declarati%ns and definiti%ns %f functi%ns we want t% use in the
pr%)ram
(b) >he$ c%ntain %nl$ definiti%ns %f the functi%ns which we want call in the pr%)ram
(c) >he$ c%ntain %nl$ declarati%ns %f the functi%ns which we want t% call in the pr%)ram
(d) =t is n%t mandat%r$ t% include header files but a (er$ )%%d pr%)rammin) practice.
Keader files c%ntains %nl$ declarati%ns %r pr%t%t$pes %f the functi%ns n%t their definiti%ns.
Mlease l%%k at an$ header file in IusrIinclude direct%r$ like 2stdi%.h2 f%r sample/ u will fi)ure
%ut the same.
'*#: &
24. =n a t$pical Q*=A s$stem/ where d%es the & lan)ua)e header files reside (i.e.
Math)
(a) IuserIinclude (b) IuserIcIinclude (c) IusrIinclude (d) IusrIcIinclude
>he path %f & lan)ua)e header files is : IusrIinclude ( Mlease tr$ t% see the c%ntent %f the
header files )
'*#: &
27. &%nsider the f%ll%win) c%de/
int :ecursi(e(int n)
{
return( :ecursi(e(n) );
}
main()
{
printf(".d;n"/ :ecursi(e(1));
}
hat can u sa$ ab%ut the ab%(e c%de"
(a) will )i(e a c%mpiler err%r sa$in) "n%t a (alid functi%n definiti%n"
(b) will )i(e a run0time err%r sa$in) "#tack %(erfl%w"
(c) will print s%me (alue returned b$ the functi%n call :ecursi(e(1)
(d) c%mpiler will issue a warnin) and run the pr%)ram with%ut an$ err%rs.
>he functi%n "recursi(e" is a useless functi%n which d%es n%thin) and miss the terminatin)
c%nditi%n %f the recursi%n/ s% the recursi%n c%ntinues till the entire #tack area is used up
b$ the pr%)ram and when the pr%)ram sees that #tack space is e!hausted/ it )i(e a runtime
err%r sa$in) "#tack %(erfl%w".
'*#: <
26. hat d%es the f%ll%win) c%de d%"
main()
{
int a/b/!;
scanf(".d.d"/Ca/Cb);
!=aFb " b : a;
while( ! F= (aLb) )
{
if ( (!.a==-) CC (!.b==-) )
{
printf(".d;n"/!);
break;
}
!++;
}
}
(a) Mrints the J&G %f a and b (b) Mrints the R&@ %f a and b
(c) Mrints all the fact%rs %f a and b (d) Mrints all the multiples %f a and b
=f $%u take 2 %r + sample (alues f%r a and b and w%rk %ut the c%de/ $%u will find that this
pr%)ram will calculate the R&@ %f a and b.
'*#: <
28. 5bser(e the f%ll%win) c%de/ and c%mment %n it
main()
{
int aS+T={1/1/+};
int i/B;
f%r(i=-;iF+;i++)
f%r(B=-;BF+;B++)
aSBT++;
printf(".d .d .d ;n"/aS-T/aS1T/aS2T);
}
hat is the %utput %f the ab%(e pr%)ram "
(a) 1-/1+/12 (b) &%mpiler issues a warnin) and prints 1-/1+/12
(c) 1/6/7 (d) &%mpiler issues a warnin) and prints 1/6/7
'rra$ elements are as )%%d as an$ %ther (ariable and thus can be incremented %r
decremented with%ut an$ pr%blem. #% c%mpiler will n%t )i(e an$ warnin) and each arra$
element is incremented + times. s% it w%uld print 1/6/7.
'*#: &
2H. hat can replace the f%ll%win) c%de"
i = 8;
while (i ,= 2)
{
i = i 0 1;
}
1 0 f%r ( i = 8; i ,=2; i++) 2 0 f%r ( i = 8; i ,=2; i00) + 0 f%r ( i = 2; i F=8; i00)
1 0 f%r ( i = 2; i ,=8; i++) 4 0 f%r ( i = 2; i F=8; i++)
>his is tri(ial/ (ari%us parts %f a while l%%p are put t%)ether in a sin)le line in a f%r l%%p.
'*#: 2
+-. hich %f the f%ll%win) is n%t all%wed"
1. case 2a2 : 2. if ( a == + ) +. case "G(%rak" 1. if ( ? 0 1 F 4 ) 4.0 case +6 :
=n a case label $%u can either use an inte)er c%nstant %r a character c%nstant but n%t a
strin) c%nstant.
'*#: +
(si3e%f(int) == 2 ma$ be assumed).
+1. (%id main()
{
int c%nst L p=4;
printf(".d"/++(Lp));
}
Answer:
&%mpiler err%r: &ann%t m%dif$ a c%nstant (alue.
Explanation:
p is a p%inter t% a "c%nstant inte)er". <ut we tried t% chan)e the (alue %f the "c%nstant
inte)er".
+2. main()
{
char sS T="man";
int i;
f%r(i=-;sS i T;i++)
printf(";n.c.c.c.c"/sS i T/L(s+i)/L(i+s)/iSsT);
}
Answer:
mmmm
aaaa
nnnn
Explanation:
sSiT/ L(i+s)/ L(s+i)/ iSsT are all different wa$s %f e!pressin) the same idea. Jenerall$ arra$
name is the base address f%r that arra$. Kere s is the base address. i is the inde!
numberIdisplacement fr%m the base address. #%/ indirectin) it with L is same as sSiT. iSsT
ma$ be surprisin). <ut in the case %f & it is same as sSiT.
++. main()
{
fl%at me = 1.1;
d%uble $%u = 1.1;
if(me==$%u)
printf("= l%(e Q");
else
printf("= hate Q");
}
Answer:
= hate Q
Explanation:
N%r fl%atin) p%int numbers (fl%at/ d%uble/ l%n) d%uble) the (alues cann%t be predicted
e!actl$. Gependin) %n the number %f b$tes/ the precessi%n with %f the (alue represented
(aries. Nl%at takes 1 b$tes and l%n) d%uble takes 1- b$tes. #% fl%at st%res -.H with less
precisi%n than l%n) d%uble.
Rule of Thum:
*e(er c%mpare %r at0least be cauti%us when usin) fl%atin) p%int numbers with relati%nal
%perat%rs (==/ ,/ F/ F=/ ,=/ V= ) .
+1. main()
{
static int (ar = 4;
printf(".d "/(ar00);
if((ar)
main();
}
Answer:
4 1 + 2 1
Explanation:
hen static st%ra)e class is )i(en/ it is initiali3ed %nce. >he chan)e in the (alue %f a static
(ariable is retained e(en between the functi%n calls. @ain is als% treated like an$ %ther
%rdinar$ functi%n/ which can be called recursi(el$.
+4. main()
{
int cS T={2.8/+.1/1/7.6/4};
int B/Lp=c/L?=c;
f%r(B=-;BF4;B++) {
printf(" .d "/Lc);
++?; }
f%r(B=-;BF4;B++){
printf(" .d "/Lp);
++p; }
}
Answer:
2 2 2 2 2 2 + 1 7 4
Explanation:
=nitiall$ p%inter c is assi)ned t% b%th p and !. =n the first l%%p/ since %nl$ ! is incremented
and n%t c / the (alue 2 will be printed 4 times. =n sec%nd l%%p p itself is incremented. #% the
(alues 2 + 1 7 4 will be printed.
+7. main()
{
e!tern int i;
i=2-;
printf(".d"/i);
}
Answer:
Rinker 9rr%r: Qndefined s$mb%l 2Ui2
Explanation:
extern st%ra)e class in the f%ll%win) declarati%n/
extern int i"
specifies t% the c%mpiler that the mem%r$ f%r i is all%cated in s%me %ther pr%)ram and that
address will be )i(en t% the current pr%)ram at the time %f linkin). <ut linker finds that n%
%ther (ariable %f name i is a(ailable in an$ %ther pr%)ram with mem%r$ space all%cated f%r
it. Kence a linker err%r has %ccurred.
+6. main()
{
char Lp;
printf(".d .d "/si3e%f(Lp)/si3e%f(p));
}
Answer:
1 2
Explanation:
>he si3e%f() %perat%r )i(es the number %f b$tes taken b$ its %perand. M is a character
p%inter/ which needs %ne b$te f%r st%rin) its (alue (a character). Kence si3e%f(Lp) )i(es a
(alue %f 1. #ince it needs tw% b$tes t% st%re the address %f the character p%inter si3e%f(p)
)i(es 2.
+8. main()
{
int i=+;
switch(i)
{
default:printf("3er%");
case 1: printf("%ne");
break;
case 2:printf("tw%");
break;
case +: printf("three");
break;
}
}
Answer:
three
Explanation:
>he default case can be placed an$where inside the l%%p. =t is e!ecuted %nl$ when all %ther
cases d%esn2t match.
+H. main()
{
printf(".!"/01FF1);
}
Answer:
fff-
Explanation :
01 is internall$ represented as all 12s. hen left shifted f%ur times the least si)nificant 1
bits are filled with -2s.>he .! f%rmat specifier specifies that the inte)er (alue be printed
as a he!adecimal (alue.
1-. main()
{
char strin)ST="Kell% %rld";
displa$(strin));
}
(%id displa$(char Lstrin))
{
printf(".s"/strin));
}
Answer:
&%mpiler 9rr%r: >$pe mismatch in redeclerati%n %f functi%n displa$
Explanation:
=n third line/ when the functi%n displa# is enc%untered/ the c%mpiler d%esn2t kn%w an$thin)
ab%ut the functi%n displa$. =t assumes the ar)uments and return t$pes t% be inte)ers/
(which is the default t$pe). hen it sees the actual functi%n displa#$ the ar)uments and
t$pe c%ntradicts with what it has assumed pre(i%usl$. Kence a c%mpile time err%r %ccurs.
11. Wdefine int char
main()
{
int i=74;
printf("si3e%f(i)=.d"/si3e%f(i));
}
Answer:
si3e%f(i)=1
Explanation:
#ince the Wdefine replaces the strin) int b$ the macr% char
12. main()
{
int i=1-;
i=Vi,11;
Mrintf ("i=.d"/i);
}
Answer:
i=-
Explanation:
=n the e!pressi%n %i&'( / *5> (V) %perat%r has m%re precedence than X ,Y s$mb%l. % is a unar$
l%)ical %perat%r. Vi (V1-) is - (n%t %f true is false). -,11 is false (3er%).
1+. WincludeFstdi%.h,
main()
{
char sST={2a2/2b2/2c2/2;n2/2c2/2;-2};
char Lp/Lstr/Lstr1;
p=CsS+T;
str=p;
str1=s;
printf(".d"/++Lp + ++Lstr10+2);
}
Answer:
66
Explanation:
p is p%intin) t% character 2;n2. str1 is p%intin) t% character 2a2 ++Lp. "p is p%intin) t% 2;n2
and that is incremented b$ %ne." the '#&== (alue %f 2;n2 is 1-/ which is then incremented t%
11. >he (alue %f ++Lp is 11. ++Lstr1/ str1 is p%intin) t% 2a2 that is incremented b$ 1 and it
bec%mes 2b2. '#&== (alue %f 2b2 is H8.
*%w perf%rmin) (11 + H8 Z +2)/ we )et 66("@");
#% we )et the %utput 66:: "@" ('scii is 66).
11. WincludeFstdi%.h,
main()
{
int aS2TS2TS2T = { {1-/2/+/1}/ {4/7/6/8} };
int Lp/L?;
p=CaS2TS2TS2T;
L?=LLLa;
printf(".d0000.d"/Lp/L?);
}
Answer:
#%me Jarba)e [alue0001-
Explanation:
p=CaS2TS2TS2T $%u declare %nl$ tw% 2G arra$s/ but $%u are tr$in) t% access the third
2G(which $%u are n%t declared) it will print )arba)e (alues. L?=LLLa startin) address %f a is
assi)ned inte)er p%inter. *%w ? is p%intin) t% startin) address %f a. =f $%u print L?/ it will
print first element %f +G arra$.
14. WincludeFstdi%.h,
main()
{
struct !!
{
int !=+;
char nameST="hell%";
};
struct !! Ls;
printf(".d"/s0,!);
printf(".s"/s0,name);
}
Answer:
&%mpiler 9rr%r
Explanation:
P%u sh%uld n%t initiali3e (ariables in declarati%n
17. WincludeFstdi%.h,
main()
{
struct !!
{
int !;
struct $$
{
char s;
struct !! Lp;
};
struct $$ L?;
};
}
Answer:
&%mpiler 9rr%r
Explanation:
>he structure $$ is nested within structure !!. Kence/ the elements are %f $$ are t% be
accessed thr%u)h the instance %f structure !!/ which needs an instance %f $$ t% be kn%wn.
=f the instance is created after definin) the structure the c%mpiler will n%t kn%w ab%ut the
instance relati(e t% !!. Kence f%r nested structure $$ $%u ha(e t% declare member.
16. main()
{
printf(";nab");
printf(";bsi");
printf(";rha");
}
Answer:
hai
Explanation:
;n 0 newline
;b 0 backspace
;r 0 linefeed
18. Wdefine s?uare(!) !L!
main()
{
int i;
i = 71Is?uare(1);
printf(".d"/i);
}
Answer:
71
Explanation:
>he macr% call s?uare(1) will substituted b$ 1L1 s% the e!pressi%n bec%mes i = 71I1L1 .
#ince I and L has e?ual pri%rit$ the e!pressi%n will be e(aluated as (71I1)L1 i.e. 17L1 = 71
1H. main()
{
char Lp="hai friends"/Lp1;
p1=p;
while(LpV=2;-2) ++Lp++;
printf(".s .s"/p/p1);
}
Answer:
ibBV)sBf%et
Explanation:
++Lp++ will be parse in the )i(en %rder
Lp that is (alue at the l%cati%n currentl$ p%inted b$ p will be taken
++Lp the retrie(ed (alue will be incremented
when ; is enc%untered the l%cati%n will be incremented that is p++ will be e!ecuted
Kence/ in the while l%%p initial (alue p%inted b$ p is XhY/ which is chan)ed t% XiY b$ e!ecutin) +
+Lp and p%inter m%(es t% p%int/ XaY which is similarl$ chan)ed t% XbY and s% %n. #imilarl$ blank
space is c%n(erted t% XVY. >hus/ we %btain (alue in p bec%mes \ibBV)sBf%et] and since p reaches
X;-Y and p1 p%ints t% p thus p1d%esn%t print an$thin).
4-. Winclude Fstdi%.h,
Wdefine a 1-
main()
{
Wdefine a 4-
printf(".d"/a);
}
Answer:
4-
Explanation:
>he prepr%cess%r directi(es can be redefined an$where in the pr%)ram. #% the m%st
recentl$ assi)ned (alue will be taken.
41. Wdefine clrscr() 1--
main()
{
clrscr();
printf(".d;n"/clrscr());
}
Answer:
1--
Explanation:
Mrepr%cess%r e!ecutes as a separate pass bef%re the e!ecuti%n %f the c%mpiler. #% te!tual
replacement %f clrscr() t% 1-- %ccurs. >he input pr%)ram t% c%mpiler l%%ks like this :
main()
{
1--;
printf(".d;n"/1--);
}
Note: 1--; is an e!ecutable statement but with n% acti%n. #% it d%esn2t )i(e an$ pr%blem
42. main()
{
printf(".p"/main);
}
Answer:
#%me address will be printed.
Explanation:
Nuncti%n names are Bust addresses (Bust like arra$ names are addresses). main() is als% a
functi%n. #% the address %f functi%n main will be printed. .p in printf specifies that the
ar)ument is an address. >he$ are printed as he!adecimal numbers.
4+. main()
{
clrscr();
}
clrscr();
Answer:
*% %utputIerr%r
Explanation:
>he first clrscr() %ccurs inside a functi%n. #% it bec%mes a functi%n call. =n the sec%nd
clrscr(); is a functi%n declarati%n (because it is n%t inside an$ functi%n).
41. enum c%l%rs {<R'&^/<RQ9/J:99*}
main()
{
printf(".d...d...d"/<R'&^/<RQ9/J:99*);
return(1);
}
Answer:
-..1..2
Explanation:
enum assi)ns numbers startin) fr%m -/ if n%t e!plicitl$ defined.
44. (%id main()
{
char far Lfarther/ Lfarthest;
printf(".d...d"/si3e%f(farther)/si3e%f(farthest));
}
Answer:
1..2
Explanation:
>he sec%nd p%inter is %f char t$pe and n%t a far p%inter
47. main()
{
int i=1--/B=+--;
printf(".d...d");
}
Answer:
+--..1--
Explanation:
printf takes the (alues %f the last tw% assi)nments %f the pr%)ram. 'n$ number %f printf2s
ma$ be )i(en. 'll %f them take %nl$ the first tw% (alues. =f m%re number %f assi)nments
)i(en in the pr%)ram/ then printf will take )arba)e (alues.
46. main()
{
char Lp;
p="Kell%";
printf(".c;n"/LCLp);
}
Answer:
K
Explanation:
L is a dereference %perat%r C is a reference %perat%r. >he$ can be applied an$ number %f
times pr%(ided it is meanin)ful. Kere p p%ints t% the first character in the strin) "Kell%". Lp
dereferences it and s% its (alue is K. ')ain C references it t% an address and L
dereferences it t% the (alue K.
48. main()
{
int i=1;
while (iF=4)
{
printf(".d"/i);
if (i,2)
)%t% here;
i++;
}
}
fun()
{
here:
printf("MM");
}
Answer:
&%mpiler err%r: Qndefined label 2here2 in functi%n main
Explanation:
Rabels ha(e functi%ns sc%pe/ in %ther w%rds the sc%pe %f the labels is limited t% functi%ns.
>he label 2here2 is a(ailable in functi%n fun() Kence it is n%t (isible in functi%n main.
4H. main()
{
static char namesS4TS2-T={"pascal"/"ada"/"c%b%l"/"f%rtran"/"perl"};
int i;
char Lt;
t=namesS+T;
namesS+T=namesS1T;
namesS1T=t;
f%r (i=-;iF=1;i++)
printf(".s"/namesSiT);
}
Answer:
&%mpiler err%r: R(alue re?uired in functi%n main
Explanation:
'rra$ names are p%inter c%nstants. #% it cann%t be m%dified.
7-. (%id main()
{
int i=4;
printf(".d"/i++ + ++i);
}
Answer:
5utput cann%t be predicted e!actl$.
Explanation:
#ide effects are in(%l(ed in the e(aluati%n %f i
71. (%id main()
{
int i=4;
printf(".d"/i+++++i);
}
Answer:
&%mpiler 9rr%r
Explanation:
>he e!pressi%n i+++++i is parsed as i ++ ++ + i which is an ille)al c%mbinati%n %f %perat%rs.
72. WincludeFstdi%.h,
main()
{
int i=1/B=2;
switch(i)
{
case 1: printf("J55G");
break;
case B: printf("<'G");
break;
}
}
Answer:
&%mpiler 9rr%r: &%nstant e!pressi%n re?uired in functi%n main.
Explanation:
>he case statement can ha(e %nl$ c%nstant e!pressi%ns (this implies that we cann%t use
(ariable names directl$ s% an err%r).
Note: 9numerated t$pes can be used in case statements.
7+. main()
{
int i;
printf(".d"/scanf(".d"/Ci)); II (alue 1- is )i(en as input here
}
Answer:
1
Explanation:
#canf returns number %f items successfull$ read and n%t 1I-. Kere 1- is )i(en as input
which sh%uld ha(e been scanned successfull$. #% number %f items read is 1.
71. Wdefine f()/)2) )WW)2
main()
{
int (ar12=1--;
printf(".d"/f((ar/12));
}
Answer:
1--
74. main()
{
int i=-;
f%r(;i++;printf(".d"/i)) ;
printf(".d"/i);
}
Answer:
1
Explanation:
<ef%re enterin) int% the f%r l%%p the checkin) c%nditi%n is "e(aluated". Kere it e(aluates t%
- (false) and c%mes %ut %f the l%%p/ and i is incremented (n%te the semic%l%n after the f%r
77. main()
{
printf(".d"/ %ut);
}
int %ut=1--;
Answer:
&%mpiler err%r: undefined s$mb%l %ut in functi%n main.
Explanation:
>he rule is that a (ariable is a(ailable f%r use fr%m the p%int %f declarati%n. 9(en th%u)h
%ut is a )l%bal (ariable/ it is n%t a(ailable f%r main. Kence an err%r.
76. main()
{
e!tern %ut;
printf(".d"/ %ut);
}
int %ut=1--;
Answer:
1--
Explanation:
>his is the c%rrect wa$ %f writin) the pre(i%us pr%)ram.
78. main()
{
sh%w();
}
(%id sh%w()
{
printf("=2m the )reatest");
}
Answer:
&%mpiler err%r: >$pe mismatch in redeclarati%n %f sh%w.
Explanation:
hen the c%mpiler sees the functi%n sh%w it d%esn2t kn%w an$thin) ab%ut it. #% the default
return t$pe (i.e./ int) is assumed. <ut when c%mpiler sees the actual definiti%n %f sh%w
mismatch %ccurs since it is declared as (%id. Kence the err%r.
>he s%luti%ns are as f%ll%ws:
1. declare (%id sh%w() in main() .
2. define sh%w() bef%re main().
+. declare e!tern (%id sh%w() bef%re the use %f sh%w().
7H. main( )
{
int aS2TS+TS2T = {{{2/1}/{6/8}/{+/1}}/{{2/2}/{2/+}/{+/1}}};
printf(\.u .u .u .d ;n]/a/La/LLa/LLLa);
printf(\.u .u .u .d ;n]/a+1/La+1/LLa+1/LLLa+1);
}
Answer:
1--/ 1--/ 1--/ 2
112/ 1-1/ 1-2/ +
Explanation:
>he )i(en arra$ is a +0G %ne. =t can als% be (iewed as a 10G arra$.
2 1 6 8 + 1 2 2 2 + + 1
1-- 1-2 1-1 1-7 1-8 11- 112 111 117 118 12- 122
thus/ f%r the first printf statement a/ La/ LLa )i(e address %f first element . since the
indirecti%n LLLa )i(es the (alue. Kence/ the first line %f the %utput.
N%r the sec%nd printf a+1 increases in the third dimensi%n thus p%ints t% (alue at 112/ La+1
increments in sec%nd dimensi%n thus p%ints t% 1-1/ LLa +1 increments the first dimensi%n
thus p%ints t% 1-2 and LLLa+1 first )ets the (alue at first l%cati%n and then increments it b$
1. Kence/ the %utput.
6-. main( )
{
int aS T = {1-/2-/+-/1-/4-}/B/Lp;
f%r(B=-; BF4; B++)
{
printf(\.d] /La);
a++;
}
p = a;
f%r(B=-; BF4; B++)
{
printf(\.d ] /Lp);
p++;
}
}
Answer:
&%mpiler err%r: l(alue re?uired.
Explanation:
9rr%r is in line with statement a++. >he %perand must be an l(alue and ma$ be %f an$ %f
scalar t$pe f%r the an$ %perat%r/ arra$ name %nl$ when subscripted is an l(alue. #impl$ arra$
name is a n%n0m%difiable l(alue.
61. main( )
{
static int aS T = {-/1/2/+/1};
int LpS T = {a/a+1/a+2/a++/a+1};
int LLptr = p;
ptr++;
printf(\;n .d .d .d]/ ptr0p/ Lptr0a/ LLptr);
Lptr++;
printf(\;n .d .d .d]/ ptr0p/ Lptr0a/ LLptr);
L++ptr;
printf(\;n .d .d .d]/ ptr0p/ Lptr0a/ LLptr);
++Lptr;
printf(\;n .d .d .d]/ ptr0p/ Lptr0a/ LLptr);
}
Answer:
111
222
+++
+11
Explanation:
Ret us c%nsider the arra$ and the tw% p%inters with s%me address
a
- 1 2 + 1
1-- 1-2 1-1 1-7 1-8
p
1-- 1-2 1-1 1-7 1-8
1--- 1--2 1--1 1--7 1--8
ptr
1---
2---
'fter e!ecuti%n %f the instructi%n ptr++ (alue in ptr bec%mes 1--2/ if scalin) fact%r f%r
inte)er is 2 b$tes. *%w ptr Z p is (alue in ptr Z startin) l%cati%n %f arra$ p/ (1--2 Z 1---) I
(scalin) fact%r) = 1/ Lptr Z a = (alue at address p%inted b$ ptr Z startin) (alue %f arra$ a/
1--2 has a (alue 1-2 s% the (alue is (1-2 Z 1--)I(scalin) fact%r) = 1/ LLptr is the (alue
st%red in the l%cati%n p%inted b$ the p%inter %f ptr = (alue p%inted b$ (alue p%inted b$ 1--2
= (alue p%inted b$ 1-2 = 1. Kence the %utput %f the firs printf is 1/ 1/ 1.
'fter e!ecuti%n %f Lptr++ increments (alue %f the (alue in ptr b$ scalin) fact%r/ s% it
bec%mes1-1. Kence/ the %utputs f%r the sec%nd printf are ptr Z p = 2/ Lptr Z a = 2/ LLptr =
2.
'fter e!ecuti%n %f L++ptr increments (alue %f the (alue in ptr b$ scalin) fact%r/ s% it
bec%mes1--1. Kence/ the %utputs f%r the third printf are ptr Z p = +/ Lptr Z a = +/ LLptr = +.
'fter e!ecuti%n %f ++Lptr (alue in ptr remains the same/ the (alue p%inted b$ the (alue is
incremented b$ the scalin) fact%r. #% the (alue in arra$ p at l%cati%n 1--7 chan)es fr%m
1-7 1- 1-8/. Kence/ the %utputs f%r the f%urth printf are ptr Z p = 1--7 Z 1--- = +/ Lptr Z a
= 1-8 Z 1-- = 1/ LLptr = 1.
62. main( )
{
char L?;
int B;
f%r (B=-; BF+; B++) scanf(\.s] /(?+B));
f%r (B=-; BF+; B++) printf(\.c] /L(?+B));
f%r (B=-; BF+; B++) printf(\.s] /(?+B));
}
Explanation:
Kere we ha(e %nl$ %ne p%inter t% t$pe char and since we take input in the same p%inter thus
we keep writin) %(er in the same l%cati%n/ each time shiftin) the p%inter (alue b$ 1. #upp%se
the inputs are @5Q#9/ >:'&^ and [=:>Q'R. >hen f%r the first input supp%se the p%inter
starts at l%cati%n 1-- then the input %ne is st%red as
@ 5 Q # 9 ;-
hen the sec%nd input is )i(en the p%inter is incremented as B (alue bec%mes 1/ s% the input
is filled in mem%r$ startin) fr%m 1-1.
@ >

: ' & ^ ;-
>he third input starts fillin) fr%m the l%cati%n 1-2
@ >

[

= : >

Q ' R ;-
>his is the final (alue st%red .
>he first printf prints the (alues at the p%siti%n ?/ ?+1 and ?+2 = @ > [
>he sec%nd printf prints three strin)s startin) fr%m l%cati%ns ?/ ?+1/ ?+2
i.e @>[=:>Q'R/ >[=:>Q'R and [=:>Q'R.
6+. main( )
{
(%id L(p;
char ch = X)Y/ Lcp = \)%%f$];
int B = 2-;
(p = Cch;
printf(\.c]/ L(char L)(p);
(p = CB;
printf(\.d]/L(int L)(p);
(p = cp;
printf(\.s]/(char L)(p + +);
}
Answer:
)2-f$
Explanation:
#ince a (%id p%inter is used it can be t$pe casted t% an$ %ther t$pe p%inter. (p = Cch st%res
address %f char ch and the ne!t statement prints the (alue st%red in (p after t$pe castin)
it t% the pr%per data t$pe p%inter. the %utput is X)Y. #imilarl$ the %utput fr%m sec%nd printf
is X2-Y. >he third printf statement t$pe casts it t% print the strin) fr%m the 1
th
(alue hence
the %utput is Xf$Y.
61. main ( )
{
static char LsS T = {\black]/ \white]/ \$ell%w]/ \(i%let]};
char LLptrS T = {s++/ s+2/ s+1/ s}/ LLLp;
p = ptr;
LL++p;
printf(\.s]/L00L++p + +);
}
Answer:
ck
Explanation:
=n this pr%blem we ha(e an arra$ %f char p%inters p%intin) t% start %f 1 strin)s. >hen we
ha(e ptr which is a p%inter t% a p%inter %f t$pe char and a (ariable p which is a p%inter t% a
p%inter t% a p%inter %f t$pe char. p h%ld the initial (alue %f ptr/ i.e. p = s++. >he ne!t
statement increment (alue in p b$ 1 / thus n%w (alue %f p = s+2. =n the printf statement the
e!pressi%n is e(aluated L++p causes )ets (alue s+1 then the pre decrement is e!ecuted and
we )et s+1 Z 1 = s . the indirecti%n %perat%r n%w )ets the (alue fr%m the arra$ %f s and adds
+ t% the startin) address. >he strin) is printed startin) fr%m this p%siti%n. >hus/ the %utput
is XckY.
64. main()
{
int i/ n;
char L! = \)irl];
n = strlen(!);
L! = !SnT;
f%r(i=-; iFn; ++i)
{
printf(\.s;n]/!);
!++;
}
}
Answer:
(blank space)
irl
rl
l
Explanation:
Kere a strin) (a p%inter t% char) is initiali3ed with a (alue \)irl]. >he strlen functi%n returns
the len)th %f the strin)/ thus n has a (alue 1. >he ne!t statement assi)ns (alue at the nth
l%cati%n (X;-Y) t% the first l%cati%n. *%w the strin) bec%mes \;-irl] . *%w the printf
statement prints the strin) after each iterati%n it increments it startin) p%siti%n. R%%p
starts fr%m - t% 1. >he first time !S-T = X;-Y hence it prints n%thin) and p%inter (alue is
incremented. >he sec%nd time it prints fr%m !S1T i.e \irl] and the third time it prints \rl] and
the last time it prints \l] and the l%%p terminates.
67. int i/B;
f%r(i=-;iF=1-;i++)
{
B+=4;
assert(iF4);
}
Answer:
:untime err%r: 'bn%rmal pr%)ram terminati%n.
assert failed (iF4)/ Ffile name,/Fline number,
Explanation:
asserts are used durin) debu))in) t% make sure that certain c%nditi%ns are satisfied. =f
asserti%n fails/ the pr%)ram will terminate rep%rtin) the same. 'fter debu))in) use/
Wundef *G9<QJ
and this will disable all the asserti%ns fr%m the s%urce c%de. 'sserti%n is a )%%d debu))in)
t%%l t% make use %f.
67. main()
{
int i=01;
+i;
printf("i = .d/ +i = .d ;n"/i/+i);
}
Answer:
i = 01/ +i = 01
Explanation:
Qnar$ + is the %nl$ dumm$ %perat%r in &. here0e(er it c%mes $%u can Bust i)n%re it Bust
because it has n% effect in the e!pressi%ns (hence the name dumm$ %perat%r).
66. hat are the files which are aut%maticall$ %pened when a & file is e!ecuted"
Answer:
stdin/ std%ut/ stderr (standard input/ standard %utput/ standard err%r).
68. what will be the p%siti%n %f the file marker"
a: fseek(ptr/-/#99^U#9>);
b: fseek(ptr/-/#99^U&Q:);
Answer :
a: >he #99^U#9> sets the file p%siti%n marker t% the startin) %f the file.
b: >he #99^U&Q: sets the file p%siti%n marker t% the current p%siti%n %f the file.
6H. main()
{
char nameS1-T/sS12T;
scanf(" ;".S_;"T;""/s);
}
K%w scanf will e!ecute"
Answer:
Nirst it checks f%r the leadin) white space and discards it. >hen it matches with a ?u%tati%n
mark and then it reads all character up t% an%ther ?u%tati%n mark.
8-. hat is the pr%blem with the f%ll%win) c%de se)ment"
while ((f)ets(recei(in) arra$/4-/fileUptr)) V= 95N) ;
Answer ) Explanation:
f)ets returns a p%inter. #% the c%rrect end %f file check is checkin) f%r V= *QRR.
81. main()
{
main();
}
Answer:
:untime err%r: #tack %(erfl%w.
Explanation:
main functi%n calls itself a)ain and a)ain. 9ach time the functi%n is called its return address
is st%red in the call stack. #ince there is n% c%nditi%n t% terminate the functi%n call/ the call
stack %(erfl%ws at runtime. #% it terminates the pr%)ram and results in an err%r.
82. main()
{
char Lcptr/c;
(%id L(ptr/(;
c=1-; (=-;
cptr=Cc; (ptr=C(;
printf(".c.("/c/();
}
Answer:
&%mpiler err%r (at line number 1): si3e %f ( is Qnkn%wn.
Explanation:
P%u can create a (ariable %f t$pe (%id L but n%t %f t$pe (%id/ since (%id is an empt$ t$pe. =n
the sec%nd line $%u are creatin) (ariable (ptr %f t$pe (%id L and ( %f t$pe (%id hence an
err%r.
8+ main()
{
char Lstr1="abcd";
char str2ST="abcd";
printf(".d .d .d"/si3e%f(str1)/si3e%f(str2)/si3e%f("abcd"));
}
Answer:
2 4 4
Explanation:
=n first si3e%f/ str1 is a character p%inter s% it )i(es $%u the si3e %f the p%inter (ariable. =n
sec%nd si3e%f the name str2 indicates the name %f the arra$ wh%se si3e is 4 (includin) the
2;-2 terminati%n character). >he third si3e%f is similar t% the sec%nd %ne.
81. main()
{
char n%t;
n%t=V2;
printf(".d"/n%t);
}
Answer:
-
Explanation:
V is a l%)ical %perat%r. =n & the (alue - is c%nsidered t% be the b%%lean (alue N'R#9/ and an$
n%n03er% (alue is c%nsidered t% be the b%%lean (alue >:Q9. Kere 2 is a n%n03er% (alue s%
>:Q9. V>:Q9 is N'R#9 (-) s% it prints -.
84. Wdefine N'R#9 01
Wdefine >:Q9 1
Wdefine *QRR -
main() {
if(*QRR)
puts("*QRR");
else if(N'R#9)
puts(">:Q9");
else
puts("N'R#9");
}
Answer:
>:Q9
Explanation:
>he input pr%)ram t% the c%mpiler after pr%cessin) b$ the prepr%cess%r is/
main()
{
if(-)
puts("*QRR");
else if(01)
puts(">:Q9");
else
puts("N'R#9");
}
Mrepr%cess%r d%esn2t replace the (alues )i(en inside the d%uble ?u%tes. >he check b$ if
c%nditi%n is b%%lean (alue false s% it )%es t% else. =n sec%nd if 01 is b%%lean (alue true hence
">:Q9" is printed.
87. main()
{
int k=1;
printf(".d==1 is "".s"/k/k==1"">:Q9":"N'R#9");
}
Answer:
1==1 is >:Q9
Explanation:
hen tw% strin)s are placed t%)ether (%r separated b$ white0space) the$ are c%ncatenated
(this is called as "strin)i3ati%n" %perati%n). #% the strin) is as if it is )i(en as ".d==1 is .s".
>he c%nditi%nal %perat%r ( ": ) e(aluates t% ">:Q9".
86. main()
{
int $;
scanf(".d"/C$); II input )i(en is 2---
if( ($.1==- CC $.1-- V= -) DD $.1-- == - )
printf(".d is a leap $ear");
else
printf(".d is n%t a leap $ear");
}
Answer:
2--- is a leap $ear
Explanation:
'n %rdinar$ pr%)ram t% check if leap $ear %r n%t.
88. Wdefine ma! 4
Wdefine int arr1Sma!T
main()
{
t$pedef char arr2Sma!T;
arr1 list={-/1/2/+/1};
arr2 name="name";
printf(".d .s"/listS-T/name);
}
Answer:
&%mpiler err%r (in the line arr1 list = {-/1/2/+/1})
Explanation:
arr2 is declared %f t$pe arra$ %f si3e 4 %f characters. #% it can be used t% declare the
(ariable name %f the t$pe arr2. <ut it is n%t the case %f arr1. Kence an err%r.
Rule of Thum:
Wdefines are used f%r te!tual replacement whereas t$pedefs are used f%r declarin) new
t$pes.
8H. int i=1-;
main()
{
e!tern int i;
{
int i=2-;
{
c%nst (%latile unsi)ned i=+-;
printf(".d"/i);
}
printf(".d"/i);
}
printf(".d"/i);
}
Answer:
+-/2-/1-
Explanation:
2{2 intr%duces new bl%ck and thus new sc%pe. =n the innerm%st bl%ck i is declared as/ c%nst
(%latile unsi)ned which is a (alid declarati%n. i is assumed %f t$pe int. #% printf prints +-. =n
the ne!t bl%ck/ i has (alue 2- and s% printf prints 2-. =n the %uterm%st bl%ck/ i is declared
as e!tern/ s% n% st%ra)e space is all%cated f%r it. 'fter c%mpilati%n is %(er the linker
res%l(es it t% )l%bal (ariable i (since it is the %nl$ (ariable (isible there). #% it prints i2s
(alue as 1-.
H-. main()
{
int LB;
{
int i=1-;
B=Ci;
}
printf(".d"/LB);
}
Answer:
1-
Explanation:
>he (ariable i is a bl%ck le(el (ariable and the (isibilit$ is inside that bl%ck %nl$. <ut the
lifetime %f i is lifetime %f the functi%n s% it li(es up t% the e!it %f main functi%n. #ince the i
is still all%cated space/ LB prints the (alue st%red in i since B p%ints i.
H1. main()
{
int i=01;
0i;
printf("i = .d/ 0i = .d ;n"/i/0i);
}
Answer:
i = 01/ 0i = 1
Explanation:
0i is e!ecuted and this e!ecuti%n d%esn2t affect the (alue %f i. =n printf first $%u Bust print
the (alue %f i. 'fter that the (alue %f the e!pressi%n 0i = 0(01) is printed.
H2. WincludeFstdi%.h,
main()
{
c%nst int i=1;
fl%at B;
B = ++i;
printf(".d .f"/ i/++B);
}
Answer:
&%mpiler err%r
Explanation:
i is a c%nstant. P%u cann%t chan)e the (alue %f c%nstant
H+. WincludeFstdi%.h,
main()
{
re)ister i=4;
char BST= "hell%";
printf(".s .d"/B/i);
}
Answer:
hell% 4
Explanation:
=f $%u declare i as re)ister c%mpiler will treat it as %rdinar$ inte)er and it will take inte)er
(alue. i (alue ma$ be st%red either in re)ister %r in mem%r$.
H1. main()
{
int i=4/B=7/3;
printf(".d"/i+++B);
}
Answer:
11
Explanation:
>he e!pressi%n i+++B is treated as (i++ + B)
H4. struct aaa{
struct aaa Lpre(;
int i;
struct aaa Lne!t;
};
main()
{
struct aaa abc/def/)hi/Bkl;
int !=1--;
abc.i=-;abc.pre(=CBkl;
abc.ne!t=Cdef;
def.i=1;def.pre(=Cabc;def.ne!t=C)hi;
)hi.i=2;)hi.pre(=Cdef;
)hi.ne!t=CBkl;
Bkl.i=+;Bkl.pre(=C)hi;Bkl.ne!t=Cabc;
!=abc.ne!t0,ne!t0,pre(0,ne!t0,i;
printf(".d"/!);
}
Answer:
2
Explanation:
ab%(e all statements f%rm a d%uble circular linked list;
abc.ne!t0,ne!t0,pre(0,ne!t0,i
this %ne p%ints t% ")hi" n%de the (alue %f at particular n%de is 2.
H7. struct p%int
{
int !;
int $;
};
struct p%int %ri)in/Lpp;
main()
{
pp=C%ri)in;
printf("%ri)in is(.d.d);n"/(Lpp).!/(Lpp).$);
printf("%ri)in is (.d.d);n"/pp0,!/pp0,$);
}
Answer:
%ri)in is(-/-)
%ri)in is(-/-)
Explanation:
pp is a p%inter t% structure. e can access the elements %f the structure either with arr%w
mark %r with indirecti%n %perat%r.
Note: #ince structure p%int is )l%ball$ declared ! C $ are initiali3ed as 3er%es
H6. main()
{
int i=UlUabc(1-);
printf(".d;n"/00i);
}
int UlUabc(int i)
{
return(i++);
}
Answer:
H
Explanation:
return(i++) it will first return i and then increments. i.e. 1- will be returned.
H8. main()
{
char Lp;
int L?;
l%n) Lr;
p=?=r=-;
p++;
?++;
r++;
printf(".p....p....p"/p/?/r);
}
Answer:
---1...---2...---1
Explanation:
++ %perat%r when applied t% p%inters increments address acc%rdin) t% their c%rresp%ndin)
data0t$pes.
HH. main()
{
char c=2 2/!/c%n(ert(3);
)etc(c);
if((c,=2a2) CC (cF=232))
!=c%n(ert(c);
printf(".c"/!);
}
c%n(ert(3)
{
return 30+2;
}
Answer:
&%mpiler err%r
Explanation:
declarati%n %f c%n(ert and f%rmat %f )etc() are wr%n).
1--. main(int ar)c/ char LLar)()
{
printf("enter the character");
)etchar();
sum(ar)(S1T/ar)(S2T);
}
sum(num1/num2)
int num1/num2;
{
return num1+num2;
}
Answer:
&%mpiler err%r.
Explanation:
ar)(S1T C ar)(S2T are strin)s. >he$ are passed t% the functi%n sum with%ut c%n(ertin) it t%
inte)er (alues.
1-1. W include Fstdi%.h,
int %neUdST={1/2/+};
main()
{
int Lptr;
ptr=%neUd;
ptr+=+;
printf(".d"/Lptr);
}
Answer:
)arba)e (alue
Explanation:
ptr p%inter is p%intin) t% %ut %f the arra$ ran)e %f %neUd.
1-2. W includeFstdi%.h,
aaa() {
printf("hi");
}
bbb(){
printf("hell%");
}
ccc(){
printf("b$e");
}
main()
{
int (LptrS+T)();
ptrS-T=aaa;
ptrS1T=bbb;
ptrS2T=ccc;
ptrS2T();
}
Answer:
b$e
Explanation:
ptr is arra$ %f p%inters t% functi%ns %f return t$pe int. ptrS-T is assi)ned t% address %f the
functi%n aaa. #imilarl$ ptrS1T and ptrS2T f%r bbb and ccc respecti(el$. ptrS2T() is in effect %f
writin) ccc()/ since ptrS2T p%ints t% ccc.
1-+. WincludeFstdi%.h,
main()
{
N=R9 Lptr;
char i;
ptr=f%pen("333.c"/"r");
while((i=f)etch(ptr))V=95N)
printf(".c"/i);
}
Answer:
c%ntents %f 333.c f%ll%wed b$ an infinite l%%p
Explanation:
>he c%nditi%n is checked a)ainst 95N/ it sh%uld be checked a)ainst *QRR.
1-1. main()
{
int i =-;B=-;
if(i CC B++)
printf(".d...d"/i++/B);
printf(".d...d/i/B);
}
Answer:
-..-
Explanation:
>he (alue %f i is -. #ince this inf%rmati%n is en%u)h t% determine the truth (alue %f the
b%%lean e!pressi%n. #% the statement f%ll%win) the if statement is n%t e!ecuted. >he (alues
%f i and B remain unchan)ed and )et printed.
1-4. main()
{
int i;
i = abc();
printf(".d"/i);
}
abc()
{
U'A = 1---;
}
Answer:
1---
Explanation:
*%rmall$ the return (alue fr%m the functi%n is thr%u)h the inf%rmati%n fr%m the
accumulat%r. Kere U'K is the pseud% )l%bal (ariable den%tin) the accumulat%r. Kence/ the
(alue %f the accumulat%r is set 1--- s% the functi%n returns (alue 1---.
1-7. int i;
main(){
int t;
f%r ( t=1;scanf(".d"/Ci)0t;printf(".d;n"/i))
printf(".d00"/t00);
}
II =f the inputs are -/1/2/+ find the %Ip
Answer:
100-
+001
2002
Explanation:
Ret us assume s%me != scanf(".d"/Ci)0t the (alues durin) e!ecuti%n
will be/
t i !
1 - 01
+ 1 02
2 2 -
1-6. main(){
int a= -;int b = 2-;char ! =1;char $ =1-;
if(a/b/!/$)
printf("hell%");
}
Answer:
hell%
Explanation:
>he c%mma %perat%r has ass%ciati(it$ fr%m left t% ri)ht. 5nl$ the ri)htm%st (alue is
returned and the %ther (alues are e(aluated and i)n%red. >hus the (alue %f last (ariable $ is
returned t% check in if. #ince it is a n%n 3er% (alue if bec%mes true s%/ "hell%" will be
printed.
1-8. main(){
unsi)ned int i;
f%r(i=1;i,02;i00)
printf("c aptitude");
}
Explanation:
i is an unsi)ned inte)er. =t is c%mpared with a si)ned (alue. #ince the b%th t$pes d%esn2t
match/ si)ned is pr%m%ted t% unsi)ned (alue. >he unsi)ned e?ui(alent %f 02 is a hu)e (alue s%
c%nditi%n bec%mes false and c%ntr%l c%mes %ut %f the l%%p.
1-H. =n the f%ll%win) pr%)ram add a statement in the functi%n fun such that the address %f
2a2 )ets st%red in 2B2.
main(){
int L B;
(%id fun(int LL);
fun(CB);
}
(%id fun(int LLk) {
int a =-;
IL add a stmt hereLI
}
Answer:
Lk = Ca
Explanation:
>he ar)ument %f the functi%n is a p%inter t% a p%inter.
11-. hat are the f%ll%win) n%tati%ns %f definin) functi%ns kn%wn as"
i. int abc(int a/fl%at b)
{
IL s%me c%de LI
}
ii. int abc(a/b)
int a; fl%at b;
{
IL s%me c%deLI
}
Answer:
i. '*#= & n%tati%n
ii. ^erni)han C :itche n%tati%n
111. main()
{
char Lp;
p=".d;n";
p++;
p++;
printf(p02/+--);
}
Answer:
+--
Explanation:
>he p%inter p%ints t% . since it is incremented twice and a)ain decremented b$ 2/ it p%ints
t% 2.d;n2 and +-- is printed.
112. main(){
char aS1--T;
aS-T=2a2;aS1T=2b2;aS2T=2c2;aS1T=2d2;
abc(a);
}
abc(char aST){
a++;
printf(".c"/La);
a++;
printf(".c"/La);
}
Explanation:
>he base address is m%dified %nl$ in functi%n and as a result a p%ints t% 2b2 then after
incrementin) t% 2c2 s% bc will be printed.
11+/ func(a/b)
int a/b;
{
return( a= (a==b) );
}
main()
{
int pr%cess()/func();
printf(">he (alue %f pr%cess is .d V;n "/pr%cess(func/+/7));
}
pr%cess(pf/(al1/(al2)
int (Lpf) ();
int (al1/(al2;
{
return((Lpf) ((al1/(al2));
}
Answer:
>he (alue if pr%cess is - V
Explanation:
>he functi%n 2pr%cess2 has + parameters 0 1/ a p%inter t% an%ther functi%n 2 and +/ inte)ers.
hen this functi%n is in(%ked fr%m main/ the f%ll%win) substituti%ns f%r f%rmal parameters
take place: func f%r pf/ + f%r (al1 and 7 f%r (al2. >his functi%n returns the result %f the
%perati%n perf%rmed b$ the functi%n 2func2. >he functi%n func has tw% inte)er parameters.
>he f%rmal parameters are substituted as + f%r a and 7 f%r b. since + is n%t e?ual t% 7/ a==b
returns -. theref%re the functi%n returns - which in turn is returned b$ the functi%n
2pr%cess2.
111. (%id main()
{
static int i=4;
if(00i){
main();
printf(".d "/i);
}
}
Answer:
- - - -
Explanation:
>he (ariable "i" is declared as static/ hence mem%r$ f%r i will be all%cated f%r %nl$ %nce/ as
it enc%unters the statement. >he functi%n main() will be called recursi(el$ unless i bec%mes
e?ual t% -/ and since main() is recursi(el$ called/ s% the (alue %f static i i.e./ - will be printed
e(er$ time the c%ntr%l is returned.
114. (%id main()
{
int k=ret(si3e%f(fl%at));
printf(";n here (alue is .d"/++k);
}
int ret(int ret)
{
ret += 2.4;
return(ret);
}
Answer:
Kere (alue is 6
Explanation:
>he int ret(int ret)/ ie./ the functi%n name and the ar)ument name can be the same.
Nirstl$/ the functi%n ret() is called in which the si3e%f(fl%at) ie./ 1 is passed/ after the first
e!pressi%n the (alue in ret will be 7/ as ret is inte)er hence the (alue st%red in ret will ha(e
implicit t$pe c%n(ersi%n fr%m fl%at t% int. >he ret is returned in main() it is printed after
and preincrement.
117. (%id main()
{
char aST="12+14;-";
int i=strlen(a);
printf("here in + .d;n"/++i);
}
Answer:
here in + 7
Explanation:
>he char arra$ 2a2 will h%ld the initiali3ed strin)/ wh%se len)th will be c%unted fr%m - till
the null character. Kence the 2=2 will h%ld the (alue e?ual t% 4/ after the pre0increment in
the printf statement/ the 7 will be printed.
116. (%id main()
{
unsi)ned )i(eit=01;
int )%tit;
printf(".u "/++)i(eit);
printf(".u ;n"/)%tit=00)i(eit);
}
Answer:
- 744+4
118. (%id main()
{
int i;
char aST=";-";
if(printf(".s;n"/a))
printf("5k here ;n");
else
printf("N%r)et it;n");
}
Answer:
5k here
Explanation:
Mrintf will return h%w man$ characters it prints. Kence printin) a null character returns 1
which makes the if statement true/ thus "5k here" is printed.
11H. (%id main()
{
(%id L(;
int inte)er=2;
int Li=Cinte)er;
(=i;
printf(".d"/(intL)L();
}
Answer:
&%mpiler 9rr%r: e cann%t appl$ indirecti%n %n t$pe (%idL.
Explanation:
[%id p%inter is a )eneric p%inter t$pe. *% p%inter arithmetic can be d%ne %n it. [%id p%inters
are n%rmall$ used f%r/
1. Massin) )eneric p%inters t% functi%ns and returnin) such p%inters.
2. 's a intermediate p%inter t$pe.
+. Qsed when the e!act p%inter t$pe will be kn%wn at a later p%int %f time.
12-. (%id main()
{
int i=i++/B=B++/k=k++;
printf(\.d.d.d]/i/B/k);
}
Answer:
Jarba)e (alues.
Explanation:
'n identifier is a(ailable t% use in pr%)ram c%de fr%m the p%int %f its declarati%n. #%
e!pressi%ns such as i = i++ are (alid statements. >he i/ B and k are aut%matic (ariables and s%
the$ c%ntain s%me )arba)e (alue. Jarba)e in is )arba)e %ut (J=J5).
121. (%id main()
{
static int i=i++/ B=B++/ k=k++;
printf(\i = .d B = .d k = .d]/ i/ B/ k);
}
Answer:
i = 1 B = 1 k = 1
Explanation:
#ince static (ariables are initiali3ed t% 3er% b$ default.
121. (%id main()
{
while(1){
if(printf(".d"/printf(".d")))
break;
else
c%ntinue;
}
}
Answer:
Jarba)e (alues
Explanation:
>he inner printf e!ecutes first t% print s%me )arba)e (alue. >he printf returns n%. %f
characters printed and this (alue als% cann%t be predicted. #till the %uter printf prints
s%methin) and s% returns a n%n03er% (alue. #% it enc%unters the break statement and c%mes
%ut %f the while statement.
122. main()
{
unsi)ned int i=1-;
while(i00,=-)
printf(".u "/i);
}
Answer:
H 8 6 7 4 1 + 2 1 - 744+4 744+1`..
Explanation:
#ince i is an unsi)ned inte)er it can ne(er bec%me ne)ati(e. #% the e!pressi%n i00 ,=- will
alwa$s be true/ leadin) t% an infinite l%%p.
122. main()
{
int aS1-T;
printf(".d"/La+10La++);
}
Answer:
1
Explanation:
La and 0La cancels %ut. >he result is as simple as 1 + + = 1 V
12+. Wdefine pr%d(a/b) aLb
main()
{
int !=+/$=1;
printf(".d"/pr%d(!+2/$01));
}
Answer:
1-
Explanation:
>he macr% e!pands and e(aluates t% as:
!+2L$01 =, !+(2L$)01 =, 1-
121. main()
{
unsi)ned int i=74---;
while(i++V=-);
printf(".d"/i);
}
Answer:
1
Explanation:
*%te the semic%l%n after the while statement. hen the (alue %f i bec%mes - it c%mes %ut
%f while l%%p. Gue t% p%st0increment %n i the (alue %f i while printin) is 1.
124. main()
{
int i=-;
while(+(+i00)V=-)
i0=i++;
printf(".d"/i);
}
Answer:
01
Explanation:
Qnar$ + is the %nl$ dumm$ %perat%r in &. #% it has n% effect %n the e!pressi%n and n%w the
while l%%p is/ while(i00V=-) which is false and s% breaks %ut %f while l%%p. >he (alue Z1 is
printed due t% the p%st0decrement %perat%r.
127. main()
{
fl%at f=4/)=1-;
enum{i=1-/B=2-/k=4-};
printf(".d;n"/++k);
printf(".f;n"/fFF2);
printf(".lf;n"/f.));
printf(".lf;n"/fm%d(f/)));
}
Answer:
Rine n% 4: 9rr%r: R(alue re?uired
Rine n% 7: &ann%t appl$ leftshift t% fl%at
Rine n% 6: &ann%t appl$ m%d t% fl%at
Explanation:
9numerati%n c%nstants cann%t be m%dified/ s% $%u cann%t appl$ ++.
<it0wise %perat%rs and . %perat%rs cann%t be applied %n fl%at (alues.
fm%d() is t% find the m%dulus (alues f%r fl%ats as . %perat%r is f%r ints.
126. main()
{
int i=1-;
(%id pascal f(int/int/int);
f(i++/i++/i++);
printf(" .d"/i);
}
(%id pascal f(inte)er :i/inte)er:B/inte)er :k)
{
write(i/B/k);
}
Answer:
&%mpiler err%r: unkn%wn t$pe inte)er
&%mpiler err%r: undeclared functi%n write
Explanation:
Mascal ke$w%rd d%esnYt mean that pascal c%de can be used. =t means that the functi%n
f%ll%ws Mascal ar)ument passin) mechanism in callin) the functi%ns.
128. (%id pascal f(int i/int B/int k)
{
printf(\.d .d .d]/i/ B/ k);
}
(%id cdecl f(int i/int B/int k)
{
printf(\.d .d .d]/i/ B/ k);
}
main()
{
int i=1-;
f(i++/i++/i++);
printf(" .d;n"/i);
i=1-;
f(i++/i++/i++);
printf(" .d"/i);
}
Answer:
1- 11 12 1+
12 11 1- 1+
Explanation:
Mascal ar)ument passin) mechanism f%rces the ar)uments t% be called fr%m left t% ri)ht.
cdecl is the n%rmal & ar)ument passin) mechanism where the ar)uments are passed fr%m
ri)ht t% left.
12H. hat is the %utput %f the pr%)ram )i(en bel%w"
main()
{
si)ned char i=-;
f%r(;i,=-;i++) ;
printf(".d;n"/i);
}
Answer
0128
Explanation
*%tice the semic%l%n at the end %f the f%r l%%p. >he initial (alue %f the i is set t% -. >he
inner l%%p e!ecutes t% increment the (alue fr%m - t% 126 (the p%siti(e ran)e %f char) and
then it r%tates t% the ne)ati(e (alue %f 0128. >he c%nditi%n in the f%r l%%p fails and s%
c%mes %ut %f the f%r l%%p. =t prints the current (alue %f i that is 0128.
1+-. main()
{
unsi)ned char i=-;
f%r(;i,=-;i++) ;
printf(".d;n"/i);
}
Answer
infinite l%%p
Explanation
>he difference between the pre(i%us ?uesti%n and this %ne is that the char is declared t%
be unsi)ned. #% the i++ can ne(er $ield ne)ati(e (alue and i,=- ne(er bec%mes false s% that
it can c%me %ut %f the f%r l%%p.
1+1. main()
{
char i=-;
f%r(;i,=-;i++) ;
printf(".d;n"/i);
}
Answer:
<eha(i%r is implementati%n dependent.
Explanation:
>he detail if the char is si)nedIunsi)ned b$ default is implementati%n dependent. =f the
implementati%n treats the char t% be si)ned b$ default the pr%)ram will print Z128 and
terminate. 5n the %ther hand if it c%nsiders char t% be unsi)ned b$ default/ it )%es t%
infinite l%%p.
Rule: P%u can write pr%)rams that ha(e implementati%n dependent beha(i%r. <ut d%nYt write
pr%)rams that depend %n such beha(i%r.
1+2. =s the f%ll%win) statement a declarati%nIdefiniti%n" Nind what d%es it mean"
int (L!)S1-T;
Answer
Gefiniti%n. ! is a p%inter t% arra$ %f(si3e 1-) inte)ers.
'ppl$ cl%ck0wise rule t% find the meanin) %f this definiti%n.
1++. hat is the %utput f%r the pr%)ram )i(en bel%w"
t$pedef enum err%r>$pe{warnin)/ err%r/ e!cepti%n/}err%r;
main()
{
err%r )1;
)1=1;
printf(".d"/)1);
}
Answer
&%mpiler err%r: @ultiple declarati%n f%r err%r
Explanation
>he name err%r is used in the tw% meanin)s. 5ne means that it is a enumerat%r c%nstant
with (alue 1. >he an%ther use is that it is a t$pe name (due t% t$pedef) f%r enum err%r>$pe.
Ji(en a situati%n the c%mpiler cann%t distin)uish the meanin) %f err%r t% kn%w in what sense
the err%r is used:
err%r )1;
)1=err%r;
II which err%r it refers in each case"
hen the c%mpiler can distin)uish between usa)es then it will n%t issue err%r (in
pure technical terms/ names can %nl$ be %(erl%aded in different namespaces).
Note: the e!tra c%mma in the declarati%n/ enum err%r>$pe{warnin)/ err%r/ e!cepti%n/} is n%t
an err%r. 'n e!tra c%mma is (alid and is pr%(ided Bust f%r pr%)rammerYs c%n(enience.
1+1. t$pedef struct err%r{int warnin)/ err%r/ e!cepti%n;}err%r;
main()
{
err%r )1;
)1.err%r =1;
printf(".d"/)1.err%r);
}
Answer
1
Explanation
>he three usa)es %f name err%rs can be distin)uishable b$ the c%mpiler at an$ instance/ s%
(alid (the$ are in different namespaces).
>$pedef struct err%r{int warnin)/ err%r/ e!cepti%n;}err%r;
>his err%r can be used %nl$ b$ precedin) the err%r b$ struct ka$w%rd as in:
struct err%r s%me9rr%r;
t$pedef struct err%r{int warnin)/ err%r/ e!cepti%n;}err%r;
>his can be used %nl$ after . (d%t) %r 0, (arr%w) %perat%r preceded b$ the (ariable name as
in :
)1.err%r =1;
printf(".d"/)1.err%r);
t$pedef struct err%r{int warnin)/ err%r/ e!cepti%n;}err%r;
>his can be used t% define (ariables with%ut usin) the precedin) struct ke$w%rd as in:
err%r )1;
#ince the c%mpiler can perfectl$ distin)uish between these three usa)es/ it is perfectl$
le)al and (alid.
Note >his c%de is )i(en here t% Bust e!plain the c%ncept behind. =n real pr%)rammin) d%nYt
use such %(erl%adin) %f names. =t reduces the readabilit$ %f the c%de. M%ssible d%esnYt mean
that we sh%uld use itV
1+4. Wifdef s%methin)
int s%me=-;
Wendif
main()
{
int thin) = -;
printf(".d .d;n"/ s%me /thin));
}
Answer:
&%mpiler err%r: undefined s$mb%l s%me
Explanation:
>his is a (er$ simple e!ample f%r c%nditi%nal c%mpilati%n. >he name s%methin) is n%t alread$
kn%wn t% the c%mpiler makin) the declarati%n
int s%me = -;
effecti(el$ rem%(ed fr%m the s%urce c%de.
1+7. Wif s%methin) == -
int s%me=-;
Wendif
main()
{
int thin) = -;
printf(".d .d;n"/ s%me /thin));
}
Answer
- -
Explanation
>his c%de is t% sh%w that prepr%cess%r e!pressi%ns are n%t the same as the %rdinar$
e!pressi%ns. =f a name is n%t kn%wn the prepr%cess%r treats it t% be e?ual t% 3er%.
1+6. hat is the %utput f%r the f%ll%win) pr%)ram"
main()
{
int arr2GS+TS+T;
printf(".d;n"/ ((arr2G==L arr2G)CC(L arr2G == arr2GS-T)) );
}
Answer
1
Explanation
>his is due t% the cl%se relati%n between the arra$s and p%inters. * dimensi%nal arra$s are
made up %f (*01) dimensi%nal arra$s.
arr2G is made up %f a + sin)le arra$s that c%ntains + inte)ers each .
>he name arr2G refers t% the be)innin) %f all the + arra$s. Larr2G refers t% the start %f
the first 1G arra$ (%f + inte)ers) that is the same address as arr2G. #% the e!pressi%n
(arr2G == Larr2G) is true (1).
#imilarl$/ Larr2G is n%thin) but L(arr2G + -)/ addin) a 3er% d%esnYt chan)e the
(alueImeanin). ')ain arr2GS-T is the an%ther wa$ %f tellin) L(arr2G + -). #% the e!pressi%n
(L(arr2G + -) == arr2GS-T) is true (1). #ince b%th parts %f the e!pressi%n e(aluates t% true
the result is true(1) and the same is printed.
1+8. (%id main()
{
if(E- == (unsi)ned int)01)
printf(\P%u can answer this if $%u kn%w h%w (alues are represented in
mem%r$]);
}
Answer
P%u can answer this if $%u kn%w h%w (alues are represented in mem%r$
Explanation
E (tilde %perat%r %r bit0wise ne)ati%n %perat%r) %perates %n - t% pr%duce all %nes t% fill the
space f%r an inte)er. Z1 is represented in unsi)ned (alue as all 1Ys and s% b%th are e?ual.
1+H. int swap(int La/int Lb)
{
La=La+Lb;
Lb=La0Lb;
La=La0Lb;
}
main()
{
int !=1-/$=2-;
swap(C!/C$);
printf("!= .d $ = .d;n"/!/$);
}
Answer
! = 2- $ = 1-
Explanation
>his is %ne wa$ %f swappin) tw% (alues. #imple checkin) will help understand this.
11-. main()
{
char Lp = \a$?m];
printf(\.c]/++L(p++));
}
Answer:
3
111. main()
{
int i=4;
printf(".d"/++i++);
}
Answer:
&%mpiler err%r: R(alue re?uired in functi%n main
Explanation:
++i $ields an r(alue. N%r p%stfi! ++ t% %perate an l(alue is re?uired.
112. main()
{
char Lp = \a$?m];
char c;
c = ++Lp++;
printf(\.c]/c);
}
Answer:
b
Explanation:
>here is n% difference between the e!pressi%n ++L(p++) and ++Lp++. Marenthesis Bust w%rks
as a (isual clue f%r the reader t% see which e!pressi%n is first e(aluated.
11+. main()
{
int i=4;
printf(\.d]/i=++i ==7);
}
Answer:
1
Explanation:
>he e!pressi%n can be treated as i = (++i==7)/ because == is %f hi)her precedence than =
%perat%r. =n the inner e!pressi%n/ ++i is e?ual t% 7 $ieldin) true(1). Kence the result.
111. main()
{
char pS T=".d;n";
pS1T = 2c2;
printf(p/74);
}
Answer:
'
Explanation:
Gue t% the assi)nment pS1T = XcY the strin) bec%mes/ \.c;n]. #ince this strin) bec%mes the
f%rmat strin) f%r printf and '#&== (alue %f 74 is X'Y/ the same )ets printed.
114. (%id ( L abc( int/ (%id ( Ldef) () ) ) ();
Answer::
abc is a ptr t% a functi%n which takes 2 parameters .(a). an inte)er (ariable.(b). a p%inter t%
a functi%n which returns (%id. >he return t$pe %f the functi%n is (%id.
Explanation:
'ppl$ the cl%ck0wise rule t% find the result.
117. main()
{
while (strcmp(\s%me]/]s%me;-]))
printf(\#trin)s are n%t e?ual;n]);
}
Answer:
*% %utput
Explanation:
9ndin) the strin) c%nstant with ;- e!plicitl$ makes n% difference. #% \s%me] and \s%me;-]
are e?ui(alent. #%/ strcmp returns - (false) hence breakin) %ut %f the while l%%p.
116. main()
{
char str1ST = {XsY/Y%Y/YmY/YeY};
char str2ST = {XsY/Y%Y/YmY/YeY/Y;-Y};
while (strcmp(str1/str2))
printf(\#trin)s are n%t e?ual;n]);
}
Answer:
\#trin)s are n%t e?ual]
\#trin)s are n%t e?ual]
`.
Explanation:
=f a strin) c%nstant is initiali3ed e!plicitl$ with characters/ X;-Y is n%t appended
aut%maticall$ t% the strin). #ince str1 d%esnYt ha(e null terminati%n/ it treats whate(er the
(alues that are in the f%ll%win) p%siti%ns as part %f the strin) until it rand%ml$ reaches a
X;-Y. #% str1 and str2 are n%t the same/ hence the result.
118. main()
{
int i = +;
f%r (;i++=-;) printf(\.d]/i);
}
Answer:
&%mpiler 9rr%r: R(alue re?uired.
Explanation:
's we kn%w that increment %perat%rs return r(alues and hence it cann%t appear %n the left
hand side %f an assi)nment %perati%n.
11H. (%id main()
{
int Lmptr/ Lcptr;
mptr = (intL)mall%c(si3e%f(int));
printf(\.d]/Lmptr);
int Lcptr = (intL)call%c(si3e%f(int)/1);
printf(\.d]/Lcptr);
}
Answer:
)arba)e0(alue -
Explanation:
>he mem%r$ space all%cated b$ mall%c is uninitiali3ed/ whereas call%c returns the all%cated
mem%r$ space initiali3ed t% 3er%s.
14-. (%id main()
{
static int i;
while(iF=1-)
(i,2)"i++:i00;
printf(\.d]/ i);
}
Answer:
+2676
Explanation:
#ince i is static it is initiali3ed t% -. =nside the while l%%p the c%nditi%nal %perat%r e(aluates
t% false/ e!ecutin) i00. >his c%ntinues till the inte)er (alue r%tates t% p%siti(e (alue (+2676).
>he while c%nditi%n bec%mes false and hence/ c%mes %ut %f the while l%%p/ printin) the i
(alue.
141. main()
{
int i=1-/B=2-;
B = i/ B"(i/B)"i:B:B;
printf(".d .d"/i/B);
}
Answer:
1- 1-
Explanation:
>he >ernar$ %perat%r ( " : ) is e?ui(alent f%r if0then0else statement. #% the ?uesti%n can be
written as:
if(i/B)
{
if(i/B)
B = i;
else
B = B;
}
else
B = B;
142. 1. c%nst char La;
2. charL c%nst a;
+. char c%nst La;
0Gifferentiate the ab%(e declarati%ns.
Answer:
1. 2c%nst2 applies t% char L rather than 2a2 ( p%inter t% a c%nstant char )
La=2N2 : ille)al
a="Ki" : le)al
2. 2c%nst2 applies t% 2a2 rather than t% the (alue %f a (c%nstant p%inter t% char )
La=2N2 : le)al
a="Ki" : ille)al
+. #ame as 1.
14+. main()
{
int i=4/B=1-;
i=iC=BCC1-;
printf(".d .d"/i/B);
}
Answer:
1 1-
Explanation:
>he e!pressi%n can be written as i=(iC=(BCC1-)); >he inner e!pressi%n (BCC1-) e(aluates t% 1
because B==1-. i is 4. i = 4C1 is 1. Kence the result.
141. main()
{
re)ister int a=2;
printf("'ddress %f a = .d"/Ca);
printf("[alue %f a = .d"/a);
}
Answer:
&%mpier 9rr%r: 2C2 %n re)ister (ariable
Rule to Rememer:
C (address %f ) %perat%r cann%t be applied %n re)ister (ariables.
144. main()
{
fl%at i=1.4;
switch(i)
{
case 1: printf("1");
case 2: printf("2");
default : printf("-");
}
}
Answer:
&%mpiler 9rr%r: switch e!pressi%n n%t inte)ral
Explanation:
#witch statements can be applied %nl$ t% inte)ral t$pes.
147. main()
{
e!tern i;
printf(".d;n"/i);
{
int i=2-;
printf(".d;n"/i);
}
}
Answer:
Rinker 9rr%r : Qnres%l(ed e!ternal s$mb%l i
Explanation:
>he identifier i is a(ailable in the inner bl%ck and s% usin) e!tern has n% use in res%l(in) it.
146. main()
{
int a=2/Lf1/Lf2;
f1=f2=Ca;
Lf2+=Lf2+=a+=2.4;
printf(";n.d .d .d"/a/Lf1/Lf2);
}
Answer:
17 17 17
Explanation:
f1 and f2 b%th refer t% the same mem%r$ l%cati%n a. #% chan)es thr%u)h f1 and f2
ultimatel$ affects %nl$ the (alue %f a.
148. main()
{
char Lp="J55G";
char aS T="J55G";
printf(";n si3e%f(p) = .d/ si3e%f(Lp) = .d/ strlen(p) = .d"/ si3e%f(p)/ si3e%f(Lp)/
strlen(p));
printf(";n si3e%f(a) = .d/ strlen(a) = .d"/ si3e%f(a)/ strlen(a));
}
Answer:
si3e%f(p) = 2/ si3e%f(Lp) = 1/ strlen(p) = 1 si3e%f(a) = 4/ strlen(a) = 1
Explanation:
si3e%f(p) =, si3e%f(charL) =, 2 si3e%f(Lp) =, si3e%f(char) =, 1
#imilarl$/ si3e%f(a) =, si3e %f the character arra$ =, 4
hen si3e%f %perat%r is applied t% an arra$ it returns the si3e%f the arra$ and it is n%t the
same as the si3e%f the p%inter (ariable. Kere the si3e%f(a) where a is the character arra$
and the si3e %f the arra$ is 4 because the space necessar$ f%r the terminatin) *QRR
character sh%uld als% be taken int% acc%unt.
14H. Wdefine G=@( arra$/ t$pe) si3e%f(arra$)Isi3e%f(t$pe)
main()
{
int arrS1-T;
printf(\>he dimensi%n %f the arra$ is .d]/ G=@(arr/ int));
}
Answer:
1-
Explanation:
>he si3e %f inte)er arra$ %f 1- elements is 1- L si3e%f(int). >he macr% e!pands t%
si3e%f(arr)Isi3e%f(int) =, 1- L si3e%f(int) I si3e%f(int) =, 1-.
17-. int G=@(int arra$ST)
{
return si3e%f(arra$)Isi3e%f(int );
}
main()
{
int arrS1-T;
printf(\>he dimensi%n %f the arra$ is .d]/ G=@(arr));
}
Answer:
1
Explanation:
'rra$s cann%t be passed t% functi%ns as ar)uments and %nl$ the p%inters can be passed. #%
the ar)ument is e?ui(alent t% int L arra$ (this is %ne %f the (er$ few places where ST and L
usa)e are e?ui(alent). >he return statement bec%mes/ si3e%f(int L)I si3e%f(int) that happens
t% be e?ual in this case.
171. main()
{
static int aS+TS+T={1/2/+/1/4/7/6/8/H};
int i/B;
static LpST={a/a+1/a+2};
f%r(i=-;iF+;i++)
{
f%r(B=-;BF+;B++)
printf(".d;t.d;t.d;t.d;n"/L(L(p+i)+B)/
L(L(B+p)+i)/L(L(i+p)+B)/L(L(p+B)+i));
}
}
Answer:
1 1 1 1
2 1 2 1
+ 6 + 6
1 2 1 2
4 4 4 4
7 8 7 8
6 + 6 +
8 7 8 7
H H H H
Explanation:
L(L(p+i)+B) is e?ui(alent t% pSiTSBT.
172. main()
{
(%id swap();
int !=1-/$=8;
swap(C!/C$);
printf("!=.d $=.d"/!/$);
}
(%id swap(int La/ int Lb)
{
La _= Lb/ Lb _= La/ La _= Lb;
}
Answer:
!=8 $=1-
Explanation:
Qsin) _ like this is a wa$ t% swap tw% (ariables with%ut usin) a temp%rar$ (ariable and that
t%% in a sin)le statement.
=nside main()/ (%id swap(); means that swap is a functi%n that ma$ take an$ number %f
ar)uments (n%t n% ar)uments) and returns n%thin). #% this d%esnYt issue a c%mpiler err%r b$
the call swap(C!/C$); that has tw% ar)uments.
>his c%n(enti%n is hist%ricall$ due t% pre0'*#= st$le (referred t% as ^erni)han and :itchie
st$le) st$le %f functi%n declarati%n. =n that st$le/ the swap functi%n will be defined as
f%ll%ws/
(%id swap()
int La/ int Lb
{
La _= Lb/ Lb _= La/ La _= Lb;
}
where the ar)uments f%ll%w the (). #% naturall$ the declarati%n f%r swap will l%%k like/ (%id
swap() which means the swap can take an$ number %f ar)uments.
17+. main()
{
int i = 246;
int LiMtr = Ci;
printf(".d .d"/ L((charL)iMtr)/ L((charL)iMtr+1) );
}
Answer:
1 1
Explanation:
>he inte)er (alue 246 is st%red in the mem%r$ as/ -------1 -------1/ s% the indi(idual
b$tes are taken b$ castin) it t% char L and )et printed.
171. main()
{
int i = 248;
int LiMtr = Ci;
printf(".d .d"/ L((charL)iMtr)/ L((charL)iMtr+1) );
}
Answer:
2 1
Explanation:
>he inte)er (alue 246 can be represented in binar$ as/ -------1 -------1. :emember
that the =*>9R machines are Xsmall0endianY machines. #mall0endian means that the l%wer
%rder b$tes are st%red in the hi)her mem%r$ addresses and the hi)her %rder b$tes are
st%red in l%wer addresses. >he inte)er (alue 248 is st%red in mem%r$ as: -------1
------1-.
174. main()
{
int i=+--;
char Lptr = Ci;
L++ptr=2;
printf(".d"/i);
}
Answer:
447
Explanation:
>he inte)er (alue +-- in binar$ n%tati%n is: -------1 --1-11--. =t is st%red in mem%r$
(small0endian) as: --1-11-- -------1. :esult %f the e!pressi%n L++ptr = 2 makes the
mem%r$ representati%n as: --1-11-- ------1-. #% the inte)er c%rresp%ndin) t% it is
------1- --1-11-- =, 447.
177. Winclude Fstdi%.h,
main()
{
char L str = "hell%";
char L ptr = str;
char least = 126;
while (Lptr++)
least = (LptrFleast ) "Lptr :least;
printf(".d"/least);
}
Answer:
-
Explanation:
'fter XptrY reaches the end %f the strin) the (alue p%inted b$ XstrY is X;-Y. #% the (alue %f
XstrY is less than that %f XleastY. #% the (alue %f XleastY finall$ is -.
176. Geclare an arra$ %f * p%inters t% functi%ns returnin) p%inters t% functi%ns returnin)
p%inters t% characters"
Answer:
(charL(L)( )) (LptrS*T)( );
178. main()
{
struct student
{
char nameS+-T;
struct date d%b;
}stud;
struct date
{
int da$/m%nth/$ear;
};
scanf(".s.d.d.d"/stud.r%lln%/Cstudent.d%b.da$/ student.d%b.m%nth/
Cstudent.d%b.$ear);
}
Answer:
&%mpiler 9rr%r: Qndefined structure date
Explanation:
=nside the struct definiti%n %f XstudentY the member %f t$pe struct date is )i(en. >he
c%mpiler d%esnYt ha(e the definiti%n %f date structure (f%rward reference is n%t all%wed in
& in this case) s% it issues an err%r.
17H. main()
{
struct date;
struct student
{
char nameS+-T;
struct date d%b;
}stud;
struct date
{
int da$/m%nth/$ear;
};
scanf(".s.d.d.d"/ stud.r%lln%/ Cstudent.d%b.da$/ Cstudent.d%b.m%nth/
Cstudent.d%b.$ear);
}
Answer:
&%mpiler 9rr%r: Qndefined structure date
Explanation:
5nl$ declarati%n %f struct date is a(ailable inside the structure definiti%n %f XstudentY but
t% ha(e a (ariable %f t$pe struct date the definiti%n %f the structure is re?uired.
16-. >here were 1- rec%rds st%red in \s%mefile.dat] but the f%ll%win) pr%)ram printed 11
names. hat went wr%n)"
(%id main()
{
struct student
{
char nameS+-T/ r%lln%S7T;
}stud;
N=R9 Lfp = f%pen(\s%mefile.dat]/]r]);
while(Vfe%f(fp))
{
fread(Cstud/ si3e%f(stud)/ 1 / fp);
puts(stud.name);
}
}
Explanation:
fread reads 1- rec%rds and prints the names successfull$. =t will return 95N %nl$ when
fread tries t% read an%ther rec%rd and fails readin) 95N (and returnin) 95N). #% it prints
the last rec%rd a)ain. 'fter this %nl$ the c%nditi%n fe%f(fp) bec%mes false/ hence c%mes %ut
%f the while l%%p.
161. =s there an$ difference between the tw% declarati%ns"
1 1. int f%%(int LarrST) and
2 2. int f%%(int LarrS2T)
Answer:
*%
Explanation:
Nuncti%ns can %nl$ pass p%inters and n%t arra$s. >he numbers that are all%wed inside the ST
is Bust f%r m%re readabilit$. #% there is n% difference between the tw% declarati%ns.
162. hat is the subtle err%r in the f%ll%win) c%de se)ment"
(%id fun(int n/ int arrST)
{
int Lp=-;
int i=-;
while(i++Fn)
p = CarrSiT;
Lp = -;
}
Answer ) Explanation:
=f the b%d$ %f the l%%p ne(er e!ecutes p is assi)ned n% address. #% p remains *QRR where
Lp =- ma$ result in pr%blem (ma$ rise t% runtime err%r \*QRR p%inter assi)nment] and
terminate the pr%)ram).
16+. hat is wr%n) with the f%ll%win) c%de"
int Lf%%()
{
int Ls = mall%c(si3e%f(int)1--);
assert(s V= *QRR);
return s;
}
Answer ) Explanation:
assert macr% sh%uld be used f%r debu))in) and findin) %ut bu)s. >he check s V= *QRR is f%r
err%rIe!cepti%n handlin) and f%r that assert sh%uldnYt be used. ' plain if and the
c%rresp%ndin) remed$ statement has t% be )i(en.
161. hat is the hidden bu) with the f%ll%win) statement"
assert((al++ V= -);
Answer ) Explanation:
'ssert macr% is used f%r debu))in) and rem%(ed in release (ersi%n. =n assert/ the
e!pressi%n in(%l(es side0effects. #% the beha(i%r %f the c%de bec%mes different in case %f
debu) (ersi%n and the release (ersi%n thus leadin) t% a subtle bu).
Rule to Rememer: G%nYt use e!pressi%ns that ha(e side0effects in assert statements.
164. (%id main()
{
int Li = -!1--; II i p%ints t% the address 1--
Li = -; II set the (alue %f mem%r$ l%cati%n p%inted b$ i;
}
Answer:
Qndefined beha(i%r
Explanation:
>he sec%nd statement results in undefined beha(i%r because it p%ints t% s%me l%cati%n
wh%se (alue ma$ n%t be a(ailable f%r m%dificati%n. >his t$pe %f p%inter in which the n%n0
a(ailabilit$ %f the implementati%n %f the referenced l%cati%n is kn%wn as 2inc%mplete t$pe2.
167. Wdefine assert(c%nd) if(V(c%nd)) ;
(fprintf(stderr/ "asserti%n failed: .s/ file .s/ line .d ;n"/Wc%nd/;
UUN=R9UU/UUR=*9UU)/ ab%rt())
(%id main()
{
int i = 1-;
if(i==-)
assert(i F 1--);
else
printf(">his statement bec%mes else f%r if in assert macr%");
}
Answer:
*% %utput
Explanation:
>he else part in which the printf is there bec%mes the else f%r if in the assert macr%.
Kence n%thin) is printed.
>he s%luti%n is t% use c%nditi%nal %perat%r instead %f if statement/
Wdefine assert(c%nd) ((c%nd)"(-): (fprintf (stderr/ "asserti%n failed: ; .s/ file .s/ line
.d ;n"/Wc%nd/ UUN=R9UU/UUR=*9UU)/ ab%rt()))
Note:
K%we(er this pr%blem %f \matchin) with nearest else] cann%t be s%l(ed b$ the usual meth%d
%f placin) the if statement inside a bl%ck like this/
Wdefine assert(c%nd) { ;
if(V(c%nd)) ;
(fprintf(stderr/ "asserti%n failed: .s/ file .s/ line .d ;n"/Wc%nd/;
UUN=R9UU/UUR=*9UU)/ ab%rt()) ;
}
166. =s the f%ll%win) c%de le)al"
struct a
{
int !;
struct a b;
}
Answer:
*%
Explanation:
=t is n%t le)al f%r a structure t% c%ntain a member that is %f the same t$pe as in this case.
<ecause this will cause the structure declarati%n t% be recursi(e with%ut end.
168. =s the f%ll%win) c%de le)al"
struct a
{
int !;
struct a Lb;
}
Answer:
Pes.
Explanation:
Lb is a p%inter t% t$pe struct a and s% is le)al. >he c%mpiler kn%ws/ the si3e %f the p%inter
t% a structure e(en bef%re the si3e %f the structure is determined(as $%u kn%w the p%inter
t% an$ t$pe is %f same si3e). >his t$pe %f structures is kn%wn as Xself0referencin)Y structure.
16H. =s the f%ll%win) c%de le)al"
t$pedef struct a
{
int !;
a>$pe Lb;
}a>$pe
Answer:
*%
Explanation:
>he t$pename a>$pe is n%t kn%wn at the p%int %f declarin) the structure (f%rward
references are n%t made f%r t$pedefs).
18-. =s the f%ll%win) c%de le)al"
t$pedef struct a a>$pe;
struct a
{
int !;
a>$pe Lb;
};
Answer:
Pes
Explanation:
>he t$pename a>$pe is kn%wn at the p%int %f declarin) the structure/ because it is alread$
t$pedefined.
181. =s the f%ll%win) c%de le)al"
(%id main()
{
t$pedef struct a a>$pe;
a>$pe s%me[ariable;
struct a
{
int !;
a>$pe Lb;
};
}
Answer:
*%
Explanation:
hen the declarati%n/
t$pedef struct a a>$pe;
is enc%untered b%d$ %f struct a is n%t kn%wn. >his is kn%wn as Xinc%mplete t$pesY.
182. (%id main()
{
printf(\si3e%f ((%id L) = .d ;n\/ si3e%f( (%id L));
printf(\si3e%f (int L) = .d ;n]/ si3e%f(int L));
printf(\si3e%f (d%uble L) = .d ;n]/ si3e%f(d%uble L));
printf(\si3e%f(struct unkn%wn L) = .d ;n]/ si3e%f(struct unkn%wn L));
}
Answer :
si3e%f ((%id L) = 2
si3e%f (int L) = 2
si3e%f (d%uble L) = 2
si3e%f(struct unkn%wn L) = 2
Explanation:
>he p%inter t% an$ t$pe is %f same si3e.
18+. char input#trin)S1--T = {-};
>% )et strin) input fr%m the ke$b%ard which %ne %f the f%ll%win) is better"
1) )ets(input#trin))
2) f)ets(input#trin)/ si3e%f(input#trin))/ fp)
Answer ) Explanation:
>he sec%nd %ne is better because )ets(input#trin)) d%esn2t kn%w the si3e %f the strin)
passed and s%/ if a (er$ bi) input (here/ m%re than 1-- chars) the characters will be written
past the input strin). hen f)ets is used with stdin perf%rms the same %perati%n as )ets
but is safe.
181. hich (ersi%n d% $%u prefer %f the f%ll%win) tw%"
1) printf(\.s]/str); II %r the m%re curt %ne
2) printf(str);
Answer ) Explanation:
Mrefer the first %ne. =f the str c%ntains an$ f%rmat characters like .d then it will result in
a subtle bu).
184. (%id main()
{
int i=1-/ B=2;
int Lip= Ci/ LBp = CB;
int k = LipILBp;
printf(\.d]/k);
}
Answer:
&%mpiler 9rr%r: \Qne!pected end %f file in c%mment started in line 4].
Explanation:
>he pr%)rammer intended t% di(ide tw% inte)ers/ but b$ the \ma!imum munch] rule/ the
c%mpiler treats the %perat%r se?uence I and L as IL which happens t% be the startin) %f
c%mment. >% f%rce what is intended b$ the pr%)rammer/
int k = LipI LBp;
II )i(e space e!plicitl$ separatin) I and L
II%r
int k = LipI(LBp);
II put braces t% f%rce the intenti%n
will s%l(e the pr%blem.
187. (%id main()
{
char ch;
f%r(ch=-;chF=126;ch++)
printf(\.c .d ;n\/ ch/ ch);
}
Answer:
=mplementati%n dependent
Explanation:
>he char t$pe ma$ be si)ned %r unsi)ned b$ default. =f it is si)ned then ch++ is e!ecuted
after ch reaches 126 and r%tates back t% 0128. >hus ch is alwa$s smaller than 126.
186. =s this c%de le)al"
int Lptr;
ptr = (int L) -!1--;
Answer:
Pes
Explanation:
>he p%inter ptr will p%int at the inte)er in the mem%r$ l%cati%n -!1--.
188. main()
{
char aS1T="K9RR5";
printf(".s"/a);
}
Answer:
&%mpiler err%r: >%% man$ initiali3ers
Explanation:
>he arra$ a is %f si3e 1 but the strin) c%nstant re?uires 7 b$tes t% )et st%red.
18H. main()
{
char aS1T="K9RR";
printf(".s"/a);
}
Answer:
K9RR.aVEaVa"""aEEV
Explanation:
>he character arra$ has the mem%r$ Bust en%u)h t% h%ld the strin) \K9RR] and d%esnYt ha(e
en%u)h space t% st%re the terminatin) null character. #% it prints the K9RR c%rrectl$ and
c%ntinues t% print )arba)e (alues till it accidentall$ c%mes acr%ss a *QRR character.
1H-. main()
{
int a=1-/LB;
(%id Lk;
B=k=Ca;
B++;
k++;
printf(";n .u .u "/B/k);
}
Answer:
&%mpiler err%r: &ann%t increment a (%id p%inter
Explanation:
[%id p%inters are )eneric p%inters and the$ can be used %nl$ when the t$pe is n%t kn%wn and
as an intermediate address st%ra)e t$pe. *% p%inter arithmetic can be d%ne %n it and $%u
cann%t appl$ indirecti%n %perat%r (L) %n (%id p%inters.
1H1. main()
{
e!tern int i;
{
int i=2-;
{
c%nst (%latile unsi)ned i=+-; printf(".d"/i);
}
printf(".d"/i);
}
printf(".d"/i);
}
int i;
Answer:
+-2--
1H2. Mrintf can be implemented b$ usin) UUUUUUUUUU list.
Answer:
[ariable len)th ar)ument lists
1H+. char Ls%meNun()
{
char Ltemp = \strin) c%nstant";
return temp;
}
int main()
{
puts(s%meNun());
}
Answer:
strin) c%nstant
Explanation:
>he pr%)ram suffers n% pr%blem and )i(es the %utput c%rrectl$ because the character
c%nstants are st%red in c%deIdata area and n%t all%cated in stack/ s% this d%esnYt lead t%
dan)lin) p%inters.
1H1. char Ls%meNun1()
{
char tempS T = \strin)";
return temp;
}
char Ls%meNun2()
{
char tempS T = {XsY/ XtY/YrY/YiY/YnY/Y)Y};
return temp;
}
int main()
{
puts(s%meNun1());
puts(s%meNun2());
}
Answer:
Jarba)e (alues.
Explanation:
<%th the functi%ns suffer fr%m the pr%blem %f dan)lin) p%inters. =n s%meNun1() temp is a
character arra$ and s% the space f%r it is all%cated in heap and is initiali3ed with character
strin) \strin)]. >his is created d$namicall$ as the functi%n is called/ s% is als% deleted
d$namicall$ %n e!itin) the functi%n s% the strin) data is n%t a(ailable in the callin) functi%n
main() leadin) t% print s%me )arba)e (alues. >he functi%n s%meNun2() als% suffers fr%m the
same pr%blem but the pr%blem can be easil$ identified in this case.
9nd0#emester 9!aminati%n
1) main()
{
int !ST={-/-/-/-/-};
int i;
f%r(i=1;iF=1;i++)
!S!SiTT++;
f%r(i=-;iF4;i++)
printf(".d"/!SiT);
}
hat is the %utput %f the c%de"
(a) -----
(b) 11111
(c) 1----
(d) 12+14
'nswer: c
2) Wdefine fun(a/b) a=a+b;b=a0b;a=a0b;
main()
{
int !=4/$=1-;
fun(!/$);
printf(".d .d;n"/!/$);
}
hatYs the %utput %f the c%de"
(a) 14/4
(b) 1-/4
(c) 1-/1-
(d) 4/14
'nswer: b
+) main()
{
int '=4/!;
!=fun(C'/');
printf(".d"/!);
}
fun(int L!/ int $)
{
L!=L!+1;
return(L!L$);
}
hat is the %utput %f the ab%(e pr%)ram"
(a) +7
(b) +-
(c) 24
(d) &%mpiler dependent
'nswer: b
1) main()
{
fib(4);
}
fib(int n)
{
if(n==- DD n==1)
return 1;
return fib(n01)+fib(n02);
}
K%w man$ times functi%ns fib() is called
(a) 11 (b) 14 (c) 17 (d) +
'nswer: b
4) Wdefine scanf ".s is a strin)"
main()
{
printf(scanf/scanf);
}
hat is the %utput %f the c%de"
(a) c%mpile err%r
(b) scanf is a strin)
(c) .s is a strin) is a strin)
(d) .s is a strin)
'nswer: c
7) int n=672+/result=-/ temp;
while(n,-)
{
temp=nI1-;
result+=tempL1-;
n=nI1-;
}
hat is the (alue %f result after the l%%p has e!ecuted"
a. 672+
b. 67--
c. 814-
d. 81--
'nswer: c
6) struct '
{
int a;
char b;
int c;
};
uni%n <
{
char a;
int b;
int c;
};
hich %f the f%ll%win) is c%rrect"
a. si3e %f ' is alwa$s diff. f%rm si3e %f <.
b. si3e %f ' is alwa$s same as si3e %f <.
c. we can2t sa$ an$thin) because '/< are n%t h%m%)en%us
d. si3e %f a can be same dependin) %n usa)e %f <.
'nswer: a
8) main()
{
int aST={ 2/1/7/8/1-};
int i;
chan)e(a/4);
f%r( i = -; i F= 1; i++)
printf(";n .d"/aSiT);
}
chan)e( int Lb/ int n)
{
int i;
f%r( i = -; i F n; i++)
L(b+i) = L(b+i) + 4;
}
hatYs the %utput %f the c%de"
a. 6 H 11 1+ 14
b. 2 1 7 8 1-
c. 8 1- 12 11 17
d. n%ne %f the ab%(e
'nswer: a
H) main()
{
int i=-;
f%r(i=-;iF2-;i++)
{
switch(i)
{
case -:i+=4;
case 1:i+=2;
case 4:i+=4;
default:i+=1;
break;
}
printf(".d/"/i);
}
}
hat is the %utput"
(a) -/4/H/1+/16/
(b) 4/H/1+/16/
(c) 12/16/22/
(d) 17/21/
'nswer: d
1-) hat d%es f%ll%win) pr%)ram print"
main()
{
inc(); inc(); inc();
}
inc()
{
static int !;
printf(".d"/ ++!);
}
(a) -12
(b) 12+
(c) + c%nsecuti(e unprectiable numbers
(d) 111.
'nswer: b
11) main() {
unsi)ned char ch;
N=R9 Lfp=f%pen(">rial.t!t"/ "r");
while((ch=)etc(fp))V=95N)
printf(".c;n"/ ch);
fcl%se(fp);
}
hat d%es the ab%(e pr%)ram d%"
a) Mrints the w%rd ">rail.t!t"
b) Mrints the c%ntents %f the file ">rail.t!t"
c). =nfinite l%%pI#e)mentati%n Nault
d). <%th b and c
'nswer: d
12) main()
{
unsi)ned char ch = -!8-;
printf(".d;n"/ chFF1);
}
hat will be the %utput %f the pr%)ram"
a) @em%r$ 5(erfl%w
b) 247
c) 128
d) 71
'nswer: b
1+) =f the f%ll%win) statement is successfull$ e!ecuted then fp p%ints t%...
N=R9 Lfp = f%pen("trial"/ "r");
a) >he first character in the file
b) ' structure which c%ntains a char p%inter t% the first character in the file
c) >he name %f the file
d) *%ne %f the ab%(e
'nswer: b
11) main()
{
char aST="[isual &++";
char Lb ="[isual &";
int i/ Lc=Ci;
printf(".d .d .d/ "/ si3e%f(a)/ si3e%f(b)/ si3e%f(c));
printf(".d .d .d;n"/ si3e%f(La)/ si3e%f(Lb)/ si3e%f(Lc));
}
hat will be the %utput"
a) 11 1 1/ 1 1 1
b) 11 1 1/ 1 1 1
c) 1- 1 1/ 1 1 1
d) 1- 1 1/ 1 1 1
'nswer: b
14) main()
{
int aST={2/ +/ 1/ 4/ 7};
printf(".u .d "/ CaS1T0a/ si3e%f(a)Isi3e%f(int));
}
=t prints:
a) 1 4
b) 1 4
c) 1 2-
d) 1 2-
'nswer: a
17) hat is the %utput %f f%ll%win) pr%)ram"
main() {
printf(4+"Nift$#i!");
}
a) &%mpilati%n 9rr%r
b) #i!t$5ne
c) #i!
d) 4+Nift$#i!
'nswer: c
16) Wdefine int char
main() {
int i=74;
printf("si3e%f(i)=.d"/si3e%f(i));
}
hat is the %utput"
a) si3e%f(i)=1
b) si3e%f(i)=1
c) si3e%f(i)=74
d) si3e%f(i)=-
'nswer: a
18) hat is the %utput %f the f%ll%win) pr%)ram"
main()
{
char s1ST="Kell%";
char s2ST=">hereV";
s1=s2;
printf(".s"/s1);
}
a) Kell% >hereV
b) Kell%
c) >hereV
d) &%mpilati%n 9rr%r
'nswer: d
1H) ' mem%r$ %f 2- b$tes is all%cated t% a strin) declared as char Ls then the f%ll%win) tw%
statements is e!ecuted:
s="9ntrance"
l=strlen(s);
hat is the (alue %f l"
a) 2-
b) 8
c) H
d) 21
'nswer: b
2-) hat is the %utput %f the f%ll%win) pr%)ram"
W define pr%d(a/b) aLb
main()
{
int !=2/ $=+;
printf(".d;n"/pr%d(!+2/$01-));
}
') -
<) 02
&) 028
G) *%ne
'nswer: <
21) main()
{
char Lptr;
ptr = (char L)mall%c(si3e%f(char)L2-);
strcp$(ptr/"==> ^anpur");
(Lptr)++;
printf(".s "/ptr);
ptr++;
printf(".s;n"/ptr);
}
a) O=> ^anpur => ^anpur
b) ==> ^anpur ==> ^anpur
c) => ^anpur O=> ^anpur
d) => ^anpur => ^anpur
'nswer: a
22) main()
{
fl%at i=1.4;
switch(i)
{
case 1: printf("1");
case 2: printf("2");
default : printf("-");
}
}
a) 12-
b) 1
c) -
d) &%mpiler 9rr%r
'nswer: d
2+) Ka(e a l%%k at these tw% statements
i. (aF=2-) " (b=+-) : (c=+-);
ii. L((aF=2-) " Cb : Cc) = +-;
hich %f the f%ll%win) is c%rrect"
a) (i) and (ii) are same
b) (i) and (ii) are different
c) (ii) results in s$nta! err%r
d) *%ne %f the ab%(e
'nswer: a
21) hat is the %utput %f the f%ll%win) pr%)ram"
main()
{
int i=0+/ B=2/ k=-/ m;
m = ++i CC ++B DD ++k;
printf(".d .d .d .d;n"/ i/ B/ k/ m);
}
a) Gepends up%n the m%%d %f the c%mpiler
b) 02 + 1 1
c) 02 + - 1
d) #$nta! err%r
'nswer: c
24) aSiTSBTSkTSlT is e?ui(alent t% ...
a) L(L(L(L(a+i)+B)+k)+l)
b) L(L(L(La+i)+B)+k)+l
c) La+i+B+k+l
d) L(a+i+B+k+l)
'nswer: a
27) K%w will $%u declare "an arra$ %f + functi%n p%inters where each functi%n recei(es an
int and returns a fl%at"..."
a) fl%at (LarrS+T) (int);
b) fl%at Larr (int) S+T;
c) fl%at (Larr) (int) S+T;
d) (fl%at L) arrS+T (int);
'nswer: a
26) main() {
int i = -;
switch(i) {
printf("&%mm%n t% b%th the cases. ");
case -:
printf("#l%w and #tead$ wins the race");
break;
case 1:
printf("h% are the$..."");
break;
}
switch(i) {
}
}
a) &%mm%n t% b%th the cases. #l%w and #tead$ wins the race.
b) #l%w and #tead$ wins the race.
c) 9rr%r in Nirst switch...case
d) 9rr%r in #ec%nd switch...case
'nswer: b
28) main() {
int i=4/ B=1;
switch(i)
{
default: printf("Gefault");
break;
case 1:
printf("&ase 1");
break;
case 6I2.-:
printf("&ase 2");
break;
case (+L2)01:
printf("&ase +");
break;
case B:
printf("&ase 1");
break;
}
}
a) Gefault
b) 9rr%r in #ec%nd/ N%urth cases
c) 9rr%r in #ec%nd/ >hird/ N%urth cases
d) 9rr%r in N%urth case
'nswer: b
2H) main()
{
int a=1-/ b;
a,=4 " b=1-- : b=2--;
printf("a = .d/ b = .d;n"/ a/ b);
}
a) a = 1-/ b = 1--
b) a = 1-/ b = 2--
c) &%mpilati%n 9rr%r
d) #e)mentati%n Nault
'nswer: c
+-) main() {
char ch=2a2;
printf(ch " ".d" : ".c"/ ch);
}
=t prints:
a) a b) H6 c) 74 d)#$nta! 9rr%r
'nswer: b
+1) main() {
int i=1;
switch(i++)
{
case 1: printf("5ne... "); c%ntinue;
case 2: printf(">w%... "); break;
}
}
hat is the %utput"
a) 5ne... >w%...
b) 5ne...
c) =nfinite R%%p
d) #$nta! err%r
'nswer: d
+2) main()
{
static int aS2-T;
int i=-;
aSiT = i++;
printf(".d .d .d;n"/ aS-T/ aS1T/ i);
}
a) - - 1
b) 1 - 1
c) - 1 1
d) &%mpiler Gependent
'nswer: d
++) main()
{
int i=-1-/ B=-!2-/ k/ l/ m;
k = i D B;
l = i C B;
m = i _ B;
printf(".d .d .d .d .d;n"/ i/ B/ k/ l/ m);
}
hat is the %utput"
a) 1- 2- 7- - 7-
b) +2 +2 71 - 01
c) +2 +2 +2 +2 -
d) +2 +2 +2 7- -
'nswer: c
+1) main(){
int a/b;
int Lp/L?;
a=1-;b=1H;
p=C(a+b);
}
a) err%r in p=C(a+b)
b) warnin) in p=Cma!
c) c%mpiles successfull$ with n% warnin)s
d) c%mpiles successfull$ but se)mentati%n fault at runtime
'nswer: a
+4) main() {
int i=4;
printf(".d .d;n"/ i++/ i++);
}
5utput is:
a) 4 4
b) 7 4
c) 4 7
d) &%mpiler Gependent
'nswer: d
+7) hat d%es the f%ll%win) functi%n print"
func(int i)
{
if(i.2)return -;
else return 1;
}
main() { {
int i =+;
i=func(i);
i=func(i);
printf(".d"/i);
}
a) +
b) 1
c) -
d) 2
'nswer: b
+6) hen an arra$ is passed as parameter t% a functi%n/ which %f the f%ll%win) statement is
c%rrect ch%ice:
a) >he functi%n can chan)e (alues in the %ri)inal arra$
b) =n & parameters are passed b$ (alue. >he functi%n cann%t chan)e the %ri)inal (alue in the
arra$.
c) =t results in c%mpilati%n err%r when the functi%n tries t% access the elements in the
arra$.
d) :esults in a run time err%r when the funti%n tries t% access the elements in the arra$.
'nswer: a
+8) int f%%(int a/ int b)
{
if (aCb) return 1;
return -;
}
hich %f the f%ll%win) is true"
a) if either a %r b are 3er% returns alwa$s -
b) if b%th a C b are n%n 3er% returns alwa$s 1
c) if b%th a and b are ne)ati(e returns alwa$s -
d) if b%th are p%siti(e returns alwa$s -
'nswer: a
+H) hat w%uld the f%ll%win) pr%)ram results in
main()
{
char pST="strin)";
char t;
int i/B;
f%r(i=-/B=strlen(p);iFB;i++)
{
t=pSiT;
pSiT=pSB0iT;
pSB0iT=t;
}
printf(".s"/p);
}
a) will print:strin)
b) will n%t print an$thin) since p will be p%intin) t% a null strin)
c) will print:)nirt#
d) will result in a c%mplicati%n err%r
'nswer: b
1-) main()
{
unsi)ned int !=01;
int $;
$ = E-;
if(! == $)
printf("same");
else
printf("n%t same");
}
hat will be the %utput and the (alues %f ! and $ respecti(el$"
a) same/ @'A=*>/ 01
b) n%t same/ @'A=*>/ 0@'A=*>
c) same/ @'AQ*=>/ 01
d) same/ @'AQ*=>/ @'AQ*=>
e) n%t same/ @'A=*>/ @'AQ*=>
'nswer: a
Nill in the blanks
rite the %utput %f the f%ll%win) pr%)rams:
11) main()
{
char sS T="man";
int i;
f%r(i=-;sS i T;i++)
printf(".c.c.c;n"/sS i T/L(s+i)/iSsT);
}
'nswer: mmm
aaa
nnn
12.) main() {
int cS T={2.8/+.1/1};
int B/Lp=c/L?=c;
f%r(B=-;BF+;B++)
{
printf(".d"/Lc);
++?;
}
f%r(B=-;BF+;B++)
{
printf(".d"/Lp);
++p;
}
}
'nswer: 2222+1
1+.) main( )
{
int aS2TS+TS2T = {{{2/1}/{6/8}/{+/1}}/{{2/2}/{2/+}/{+/1}}};
printf(".u .u .u .d ;n"/a/La/LLa/LLLa);
printf(".u .u .u .d ;n"/a+1/La+1/LLa+1/LLLa+1);
}
Ji(en #tartin) @em%r$ 'ddress %f ' is +2212142H7
'nswer: +2212142H7 +2212142H7 +2212142H7 2
+221214+2- +221214+-8 +221214+-- +
11) main()
{
char Lp;
int L?;
d%uble Lr;
p=?=r=-;
p++;
?++;
r++;
printf(".p....p....p"/p/?/r);
}
'nswer: 1...1...8
14) main() {
int LpS1-T;
int (L?)S1-T;
printf(".u;n"/si3e%f(p));
printf(".u;n"/si3e%f(Lp));
printf(".u;n"/si3e%f(?));
printf(".u;n"/si3e%f(L?));
}
'nswer: 1-
1
1
1-
17) =n the f%ll%win) functi%n fill in the blanks (with a sin)le character) t% make the swap
functi%n w%rk
main() {
int ! = 1-;
int $ = 14;
swap(UUU/UUU);
printf("! = .d/$ = .d"/!/$);
}
int swap(int UU/int UU)
{
int t = UU;
UU = UU;
UU =t;
}
'nswer:
C!/ C$/ L!/ L$/ L!/ L!/ L$/ L$
16) hile &%mpilin) a & Nile the :%ute N%ll%wed is
& Nile 0,UUUUUUUUUUUUUUUUU0,UUUUUUUUUUUUUUUUU0,UUUUUUUUUUUUUUUU0,a.%ut
(&%mpilerIMrepr%cess%rIRinker)
'nswer: Mrepr%cess%r c%mpiler linker
18) 'n e?ui(alent wa$ %f writin) the f%ll%win) statement
int !S@TS*T; II@ and * are inte)er c%nstants are
(in (@I*) tick either @ %r *)
int LL!;
! = (int LL)mall%c((@I*)Lsi3e%f(UUUUU));
int i;
f%r(i=-;iF(@I*);i++)
!SiT = (int L)mall%c((@I*)Lsi3e%f(UUUUU));
and t% access the (ariable !SiTSBT is e?ui(alent t% UUUUUUUUUUUUU(use %nl$ p%inters)
'nswer: @/ int L/ */ int
1H) >he Gata t$pe and the c%rresp%ndin) #trin) t% Mrint:
#trin) Gata >$pe
UUUUUU int
.f UUUUUUUUUU
.u UUUUUUUUUU
UUUUUU Ke!adecimal
.p UUUUUUUUUU
.% UUUUUUUUUU
'nswer: .d/ fl%at/ unsi)ned int/ .!/ p%inter/ %ctal
4-) main()
{
char Lp = -;
p =(char L)mall%c(1-Lsi3e%f(char));
p = "&0&%urse";
printf(".d "/strlen(p));
printf(".d "/si3e%f(p));
printf(".d "/si3e%f(Lp));
}
'nswer: 8 1 1

You might also like