Sunteți pe pagina 1din 53

C Aptitude

1. void main()
{
int const * p=5;
printf("%d",++(*p));
}
Answer:
Compiler error: Cannot modify a constant value.
Explanation:
p is a pointer to a "constant integer". But we tried to change the value of the "constant integer".
2. main()
{
char s[ ="man";
int i;
for(i=!;s[ i ;i++)
printf(""n%c%c%c%c",s[ i ,*(s+i),*(i+s),i[s);
}
Answer:
mmmm
aaaa
nnnn
Explanation:
s[i], *(is!, *(si!, i[s] are all different ways of e"pressing the same idea. #enerally array
name is the $ase address for that array. %ere s is the $ase address. i is the inde" num$er&displacement from the
$ase address. 'o, indirecting it with * is same as s[i]. i[s] may $e surprising. But in the case of C it is same as
s[i].
#. main()
{
f$oat m% = 1.1;
do&'$% (o& = 1.1;
if(m%==(o&)
printf(") $ov% *");
%$s%
printf(") hat% *");
}
Answer:
( hate )
Explanation:
*or floating point num$ers (float, dou$le, long dou$le) the values cannot $e predicted e"actly.
+epending on the num$er of $ytes, the precession with of the value represented varies. *loat ta,es - $ytes and
long dou$le ta,es ./ $ytes. 'o float stores /.0 with less precision than long dou$le.
Rule of Thumb:
1ever compare or at2least $e cautious when using floating point num$ers with relational
operators (33 , 4, 5, 53, 43,63 ) .
+. main()
{
static int var = 5;
printf("%d ",var,,);
if(var)
main();
}
Answer:
7 - 8 9 .
Explanation:
:hen static storage class is given, it is initiali;ed once. <he change in the value of a static
varia$le is retained even $etween the function calls. =ain is also treated li,e any other ordinary function,
which can $e called recursively.
5. main()
{
int c[ ={2.-,#.+,+,../,5};
int 0,*p=c,*1=c;
for(0=!;025;0++) {
printf(" %d ",*c);
++1; }
for(0=!;025;0++){
printf(" %d ",*p);
++p; }
}
Answer:
9 9 9 9 9 9 8 - > 7
Explanation:
(nitially pointer c is assigned to $oth p and q. (n the first loop, since only q is incremented and
not c , the value 9 will $e printed 7 times. (n second loop p itself is incremented. 'o the values 9 8 - > 7 will $e
printed.
.. main()
{
%3t%rn int i;
i=2!;
printf("%d",i);
}
Answer:
4in5%r 6rror : )ndefined sym$ol ?@i?
Explanation:
e"tern storage class in the following declaration,
extern int i;
specifies to the compiler that the memory for i is allocated in some other program and that address will $e
given to the current program at the time of lin,ing. But lin,er finds that no other varia$le of name i is availa$le
in any other program with memory space allocated for it. %ence a lin,er error has occurred .
/. main()
{
int i=,1,0=,1,5=!,$=2,m;
m=i++770++775++88$++;
printf("%d %d %d %d %d",i,0,5,$,m);
}
Answer:
/ / . 8 .
Explanation :
9
Aogical operations always give a result of 1 or 0 . Bnd also the logical B1+ (CC! operator
has higher priority over the logical DE (FF! operator. 'o the e"pression Gi++ && j++ && ++! is e"ecuted
first. <he result of this e"pression is / (2. CC 2. CC / 3 /!. 1ow the e"pression is / FF 9 which evaluates to .
($ecause DE operator always gives . e"cept for G/ FF /H com$ination2 for which it gives /!. 'o the value of m is
.. <he values of other varia$les are also incremented $y ..
-. main()
{
char *p;
printf("%d %d ",si9%of(*p),si9%of(p));
}
Answer:
. 9
Explanation:
<he si;eof(! operator gives the num$er of $ytes ta,en $y its operand. I is a character pointer,
which needs one $yte for storing its value (a character!. %ence si;eof(*p! gives a value of .. 'ince it needs two
$ytes to store the address of the character pointer si;eof(p! gives 9.
:. main()
{
int i=#;
s;itch(i)
{
d%fa&$t<printf("9%ro");
cas% 1< printf("on%");
'r%a5;
cas% 2<printf("t;o");
'r%a5;
cas% #< printf("thr%%");
'r%a5;
}
}
Answer :
three
Explanation :
<he default case can $e placed anywhere inside the loop. (t is e"ecuted only when all other
cases doesn?t match.
1!. main()
{
printf("%3",,122+);
}
Answer:
fff/
Explanation :
2. is internally represented as all .?s. :hen left shifted four times the least significant - $its are
filled with /?s.<he J" format specifier specifies that the integer value $e printed as a he"adecimal value.
11. main()
{
char strin=[=">%$$o ?or$d";
disp$a((strin=);
8
}
void disp$a((char *strin=)
{
printf("%s",strin=);
}
Answer:
@ompi$%r 6rror < <ype mismatch in redeclaration of function display
Explanation :
(n third line, when the function displ"# is encountered, the compiler doesn?t ,now anything
a$out the function display. (t assumes the arguments and return types to $e integers, (which is the default type!.
:hen it sees the actual function displ"#$ the arguments and type contradicts with what it has assumed
previously. %ence a compile time error occurs.
12. main()
{
int c=, ,2;
printf("c=%d",c);
}
Answer:
c39K
Explanation:
%ere unary minus (or negation! operator is used twice. 'ame maths rules applies, ie. minus *
minus3 plus.
%ote:
%owever you cannot give li,e 229. Because 22 operator can only $e applied to varia$les as a
de&rement operator (eg., i22!. 9 is a constant and not a varia$le.
1#. Ad%fin% int char
main()
{
int i=.5;
printf("si9%of(i)=%d",si9%of(i));
}
Answer:
si;eof(i!3.
Explanation:
'ince the Ldefine replaces the string int $y the macro &h"r
1+. main()
{
int i=1!;
i=BiC1+;
printf("i=%d",i);
}
Answer:
i3/
Explanation:
(n the e"pression 'i(1) , 1D< (6! operator has more precedence than G 4H sym$ol. ' is a unary
logical operator. 6i (6./! is / (not of true is false!. /4.- is false (;ero!.
15. Ainc$&d%2stdio.hC
-
main()
{
char s[={DaD,D'D,DcD,D"nD,DcD,D"!D};
char *p,*str,*str1;
p=7s[#;
str=p;
str1=s;
printf("%d",++*p + ++*str1,#2);
}
Answer:
MM
Explanation:
p is pointing to character ?Nn?. str. is pointing to character ?a? *p. "p is pointing to ?Nn? and that is
incremented $y one." the B'C(( value of ?Nn? is ./, which is then incremented to ... <he value of *p is ...
*str., str. is pointing to ?a? that is incremented $y . and it $ecomes ?$?. B'C(( value of ?$? is 0O.
1ow performing (.. 0O P 89!, we get MM("="!K
'o we get the output MM :: "=" (Bscii is MM!.
1.. Ainc$&d%2stdio.hC
main()
{
int a[2[2[2 = { {1!,2,#,+}, {5,.,/,-} };
int *p,*1;
p=7a[2[2[2;
*1=***a;
printf("%d,,,,%d",*p,*1);
}
Answer:
'ome#ar$ageQalue222.
Explanation:
p3Ca[9][9][9] you declare only two 9+ arrays, $ut you are trying to access the third
9+(which you are not declared! it will print gar$age values. *R3***a starting address of a is assigned integer
pointer. 1ow R is pointing to starting address of a. (f you print *R, it will print first element of 8+ array.
1/. Ainc$&d%2stdio.hC
main()
{
str&ct 33
{
int 3=#;
char nam%[="h%$$o";
};
str&ct 33 *s;
printf("%d",s,C3);
printf("%s",s,Cnam%);
}
Answer:
Compiler Srror
Explanation:
Tou should not initiali;e varia$les in declaration
1-. Ainc$&d%2stdio.hC
main()
7
{
str&ct 33
{
int 3;
str&ct ((
{
char s;
str&ct 33 *p;
};
str&ct (( *1;
};
}
Answer:
Compiler Srror
Explanation:
<he structure yy is nested within structure "". %ence, the elements are of yy are to $e accessed
through the instance of structure "", which needs an instance of yy to $e ,nown. (f the instance is created after
defining the structure the compiler will not ,now a$out the instance relative to "". %ence for nested structure
yy you have to declare mem$er.
1:. main()
{
printf(""na'");
printf(""'si");
printf(""rha");
}
Answer:
hai
Explanation:
Nn 2 newline
N$ 2 $ac,space
Nr 2 linefeed
2!. main()
{
int i=5;
printf("%d%d%d%d%d%d",i++,i,,,++i,,,i,i);
}
Answer:
-77-7
Explanation:
<he arguments in a function call are pushed into the stac, from left to right. <he evaluation is
$y popping out from the stac,. and the evaluation is from right to left, hence the result.
21. Ad%fin% s1&ar%(3) 3*3
main()
{
int i;
i = .+Es1&ar%(+);
printf("%d",i);
}
Answer:
>-
>
Explanation:
the macro call sRuare(-! will su$stituted $y -*- so the e"pression $ecomes i 3 >-&-*- . 'ince &
and * has eRual priority the e"pression will $e evaluated as (>-&-!*- i.e. .>*- 3 >-

22. main()
{
char *p="hai fri%nds",*p1;
p1=p;
;hi$%(*pB=D"!D) ++*p++;
printf("%s %s",p,p1);
}
Answer:
i$U6gsUfoet
Explanation:
*p will $e parse in the given order
*p that is value at the location currently pointed $y p will $e ta,en
*p the retrieved value will $e incremented
when K is encountered the location will $e incremented that is p will $e e"ecuted
%ence, in the while loop initial value pointed $y p is GhH, which is changed to GiH $y e"ecuting *p and pointer
moves to point, GaH which is similarly changed to G$H and so on. 'imilarly $lan, space is converted to G6H. <hus,
we o$tain value in p $ecomes Vi$U6gsUfoetW and since p reaches GN/H and p. points to p thus p.doesnot print
anything.
2#. Ainc$&d% 2stdio.hC
Ad%fin% a 1!
main()
{
Ad%fin% a 5!
printf("%d",a);
}
Answer:
7/
Explanation:
<he preprocessor directives can $e redefined anywhere in the program. 'o the most recently
assigned value will $e ta,en.
2+. Ad%fin% c$rscr() 1!!
main()
{
c$rscr();
printf("%d"n",c$rscr());
}
Answer:
.//
Explanation:
Ireprocessor e"ecutes as a seperate pass $efore the e"ecution of the compiler. 'o te"tual
replacement of clrscr(! to .// occurs.<he input program to compiler loo,s li,e this :
main(!
X
.//K
printf("JdNn",.//!K
Y
%ote: M
.//K is an e"ecuta$le statement $ut with no action. 'o it doesn?t give any pro$lem
25. main()
{
printf("%p",main);
}
Answer:
'ome address will $e printed.
Explanation:
*unction names are Uust addresses (Uust li,e array names are addresses!.
main(! is also a function. 'o the address of function main will $e printed. Jp in printf specifies that the
argument is an address. <hey are printed as he"adecimal num$ers.
9M! main(!
X
clrscr(!K
Y
clrscr(!K
Answer:
1o output&error
Explanation:
<he first clrscr(! occurs inside a function. 'o it $ecomes a function call. (n the second clrscr(!K
is a function declaration ($ecause it is not inside any function!.
9O! enum colors XBABCZ,BA)S,#ESS1Y
main(!
X

printf("Jd..Jd..Jd",BABCZ,BA)S,#ESS1!K

return(.!K
Y
Answer:
/.....9
Explanation:
enum assigns num$ers starting from /, if not e"plicitly defined.
90! void main(!
X
char far *farther,*farthestK

printf("Jd..Jd",si;eof(farther!,si;eof(farthest!!K

Y
Answer:
-..9
Explanation:
the second pointer is of char type and not a far pointer
8/! main(!
X
int i3-//,U38//K
O
printf("Jd..Jd"!K
Y
Answer:
-//..8//
Explanation:
printf ta,es the values of the first two assignments of the program. Bny num$er of printf?s may
$e given. Bll of them ta,e only the first two values. (f more num$er of assignments given in
the program,then printf will ta,e gar$age values.
8.! main(!
X
char *pK
p3"%ello"K
printf("JcNn",*C*p!K
Y
Answer:
%
Explanation:
* is a dereference operator C is a reference operator. <hey can $e applied any num$er of
times provided it is meaningful. %ere p points to the first character in the string "%ello". *p
dereferences it and so its value is %. Bgain C references it to an address and * dereferences it
to the value %.
89! main(!
X
int i3.K
while (i537!
X
printf("Jd",i!K
if (i49!
goto hereK
iK
Y
Y
fun(!
X
here:
printf("II"!K
Y
Answer:
Compiler error: )ndefined la$el ?here? in function main
Explanation:
Aa$els have functions scope, in other words <he scope of the la$els is limited to functions .
<he la$el ?here? is availa$le in function fun(! %ence it is not visi$le in function main.
88! main(!
X
static char names[7][9/]3X"pascal","ada","co$ol","fortran","perl"YK
int iK
char *tK
t3names[8]K
names[8]3names[-]K
names[-]3tK
0
for (i3/Ki53-Ki!
printf("Js",names[i]!K
Y
Answer:
Compiler error: Avalue reRuired in function main
Explanation:
Brray names are pointer constants. 'o it cannot $e modified.
8-! void main(!
X
int i37K
printf("Jd",i i!K
Y
Answer:
Dutput Cannot $e predicted e"actly.
Explanation:
'ide effects are involved in the evaluation of i
87! void main(!
X
int i37K
printf("Jd",ii!K
Y
Answer:
Compiler Srror
Explanation:
<he e"pression ii is parsed as i i which is an illegal com$ination of operators.

8>! Linclude5stdio.h4
main(!
X
int i3.,U39K
switch(i!
X
case .: printf("#DD+"!K
$rea,K
case U: printf("BB+"!K
$rea,K
Y
Y
Answer:
Compiler Srror: Constant e"pression reRuired in function main.
Explanation:
<he case statement can have only constant e"pressions (this implies that we cannot use
varia$le names directly so an error!.
%ote:
Snumerated types can $e used in case statements.
8M! main(!
X
int iK
printf("Jd",scanf("Jd",Ci!!K && value ./ is given as input here
Y
./
Answer:
.
Explanation:
'canf returns num$er of items successfully read and not .&/. %ere ./ is given as input which
should have $een scanned successfully. 'o num$er of items read is ..
8O! Ldefine f(g,g9! gLLg9
main(!
X
int var.93.//K
printf("Jd",f(var,.9!!K
Y
Answer:
.//
80! main(!
X
int i3/K

for(KiKprintf("Jd",i!! K
printf("Jd",i!K
Y
Answer:
.
Explanation:
$efore entering into the for loop the chec,ing condition is "evaluated". %ere it evaluates to /
(false! and comes out of the loop, and i is incremented (note the semicolon after the for loop!.
-/! Linclude5stdio.h4
main(!
X
char s[]3X?a?,?$?,?c?,?Nn?,?c?,?N/?YK
char *p,*str,*str.K
p3Cs[8]K
str3pK
str.3sK
printf("Jd",*p *str.289!K
Y
Answer:
=
Explanation:
p is pointing to character ?Nn?.str. is pointing to character ?a? *p meBnswer:"p is pointing to
?Nn? and that is incremented $y one." the B'C(( value of ?Nn? is ./. then it is incremented to ...
the value of *p is ... *str. meBnswer:"str. is pointing to ?a? that is incremented $y . and
it $ecomes ?$?. B'C(( value of ?$? is 0O. $oth .. and 0O is added and result is su$tracted from
89.
i.e. (..0O289!3MM("="!K
-.! Linclude5stdio.h4
main(!
X
struct ""
X
..
int "38K
char name[]3"hello"K
YK
struct "" *s3malloc(si;eof(struct ""!!K
printf("Jd",s24"!K
printf("Js",s24name!K
Y
Answer:
Compiler Srror
Explanation:
(nitiali;ation should not $e done for structure mem$ers inside the structure declaration
-9! Linclude5stdio.h4
main(!
X
struct ""
X
int "K
struct yy
X
char sK
struct "" *pK
YK
struct yy *RK
YK
Y
Answer:
Compiler Srror
Explanation:
in the end of nested structure yy a mem$er have to $e declared.
-8! main(!
X
e"tern int iK
i39/K
printf("Jd",si;eof(i!!K
Y
Answer:
Ain,er error: undefined sym$ol ?@i?.
Explanation:
e"tern declaration specifies that the varia$le i is defined somewhere else. <he compiler passes
the e"ternal varia$le to $e resolved $y the lin,er. 'o compiler doesn?t find an error. +uring
lin,ing the lin,er searches for the definition of i. 'ince it is not found the lin,er flags an error.
--! main(!
X
printf("Jd", out!K
Y
int out3.//K
Answer:
Compiler error: undefined sym$ol out in function main.
Explanation:
.9
<he rule is that a varia$le is availa$le for use from the point of declaration. Sven though a is a
glo$al varia$le, it is not availa$le for main. %ence an error.
-7! main(!
X
e"tern outK
printf("Jd", out!K
Y
int out3.//K
Answer:
.//
Explanation:
<his is the correct way of writing the previous program.

->! main(!
X
show(!K
Y
void show(!
X
printf("(?m the greatest"!K
Y
Answer:
Compier error: <ype mismatch in redeclaration of show.
Explanation:
:hen the compiler sees the function show it doesn?t ,now anything a$out it. 'o the default
return type (ie, int! is assumed. But when compiler sees the actual definition of show mismatch
occurs since it is declared as void. %ence the error.
<he solutions are as follows:
.. declare void show(! in main(! .
9. define show(! $efore main(!.
8. declare e"tern void show(! $efore the use of show(!.

-M! main( !
X
int a[9][8][9] 3 XXX9,-Y,XM,OY,X8,-YY,XX9,9Y,X9,8Y,X8,-YYYK
printf(VJu Ju Ju Jd NnW,a,*a,**a,***a!K
printf(VJu Ju Ju Jd NnW,a.,*a.,**a.,***a.!K
Y
Answer:
.//, .//, .//, 9
..-, ./-, ./9, 8
Explanation:
<he given array is a 82+ one. (t can also $e viewed as a .2+ array.

9 - M O 8 - 9 9 9 8 8 -
.// ./9 ./- ./> ./O ../ ..9 ..- ..> ..O .9/ .99
thus, for the first printf statement a, *a, **a give address of first element . since the
indirection ***a gives the value. %ence, the first line of the output.
for the second printf a. increases in the third dimension thus points to value at ..-, *a.
increments in second dimension thus points to ./-, **a . increments the first dimension thus
.8
points to ./9 and ***a. first gets the value at first location and then increments it $y ..
%ence, the output.
-O! main( !
X
int a[ ] 3 X./,9/,8/,-/,7/Y,U,*pK
for(U3/K U57K U!
X
printf(VJdW ,*a!K
aK
Y
p 3 aK
for(U3/K U57K U!
X
printf(VJd W ,*p!K
pK
Y
Y
Answer:
Compiler error: lvalue reRuired.
Explanation:
Srror is in line with statement a. <he operand must $e an lvalue and may $e of any of scalar
type for the any operator, array name only when su$scripted is an lvalue. 'imply array name is
a non2modifia$le lvalue.
-0! main( !
X
static int a[ ] 3 X/,.,9,8,-YK
int *p[ ] 3 Xa,a.,a9,a8,a-YK
int **ptr 3 pK
ptrK
printf(VNn Jd Jd JdW, ptr2p, *ptr2a, **ptr!K
*ptrK
printf(VNn Jd Jd JdW, ptr2p, *ptr2a, **ptr!K
*ptrK
printf(VNn Jd Jd JdW, ptr2p, *ptr2a, **ptr!K
*ptrK
printf(VNn Jd Jd JdW, ptr2p, *ptr2a, **ptr!K
Y
Answer:
111
222
###
#++
Explanation:
Aet us consider the array and the two pointers with some address
"
/ . 9 8 -
.// ./9 ./- ./> ./O
p
.// ./9 ./- ./> ./O
./// .//9 .//- .//> .//O
.-
ptr
.///
9///
Bfter e"ecution of the instruction ptr value in ptr $ecomes .//9, if scaling factor for integer
is 9 $ytes. 1ow ptr P p is value in ptr P starting location of array p, (.//9 P .///! & (scaling
factor! 3 ., *ptr P a 3 value at address pointed $y ptr P starting value of array a, .//9 has a
value ./9 so the value is (./9 P .//!&(scaling factor! 3 ., **ptr is the value stored in the
location pointed $y the pointer of ptr 3 value pointed $y value pointed $y .//9 3 value
pointed $y ./9 3 .. %ence the output of the firs printf is ., ., ..
Bfter e"ecution of *ptr increments value of the value in ptr $y scaling factor, so it
$ecomes.//-. %ence, the outputs for the second printf are ptr P p 3 9, *ptr P a 3 9, **ptr 3 9.
Bfter e"ecution of *ptr increments value of the value in ptr $y scaling factor, so it
$ecomes.//-. %ence, the outputs for the third printf are ptr P p 3 8, *ptr P a 3 8, **ptr 3 8.
Bfter e"ecution of *ptr value in ptr remains the same, the value pointed $y the value is
incremented $y the scaling factor. 'o the value in array p at location .//> changes from ./>
./ ./O,. %ence, the outputs for the fourth printf are ptr P p 3 .//> P ./// 3 8, *ptr P a 3 ./O P
.// 3 -, **ptr 3 -.
7/! main( !
X
char *RK
int UK
for (U3/K U58K U! scanf(VJsW ,(RU!!K
for (U3/K U58K U! printf(VJcW ,*(RU!!K
for (U3/K U58K U! printf(VJsW ,(RU!!K
Y
Explanation:
%ere we have only one pointer to type char and since we ta,e input in the same pointer thus
we ,eep writing over in the same location, each time shifting the pointer value $y .. 'uppose
the inputs are =D)'S, <EBCZ and Q(E<)BA. <hen for the first input suppose the pointer
starts at location .// then the input one is stored as
= D ) ' S N/
:hen the second input is given the pointer is incremented as U value $ecomes ., so the input is
filled in memory starting from ./..
= < E B C Z N/
<he third input starts filling from the location ./9
= < Q ( E < ) B A N/
<his is the final value stored .
<he first printf prints the values at the position R, R. and R9 3 = < Q
<he second printf prints three strings starting from locations R, R., R9
i.e =<Q(E<)BA, <Q(E<)BA and Q(E<)BA.

7.! main( !
X
void *vpK
char ch 3 GgH, *cp 3 VgoofyWK
int U 3 9/K
vp 3 CchK
printf(VJcW, *(char *!vp!K
vp 3 CUK
printf(VJdW,*(int *!vp!K
vp 3 cpK
.7
printf(VJsW,(char *!vp 8!K
Y
Answer:
g9/fy
Explanation:
'ince a void pointer is used it can $e type casted to any other type pointer. vp 3 Cch stores
address of char ch and the ne"t statement prints the value stored in vp after type casting it to
the proper data type pointer. the output is GgH. 'imilarly the output from second printf is G9/H.
<he third printf statement type casts it to print the string from the -
th
value hence the output is
GfyH.
79! main ( !
X
static char *s[ ] 3 XV$lac,W, VwhiteW, VyellowW, VvioletWYK
char **ptr[ ] 3 Xs8, s9, s., sY, ***pK
p 3 ptrK
**pK
printf(VJsW,*22*p 8!K
Y
Answer:
c,
Explanation:
(n this pro$lem we have an array of char pointers pointing to start of - strings. <hen we have
ptr which is a pointer to a pointer of type char and a varia$le p which is a pointer to a pointer
to a pointer of type char. p hold the initial value of ptr, i.e. p 3 s8. <he ne"t statement
increment value in p $y . , thus now value of p 3 s9. (n the printf statement the e"pression is
evaluated *p causes gets value s. then the pre decrement is e"ecuted and we get s. P . 3
s . the indirection operator now gets the value from the array of s and adds 8 to the starting
address. <he string is printed starting from this position. <hus, the output is Gc,H.
78! main(!
X
int i, nK
char *" 3 VgirlWK
n 3 strlen("!K
*" 3 "[n]K
for(i3/K i5nK i!
X
printf(VJsNnW,"!K
"K
Y
Y
Answer:
($lan, space!
irl
rl
l
Explanation:
%ere a string (a pointer to char! is initiali;ed with a value VgirlW. <he strlen function returns
the length of the string, thus n has a value -. <he ne"t statement assigns value at the nth
location (GN/H! to the first location. 1ow the string $ecomes VN/irlW . 1ow the printf statement
prints the string after each iteration it increments it starting position. Aoop starts from / to -.
.>
<he first time "[/] 3 GN/H hence it prints nothing and pointer value is incremented. <he second
time it prints from "[.] i.e VirlW and the third time it prints VrlW and the last time it prints VlW
and the loop terminates.
7-! int i,UK
for(i3/Ki53./Ki!
X
U37K
assert(i57!K
Y
Answer:
Euntime error: B$normal program termination.
assert failed (i57!, 5file name4,5line num$er4
Explanation:
asserts are used during de$ugging to ma,e sure that certain conditions are satisfied. (f assertion
fails, the program will terminate reporting the same. Bfter de$ugging use,
Lundef 1+SB)#
and this will disa$le all the assertions from the source code. Bssertion
is a good de$ugging tool to ma,e use of.

77! main(!
X
int i32.K
iK
printf("i 3 Jd, i 3 Jd Nn",i,i!K
Y
Answer:
i 3 2., i 3 2.
Explanation:
)nary is the only dummy operator in C. :here2ever it comes you can Uust ignore it Uust
$ecause it has no effect in the e"pressions (hence the name dummy operator!.
7>! :hat are the files which are automatically opened when a C file is e"ecuted[
Answer:
stdin, stdout, stderr (standard input,standard output,standard error!.
7M! what will $e the position of the file mar,er[
a: fsee,(ptr,/,'SSZ@'S<!K
$: fsee,(ptr,/,'SSZ@C)E!K
Answer :
a: <he 'SSZ@'S< sets the file position mar,er to the starting of the file.
$: <he 'SSZ@C)E sets the file position mar,er to the current position
of the file.
7O! main(!
X
char name[./],s[.9]K
scanf(" N"J[\N"]N"",s!K
Y
%ow scanf will e"ecute[
Answer:
*irst it chec,s for the leading white space and discards it.<hen it matches with a Ruotation
mar, and then it reads all character upto another Ruotation mar,.
.M
70! :hat is the pro$lem with the following code segment[
while ((fgets(receiving array,7/,file@ptr!! 63 SD*!
K
Answer & Explanation:
fgets returns a pointer. 'o the correct end of file chec, is chec,ing for 63 1)AA.
>/! main(!
X
main(!K
Y
Answer:
Euntime error : 'tac, overflow.
Explanation:
main function calls itself again and again. Sach time the function is called its return address is
stored in the call stac,. 'ince there is no condition to terminate the function call, the call stac,
overflows at runtime. 'o it terminates the program and results in an error.
>.! main(!
X
char *cptr,cK
void *vptr,vK
c3./K v3/K
cptr3CcK vptr3CvK
printf("JcJv",c,v!K
Y
Answer:
Compiler error (at line num$er -!: si;e of v is )n,nown.
Explanation:
Tou can create a varia$le of type void * $ut not of type void, since void is an empty type. (n
the second line you are creating varia$le vptr of type void * and v of type void hence an error.
>9! main(!
X
char *str.3"a$cd"K
char str9[]3"a$cd"K
printf("Jd Jd Jd",si;eof(str.!,si;eof(str9!,si;eof("a$cd"!!K
Y
Answer:
9 7 7
Explanation:
(n first si;eof, str. is a character pointer so it gives you the si;e of the pointer varia$le. (n
second si;eof the name str9 indicates the name of the array whose si;e is 7 (including the ?N/?
termination character!. <he third si;eof is similar to the second one.
>8! main(!
X
char notK
not369K
printf("Jd",not!K
Y
Answer:
/
.O
Explanation:
6 is a logical operator. (n C the value / is considered to $e the $oolean value *BA'S, and any
non2;ero value is considered to $e the $oolean value <E)S. %ere 9 is a non2;ero value so
<E)S. 6<E)S is *BA'S (/! so it prints /.
>-! Ldefine *BA'S 2.
Ldefine <E)S .
Ldefine 1)AA /
main(! X
if(1)AA!
puts("1)AA"!K
else if(*BA'S!
puts("<E)S"!K
else
puts("*BA'S"!K
Y
Answer:
<E)S
Explanation:
<he input program to the compiler after processing $y the preprocessor is,
main(!X
if(/!
puts("1)AA"!K
else if(2.!
puts("<E)S"!K
else
puts("*BA'S"!K
Y
Ireprocessor doesn?t replace the values given inside the dou$le Ruotes. <he chec, $y if
condition is $oolean value false so it goes to else. (n second if 2. is $oolean value true hence
"<E)S" is printed.
>7! main(!
X
int ,3.K
printf("Jd33. is ""Js",,,,33.["<E)S":"*BA'S"!K
Y
Answer:
.33. is <E)S
Explanation:
:hen two strings are placed together (or separated $y white2space! they are concatenated (this
is called as "stringi;ation" operation!. 'o the string is as if it is given as "Jd33. is Js". <he
conditional operator( [: ! evaluates to "<E)S".
>>! main(!
X
int yK
scanf("Jd",Cy!K && input given is 9///
if( (yJ-33/ CC yJ.// 63 /! FF yJ.// 33 / !
printf("Jd is a leap year"!K
else
printf("Jd is not a leap year"!K
Y
.0
Answer:
9/// is a leap year
Explanation:
Bn ordinary program to chec, if leap year or not.
>M! Ldefine ma" 7
Ldefine int arr.[ma"]
main(!
X
typedef char arr9[ma"]K
arr. list3X/,.,9,8,-YK
arr9 name3"name"K
printf("Jd Js",list[/],name!K
Y
Answer:
Compiler error (in the line arr. list 3 X/,.,9,8,-Y!
Explanation:
arr9 is declared of type array of si;e 7 of characters. 'o it can $e used to declare the varia$le
name of the type arr9. But it is not the case of arr.. %ence an error.
Rule of Thumb:
Ldefines are used for te"tual replacement whereas typedefs are used for declaring new types.
>O! int i3./K
main(!
X
e"tern int iK
X
int i39/K
X
const volatile unsigned i38/K
printf("Jd",i!K
Y
printf("Jd",i!K
Y
printf("Jd",i!K
Y
Answer:
8/,9/,./
Explanation:
?X? introduces new $loc, and thus new scope. (n the innermost $loc, i is declared as,
const volatile unsigned
which is a valid declaration. i is assumed of type int. 'o printf prints 8/. (n the ne"t $loc,, i
has value 9/ and so printf prints 9/. (n the outermost $loc,, i is declared as e"tern, so no
storage space is allocated for it. Bfter compilation is over the lin,er resolves it to glo$al
varia$le i (since it is the only varia$le visi$le there!. 'o it prints i?s value as ./.
>0! main(!
X
int *UK
X
int i3./K
U3CiK
Y
9/
printf("Jd",*U!K
Y
Answer:
./
Explanation:
<he varia$le i is a $loc, level varia$le and the visi$ility is inside that $loc, only. But the
lifetime of i is lifetime of the function so it lives upto the e"it of main function. 'ince the i is
still allocated space, *U prints the value stored in i since U points i.
M/! main(!
X
int i32.K
2iK
printf("i 3 Jd, 2i 3 Jd Nn",i,2i!K
Y
Answer:
i 3 2., 2i 3 .
Explanation:
2i is e"ecuted and this e"ecution doesn?t affect the value of i. (n printf first you Uust print the
value of i. Bfter that the value of the e"pression 2i 3 2(2.! is printed.
M.! Linclude5stdio.h4
main(!
X
const int i3-K
float UK
U 3 iK
printf("Jd Jf", i,U!K
Y
Answer:
Compiler error
Explanation:
i is a constant. you cannot change the value of constant
M9! Linclude5stdio.h4
main(!
X
int a[9][9][9] 3 X X./,9,8,-Y, X7,>,M,OY YK
int *p,*RK
p3Ca[9][9][9]K
*R3***aK
printf("Jd..Jd",*p,*R!K
Y
Answer:
gar$agevalue...
Explanation:
p3Ca[9][9][9] you declare only two 9+ arrays. $ut you are trying to access the third
9+(which you are not declared! it will print gar$age values. *R3***a starting address of a is
assigned integer pointer. now R is pointing to starting address of a.if you print *R meBnswer:it
will print first element of 8+ array.
M8! Linclude5stdio.h4
main(!
9.
X
register i37K
char U[]3 "hello"K
printf("Js Jd",U,i!K
Y
Answer:
hello 7
Explanation:
if you declare i as register compiler will treat it as ordinary integer and it will ta,e integer
value. i value may $e stored either in register or in memory.
M-! main(!
X
int i37,U3>,;K
printf("Jd",iU!K
Y
Answer:
..
Explanation:
the e"pression iU is treated as (i U!

M>! struct aaaX
struct aaa *prevK
int iK
struct aaa *ne"tK
YK
main(!
X
struct aaa a$c,def,ghi,U,lK
int "3.//K
a$c.i3/Ka$c.prev3CU,lK
a$c.ne"t3CdefK
def.i3.Kdef.prev3Ca$cKdef.ne"t3CghiK
ghi.i39Kghi.prev3CdefK
ghi.ne"t3CU,lK
U,l.i38KU,l.prev3CghiKU,l.ne"t3Ca$cK
"3a$c.ne"t24ne"t24prev24ne"t24iK
printf("Jd","!K
Y
Answer:
9
Explanation:
a$ove all statements form a dou$le circular lin,ed listK
a$c.ne"t24ne"t24prev24ne"t24i
this one points to "ghi" node the value of at particular node is 9.
MM! struct point
X
int "K
int yK
YK
struct point origin,*ppK
main(!
99
X
pp3CoriginK
printf("origin is(JdJd!Nn",(*pp!.",(*pp!.y!K
printf("origin is (JdJd!Nn",pp24",pp24y!K
Y
Answer:
origin is(/,/!
origin is(/,/!
Explanation:
pp is a pointer to structure. we can access the elements of the structure either with arrow mar,
or with indirection operator.
%ote:
'ince structure point is glo$ally declared " C y are initiali;ed as ;eroes
MO! main(!
X
int i3@l@a$c(./!K
printf("JdNn",22i!K
Y
int @l@a$c(int i!
X
return(i!K
Y
Answer:
0
Explanation:
return(i! it will first return i and then increments. i.e. ./ will $e returned.
M0! main(!
X
char *pK
int *RK
long *rK
p3R3r3/K
pK
RK
rK
printf("Jp...Jp...Jp",p,R,r!K
Y
Answer:
///....///9...///-
Explanation:
operator when applied to pointers increments address according to their corresponding
data2types.
O/! main(!
X
char c3? ?,",convert(;!K
getc(c!K
if((c43?a?! CC (c53?;?!!
"3convert(c!K
printf("Jc","!K
98
Y
convert(;!
X
return ;289K
Y
Answer:
Compiler error
Explanation:
declaration of convert and format of getc(! are wrong.
O.! main(int argc, char **argv!
X
printf("enter the character"!K
getchar(!K
sum(argv[.],argv[9]!K
Y
sum(num.,num9!
int num.,num9K
X
return num.num9K
Y
Answer:
Compiler error.
Explanation:
argv[.] C argv[9] are strings. <hey are passed to the function sum without converting it to
integer values.
O9! L include 5stdio.h4
int one@d[]3X.,9,8YK
main(!
X
int *ptrK
ptr3one@dK
ptr38K
printf("Jd",*ptr!K
Y
Answer:
gar$age value
Explanation:
ptr pointer is pointing to out of the array range of one@d.
O8! L include5stdio.h4
aaa(! X
printf("hi"!K
Y
$$$(!X
printf("hello"!K
Y
ccc(!X
printf("$ye"!K
Y
main(!
X
9-
int (*ptr[8]!(!K
ptr[/]3aaaK
ptr[.]3$$$K
ptr[9]3cccK
ptr[9](!K
Y
Answer:
$ye
Explanation:
ptr is array of pointers to functions of return type int.ptr[/] is assigned to address of the
function aaa. 'imilarly ptr[.] and ptr[9] for $$$ and ccc respectively. ptr[9](! is in effect of
writing ccc(!, since ptr[9] points to ccc.
O7! Linclude5stdio.h4
main(!
X
*(AS *ptrK
char iK
ptr3fopen(";;;.c","r"!K
while((i3fgetch(ptr!!63SD*!
printf("Jc",i!K
Y
Answer:
contents of ;;;.c followed $y an infinite loop
Explanation:
<he condition is chec,ed against SD*, it should $e chec,ed against 1)AA.
O>! main(!
X
int i 3/KU3/K
if(i CC U!
printf("Jd..Jd",i,U!K
printf("Jd..Jd,i,U!K
Y
Answer:
/../
Explanation:
<he value of i is /. 'ince this information is enough to determine the truth value of the $oolean
e"pression. 'o the statement following the if statement is not e"ecuted. <he values of i and U
remain unchanged and get printed.

OM! main(!
X
int iK
i 3 a$c(!K
printf("Jd",i!K
Y
a$c(!
X
@B] 3 .///K
Y
Answer:
.///
97
Explanation:
1ormally the return value from the function is through the information from the accumulator.
%ere @B% is the pseudo glo$al varia$le denoting the accumulator. %ence, the value of the
accumulator is set ./// so the function returns value .///.
OO! int iK
main(!X
int tK
for ( t3-Kscanf("Jd",Ci!2tKprintf("JdNn",i!!
printf("Jd22",t22!K
Y
&& (f the inputs are /,.,9,8 find the o&p
Answer:
-22/
822.
9229
Explanation:
Aet us assume some "3 scanf("Jd",Ci!2t the values during e"ecution
will $e,
t i "
- / 2-
8 . 29
9 9 /

O0! main(!X
int a3 /Kint $ 3 9/Kchar " 3.Kchar y 3./K
if(a,$,",y!
printf("hello"!K
Y
Answer:
hello
Explanation:
<he comma operator has associativity from left to right. Dnly the rightmost value is returned
and the other values are evaluated and ignored. <hus the value of last varia$le y is returned to
chec, in if. 'ince it is a non ;ero value if $ecomes true so, "hello" will $e printed.
0/! main(!X
unsigned int iK
for(i3.Ki429Ki22!
printf("c aptitude"!K
Y
Explanation:
i is an unsigned integer. (t is compared with a signed value. 'ince the $oth types doesn?t match,
signed is promoted to unsigned value. <he unsigned eRuivalent of 29 is a huge value so
condition $ecomes false and control comes out of the loop.
0.! (n the following pgm add a stmt in the function fun such that the address of
?a? gets stored in ?U?.
main(!X
int * UK
void fun(int **!K
fun(CU!K
Y
9>
void fun(int **,! X
int a 3/K
&* add a stmt here*&
Y
Answer:
*, 3 Ca
Explanation:
<he argument of the function is a pointer to a pointer.

09! :hat are the following notations of defining functions ,nown as[
i. int a$c(int a,float $!
X
&* some code *&
Y
ii. int a$c(a,$!
int aK float $K
X
&* some code*&
Y
Answer:
i. B1'( C notation
ii. Zernighan C Eitche notation
08! main(!
X
char *pK
p3"JdNn"K
pK
pK
printf(p29,8//!K
Y
Answer:
8//
Explanation:
<he pointer points to J since it is incremented twice and again decremented $y 9, it points to
?JdNn? and 8// is printed.
0-! main(!X
char a[.//]K
a[/]3?a?Ka[.]]3?$?Ka[9]3?c?Ka[-]3?d?K
a$c(a!K
Y
a$c(char a[]!X
aK
printf("Jc",*a!K
aK
printf("Jc",*a!K
Y
Explanation:
<he $ase address is modified only in function and as a result a points to ?$? then after
incrementing to ?c? so $c will $e printed.

07! func(a,$!
9M
int a,$K
X
return( a3 (a33$! !K
Y
main(!
X
int process(!,func(!K
printf("<he value of process is Jd 6Nn ",process(func,8,>!!K
Y
process(pf,val.,val9!
int (*pf! (!K
int val.,val9K
X
return((*pf! (val.,val9!!K
Y
Answer:
<he value if process is / 6
Explanation:
<he function ?process? has 8 parameters 2 ., a pointer to another function 9 and 8, integers.
:hen this function is invo,ed from main, the following su$stitutions for formal parameters
ta,e place: func for pf, 8 for val. and > for val9. <his function returns the result of the
operation performed $y the function ?func?. <he function func has two integer parameters. <he
formal parameters are su$stituted as 8 for a and > for $. since 8 is not eRual to >, a33$ returns
/. therefore the function returns / which in turn is returned $y the function ?process?.
0>! void main(!
X
static int i37K
if(22i!X
main(!K
printf("Jd ",i!K
Y
Y
Answer:
/ / / /
Explanation:
<he varia$le "(" is declared as static, hence memory for ( will $e allocated for only once, as it
encounters the statement. <he function main(! will $e called recursively unless ( $ecomes eRual to /,
and since main(! is recursively called, so the value of static ( ie., / will $e printed every time the
control is returned.
0M! void main(!
X
int ,3ret(si;eof(float!!K
printf("Nn here value is Jd",,!K
Y
int ret(int ret!
X
ret 3 9.7K
return(ret!K
Y
Answer:
%ere value is M
9O
Explanation:
<he int ret(int ret!, ie., the function name and the argument name can $e the same.
*irstly, the function ret(! is called in which the si;eof(float! ie., - is passed, after the first
e"pression the value in ret will $e >, as ret is integer hence the value stored in ret will have implicit
type conversion from float to int. <he ret is returned in main(! it is printed after and preincrement.

0O! void main(!
X
char a[]3".98-7N/"K
int i3strlen(a!K
printf("here in 8 JdNn",i!K
Y
Answer:
here in 8 >
Explanation:
<he char array ?a? will hold the initiali;ed string, whose length will $e counted from / till the
null character. %ence the ?(? will hold the value eRual to 7, after the pre2increment in the printf
statement, the > will $e printed.
00! void main(!
X
unsigned giveit32.K
int gotitK
printf("Ju ",giveit!K
printf("Ju Nn",gotit322giveit!K
Y
Answer:
/ >7787
Explanation:
.//! void main(!
X
int iK
char a[]3"N/"K
if(printf("JsNn",a!!
printf("D, here Nn"!K
else
printf("*orget itNn"!K
Y
Answer:
D, here
Explanation:
Irintf will return how many characters does it print. %ence printing a null character
returns . which ma,es the if statement true, thus "D, here" is printed.

./.! void main(!
X
void *vK
int integer39K
int *i3CintegerK
v3iK
printf("Jd",(int*!*v!K
Y
90
Answer:
Compiler Srror. :e cannot apply indirection on type void*.
Explanation:
Qoid pointer is a generic pointer type. 1o pointer arithmetic can $e done on it. Qoid
pointers are normally used for,
.. Iassing generic pointers to functions and returning such pointers.
9. Bs a intermediate pointer type.
8. )sed when the e"act pointer type will $e ,nown at a later point of time.
./9! void main(!
X
int i3i,U3U,,3,K
printf(VJdJdJdW,i,U,,!K
Y
Answer:
#ar$age values.
Explanation:
Fn id%ntifi%r is avai$a'$% to &s% in pro=ram cod% from th% point of its d%c$aration.
'o e"pressions such as i 3 i are valid statements. <he i, U and , are automatic varia$les and
so they contain some gar$age value. Gar'a=% in is =ar'a=% o&t (G)GH).
./8! void main(!
X
static int i3i, U3U, ,3,K
printf(Vi 3 Jd U 3 Jd , 3 JdW, i, U, ,!K
Y
Answer:
i 3 . U 3 . , 3 .
Explanation:
'ince static varia$les are initiali;ed to ;ero $y default.
./-! void main(!
X
while(.!X
if(printf("Jd",printf("Jd"!!!
$rea,K
else
continueK
Y
Y
Answer:
#ar$age values
Explanation:
<he inner printf e"ecutes first to print some gar$age value. <he printf returns no of characters
printed and this value also cannot $e predicted. 'till the outer printf prints something and so
returns a non2;ero value. 'o it encounters the $rea, statement and comes out of the while
statement.
./-! main(!
X
unsigned int i3./K
while(i2243/!
8/
printf("Ju ",i!K
Y
Answer:
./ 0 O M > 7 - 8 9 . / >7787 >778-^..
Explanation:
'ince i is an unsigned integer it can never $ecome negative. 'o the e"pression i22 43/ will
always $e true, leading to an infinite loop.
./7! Linclude5conio.h4
main(!
X
int ",y39,;,aK
if("3yJ9! ;39K
a39K
printf("Jd Jd ",;,"!K
Y
Answer:
#ar$age2value /
Explanation:
<he value of yJ9 is /. <his value is assigned to ". <he condition reduces to if ("! or in other
words if(/! and so ; goes uninitiali;ed.
Thumb Rule: Chec, all control paths to write $ug free code.
./>! main(!
X
int a[./]K
printf("Jd",*a.2*a8!K
Y
Answer:
-
Explanation:
*a and 2*a cancels out. <he result is as simple as . 8 3 - 6
./M! Ldefine prod(a,$! a*$
main(!
X
int "38,y3-K
printf("Jd",prod("9,y2.!!K
Y
Answer:
./
Explanation:
<he macro e"pands and evaluates to as:
"9*y2. 34 "(9*y!2. 34 ./
./O! main(!
X
unsigned int i3>7///K
while(i63/!K
printf("Jd",i!K
Y
Answer:
8.
.
Explanation:
1ote the semicolon after the while statement. :hen the value of i $ecomes / it comes out of
while loop. +ue to post2increment on i the value of i while printing is ..

./0! main(!
X
int i3/K
while((i22!63/!
i23iK
printf("Jd",i!K
Y
Answer:
2.
Explanation:
*nar( + is th% on$( d&mm( op%rator in @. 'o it has no effect on the e"pression and now the
while loop is, while(i2263/! which is false and so $rea,s out of while loop. <he value P. is
printed due to the post2decrement operator.

..8! main(!
X
float f37,g3./K
enumXi3./,U39/,,37/YK
printf("JdNn",,!K
printf("JfNn",f559!K
printf("JlfNn",fJg!K
printf("JlfNn",fmod(f,g!!K
Y
Answer:
Aine no 7: Srror: Avalue reRuired
Aine no >: Cannot apply leftshift to float
Aine no M: Cannot apply mod to float
Explanation:
Snumeration constants cannot $e modified, so you cannot apply .
Bit2wise operators and J operators cannot $e applied on float values.
fmod(! is to find the modulus values for floats as J operator is for ints.
../! main(!
X
int i3./K
void pascal f(int,int,int!K
f(i,i,i!K
printf(" Jd",i!K
Y
void pascal f(integer :i,integer:U,integer :,!
X
write(i,U,,!K
Y
Answer:
Compiler error: un,nown type integer
Compiler error: undeclared function write
Explanation:
89
Iascal ,eyword doesnHt mean that pascal code can $e used. (t means that the function follows
Iascal argument passing mechanism in calling the functions.
...! void pascal f(int i,int U,int ,!
X
printf(VJd Jd JdW,i, U, ,!K
Y
void cdecl f(int i,int U,int ,!
X
printf(VJd Jd JdW,i, U, ,!K
Y
main(!
X
int i3./K
f(i,i,i!K
printf(" JdNn",i!K
i3./K
f(i,i,i!K
printf(" Jd",i!K
Y
Answer:
./ .. .9 .8
.9 .. ./ .8
Explanation:
Iascal argument passing mechanism forces the arguments to $e called from left to right. cdecl
is the normal C argument passing mechanism where the arguments are passed from right to left.

..9!. :hat is the output of the program given $elow
main(!
X
signed char i3/K
for(Ki43/Ki! K
printf("JdNn",i!K
Y
Answer
2.9O
Explanation
1otice the semicolon at the end of the for loop. <%e initial value of the i is set to /.
<he inner loop e"ecutes to increment the value from / to .9M (the positive range of
char! and then it rotates to the negative value of 2.9O. <he condition in the for loop
fails and so comes out of the for loop. (t prints the current value of i that is 2.9O.

..8! main(!
X
unsigned char i3/K
for(Ki43/Ki! K
printf("JdNn",i!K
Y
Answer
infinite loop
Explanation
88
<he difference $etween the previous Ruestion and this one is that the char is declared to $e
unsigned. 'o the i can never yield negative value and i43/ never $ecomes false so that it can come
out of the for loop.
..-! main(!
X
char i3/K
for(Ki43/Ki! K
printf("JdNn",i!K

Y
Answer:
Behavior is implementation dependent.
Explanation:
<he detail if the char is signed&unsigned $y default is implementation dependent. (f the
implementation treats the char to $e signed $y default the program will print P.9O and
terminate. Dn the other hand if it considers char to $e unsigned $y default, it goes to infinite
loop.
Rule:
Tou can write programs that have implementation dependent $ehavior. But dont write
programs that depend on such $ehavior.
..7! (s the following statement a declaration&definition. *ind what does it mean[
int (*"![./]K
Answer
+efinition.
" is a pointer to array of(si;e ./! integers.
Bpply cloc,2wise rule to find the meaning of this definition.
..>!. :hat is the output for the program given $elow
typedef enum error<ypeXwarning, error, e"ception,YerrorK
main(!
X
error g.K
g.3.K
printf("Jd",g.!K
Y
Answer
Compiler error: =ultiple declaration for error
Explanation
<he name error is used in the two meanings. Dne means that it is a enumerator
constant with value .. <he another use is that it is a type name (due to typedef! for enum
error<ype. #iven a situation the compiler cannot distinguish the meaning of error to ,now in
what sense the error is used:
error g.K
g.3errorK
&& which error it refers in each case[
:hen the compiler can distinguish $etween usages then it will not issue error (in pure
technical terms, names can only $e overloaded in different namespaces!.
Note: the e"tra comma in the declaration,
8-
enum error<ypeXwarning, error, e"ception,Y
is not an error. Bn e"tra comma is valid and is provided Uust for programmerHs convenience.


..M! typedef struct errorXint warning, error, e"ceptionKYerrorK
main(!
X
error g.K
g..error 3.K
printf("Jd",g..error!K
Y
Answer
.
Explanation
<he three usages of name errors can $e distinguisha$le $y the compiler at any instance, so
valid (they are in different namespaces!.
<ypedef struct errorXint warning, error, e"ceptionKYerrorK
<his error can $e used only $y preceding the error $y struct ,ayword as in:
struct error someSrrorK
typedef struct errorXint warning, error, e"ceptionKYerrorK
<his can $e used only after . (dot! or 24 (arrow! operator preceded $y the varia$le name as in :
g..error 3.K
printf("Jd",g..error!K
typedef struct errorXint warning, error, e"ceptionKYerrorK
<his can $e used to define varia$les without using the preceding struct ,eyword as in:
error g.K
'ince the compiler can perfectly distinguish $etween these three usages, it is perfectly legal and valid.
%ote
<his code is given here to Uust e"plain the concept $ehind. (n real programming donHt use such
overloading of names. (t reduces the reada$ility of the code. Iossi$le doesnHt mean that we should use
it6

..O! Lifdef something
int some3/K
Lendif
main(!
X
int thing 3 /K
printf("Jd JdNn", some ,thing!K
Y
Answer:
Compiler error : undefined sym$ol some
Explanation:
<his is a very simple e"ample for conditional compilation. <he name something is not
already ,nown to the compiler ma,ing the declaration
int some 3 /K
effectively removed from the source code.
..0! Lif something 33 /
87
int some3/K
Lendif
main(!
X
int thing 3 /K
printf("Jd JdNn", some ,thing!K
Y
Answer
/ /
Explanation
<his code is to show that preprocessor e"pressions are not the same as the ordinary
e"pressions. (f a name is not ,nown the preprocessor treats it to $e eRual to ;ero.
.9/!. :hat is the output for the following program
main(!
X
int arr9+[8][8]K
printf("JdNn", ((arr9+33* arr9+!CC(* arr9+ 33 arr9+[/]!! !K
Y
Answer
.
Explanation
<his is due to the close relation $etween the arrays and pointers. 1 dimensional arrays
are made up of (12.! dimensional arrays.
arr9+ is made up of a 8 single arrays that contains 8 integers each .
<he name arr9+ refers to the $eginning of all the 8 arrays. *arr9+ refers to the start of
the first .+ array (of 8 integers! that is the same address as arr9+. 'o the e"pression
(arr9+ 33 *arr9+! is true (.!.
'imilarly, *arr9+ is nothing $ut *(arr9+ /!, adding a ;ero doesnHt change the
value&meaning. Bgain arr9+[/] is the another way of telling *(arr9+ /!. 'o the
e"pression (*(arr9+ /! 33 arr9+[/]! is true (.!.
'ince $oth parts of the e"pression evaluates to true the result is true(.! and the same is
printed.
.9.! void main(!
X
if(_/ 33 (unsigned int!2.!
printf(VTou can answer this if you ,now how values are represented in memoryW!K
Y
Bnswer
Tou can answer this if you ,now how values are represented in memory
S"planation
8>
arr9+
arr9+[.]
arr9+[9]
arr9+[8]
_ (tilde operator or $it2wise negation operator! operates on / to produce all ones to fill
the space for an integer. P. is represented in unsigned value as all .Hs and so $oth are
eRual.
.99! int swap(int *a,int *$!
X
*a3*a*$K*$3*a2*$K*a3*a2*$K
Y
main(!
X
int "3./,y39/K
swap(C",Cy!K
printf(""3 Jd y 3 JdNn",",y!K
Y
Bnswer
" 3 9/ y 3 ./
S"planation
<his is one way of swapping two values. 'imple chec,ing will help understand this.
.98! main(!
X
char *p 3 VayRmWK
printf(VJcW,*(p!!K
Y
Bnswer:
$
.9-! main(!
X
int i37K
printf("Jd",i!K
Y
Answer:
Compiler error: Avalue reRuired in function main
Explanation:
i yields an rvalue. *or postfi" to operate an lvalue is reRuired.
.97! main(!
X
char *p 3 VayRmWK
char cK
c 3 *pK
printf(VJcW,c!K
Y
Answer:
$
Explanation:
<here is no difference $etween the e"pression *(p! and *p. Iarenthesis Uust
wor,s as a visual clue for the reader to see which e"pression is first evaluated.
.9>!
int aaa(! Xprintf(V%iW!KY
int $$$(!Xprintf(VhelloW!KY
8M
iny ccc(!Xprintf(V$yeW!KY
main(!
X
int ( * ptr[8]! (!K
ptr[/] 3 aaaK
ptr[.] 3 $$$K
ptr[9] 3cccK
ptr[9](!K
Y
Bnswer:
b#e
S"planation:
int (* ptr[8]!(! says that ptr is an array of pointers to functions that ta,es no arguments and
returns the type int. By the assignment ptr[/] 3 aaaK it means that the first function pointer in
the array is initiali;ed with the address of the function aaa. 'imilarly, the other two array
elements also get initiali;ed with the addresses of the functions $$$ and ccc. 'ince ptr[9]
contains the address of the function ccc, the call to the function ptr[9](! is same as calling
ccc(!. 'o it results in printing "$ye".
.9M!
main(!
X
int i37K
printf(VJdW,i3i 33>!K
Y
Answer:
1
Explanation:
<he e"pression can $e treated as i 3 (i33>!, $ecause 33 is of higher precedence than 3
operator. (n the inner e"pression, i is eRual to > yielding true(.!. %ence the result.
.9O! main(!
X
char p[ ]3"JdNn"K
p[.] 3 ?c?K
printf(p,>7!K
Y
Answer:
B
Explanation:
+ue to the assignment p[.] 3 GcH the string $ecomes, VJcNnW. 'ince this string $ecomes the
format string for printf and B'C(( value of >7 is GBH, the same gets printed.

.90! void ( * a$c( int, void ( *def! (! ! ! (!K
Bnswer::
a$c is a ptr to a function which ta,es 9 parameters .(a!. an integer varia$le.($!. a ptrto a
funtion which returns void. the return type of the function is void.
Explanation:
Bpply the cloc,2wise rule to find the result.
8O
.8/! main(!
X
while (strcmp(VsomeW,WsomeN/W!!
printf(V'trings are not eRualNnW!K
Y
Answer:
1o output
Explanation:
Snding the string constant with N/ e"plicitly ma,es no difference. 'o VsomeW and VsomeN/W are
eRuivalent. 'o, strcmp returns / (false! hence $rea,ing out of the while loop.
.8.! main(!
X
char str.[] 3 XGsH,HoH,HmH,HeHYK
char str9[] 3 XGsH,HoH,HmH,HeH,HN/HYK
while (strcmp(str.,str9!!
printf(V'trings are not eRualNnW!K
Y
Answer:
V'trings are not eRualW
V'trings are not eRualW
^.
Explanation:
(f a string constant is initiali;ed e"plicitly with characters, GN/H is not appended automatically
to the string. 'ince str. doesnHt have null termination, it treats whatever the values that are in
the following positions as part of the string until it randomly reaches a GN/H. 'o str. and str9 are
not the same, hence the result.

.89! main(!
X
int i 3 8K
for (Ki3/K! printf(VJdW,i!K
Y
Answer:
Compiler Srror: Avalue reRuired.
Explanation:
Bs we ,now that increment operators return rvalues and hence it cannot appear on the
left hand side of an assignment operation.

.88! void main(!
X
int *mptr, *cptrK
mptr 3 (int*!malloc(si;eof(int!!K
printf(VJdW,*mptr!K
int *cptr 3 (int*!calloc(si;eof(int!,.!K
printf(VJdW,*cptr!K
Y
Answer:
gar$age2value /
Explanation:
80
<he memory space allocated $y malloc is uninitiali;ed, whereas calloc returns the allocated
memory space initiali;ed to ;eros.
.8-! void main(!
X
static int iK
while(i53./!
(i49![i:i22K
printf(VJdW, i!K
Y
Answer:
89M>M
Explanation:
'ince i is static it is initiali;ed to /. (nside the while loop the conditional operator evaluates to
false, e"ecuting i22. <his continues till the integer value rotates to positive value (89M>M!. <he
while condition $ecomes false and hence, comes out of the while loop, printing the i value.
.87! main(!
X
int i3./,U39/K
U 3 i, U[(i,U![i:U:UK
printf("Jd Jd",i,U!K
Y
Answer:
./ ./
Explanation:
<he <ernary operator ( [ : ! is eRuivalent for if2then2else statement. 'o the Ruestion can $e
written as:
if(i,U!
X
if(i,U!
U 3 iK
else
U 3 UK
Y
else
U 3 UK
.8>! .. const char *aK
9. char* const aK
8. char const *aK
2+ifferentiate the a$ove declarations.
Answer:
.. ?const? applies to char * rather than ?a? ( pointer to a constant char !
*a3?*? : illegal
a3"%i" : legal
9. ?const? applies to ?a? rather than to the value of a (constant pointer to char !
*a3?*? : legal
a3"%i" : illegal
-/
8. 'ame as ..
.8M! main(!
X
int i37,U3./K
i3iC3UCC./K
printf("Jd Jd",i,U!K
Y
Answer:
. ./
Explanation:
<he e"pression can $e written as i3(iC3(UCC./!!K <he inner e"pression (UCC./! evaluates to
. $ecause U33./. i is 7. i 3 7C. is .. %ence the result.
.8O! main(!
X
int i3-,U3MK
U 3 U FF i CC printf("TD) CB1"!K
printf("Jd Jd", i, U!K
Y
Answer:
- .
Explanation:
Ih% 'oo$%an %3pr%ssion n%%ds to '% %va$&at%d on$( ti$$ th% tr&th va$&% of th% %3pr%ssion is not
5no;n. U is not eRual to ;ero itself means that the e"pressionHs truth value is .. Because it is
followed $y FF and tr&% 88 (an(thin=) =C tr&% ;h%r% (an(thin=) ;i$$ not '% %va$&at%d. 'o the
remaining e"pression is not evaluated and so the value of i remains the same.
'imilarly when CC operator is involved in an e"pression, when any of the operands $ecome
false, the whole e"pressionHs truth value $ecomes false and hence the remaining e"pression
will not $e evaluated.
fa$s% 77 (an(thin=) =C fa$s% ;h%r% (an(thin=) ;i$$ not '% %va$&at%d.
.80! main(!
X
register int a39K
printf("Bddress of a 3 Jd",Ca!K
printf("Qalue of a 3 Jd",a!K
Y
Answer:
Compier Srror: ?C? on register varia$le
Rule to Remember:
& (address of ) operator cannot be applied on register variables
.-/! main(!
X
float i3..7K
switch(i!
X
case .: printf("."!K
case 9: printf("9"!K
-.
default : printf("/"!K
Y
Y
Answer:
Compiler Srror: switch e"pression not integral
Explanation:
J;itch stat%m%nts can '% app$i%d on$( to int%=ra$ t(p%s.
.-.! main(!
X
e"tern iK
printf("JdNn",i!K
X
int i39/K
printf("JdNn",i!K
Y
Y
Answer:
Ain,er Srror : )nresolved e"ternal sym$ol i
Explanation:
<he identifier i is availa$le in the inner $loc, and so using e"tern has no use in resolving it.
.-9! main(!
X
int a39,*f.,*f9K
f.3f93CaK
*f93*f93a39.7K
printf("NnJd Jd Jd",a,*f.,*f9!K
Y
Answer:
.> .> .>
Explanation:
f. and f9 $oth refer to the same memory location a. 'o changes through f. and f9 ultimately
affects only the value of a.
.-8! main(!
X
char *p3"#DD+"K
char a[ ]3"#DD+"K
printf("Nn si;eof(p! 3 Jd, si;eof(*p! 3 Jd, strlen(p! 3 Jd", si;eof(p!, si;eof(*p!, strlen(p!!K
printf("Nn si;eof(a! 3 Jd, strlen(a! 3 Jd", si;eof(a!, strlen(a!!K
Y
Answer:
si;eof(p! 3 9, si;eof(*p! 3 ., strlen(p! 3 -
si;eof(a! 3 7, strlen(a! 3 -
Explanation:
si;eof(p! 34 si;eof(char*! 34 9
si;eof(*p! 34 si;eof(char! 34 .
'imilarly,
si;eof(a! 34 si;e of the character array 34 7
?h%n si9%of op%rator is app$i%d to an arra( it r%t&rns th% si9%of th% arra( and it is not the same
as the si;eof the pointer varia$le. %ere the si;eof(a! where a is the character array and the si;e
-9
of the array is 7 $ecause the space necessary for the terminating 1)AA character should also
$e ta,en into account.
.--! Ldefine +(=( array, type! si;eof(array!&si;eof(type!
main(!
X
int arr[./]K
printf(V<he dimension of the array is JdW, +(=(arr, int!!K
Y
Answer:
./
Explanation:
<he si;e of integer array of ./ elements is ./ * si;eof(int!. <he macro e"pands to
si;eof(arr!&si;eof(int! 34 ./ * si;eof(int! & si;eof(int! 34 ./.
.-7! int +(=(int array[]!
X
return si;eof(array!&si;eof(int !K
Y
main(!
X
int arr[./]K
printf(V<he dimension of the array is JdW, +(=(arr!!K
Y
Answer:
.
Explanation:
Frra(s cannot '% pass%d to f&nctions as ar=&m%nts and on$( th% point%rs can '% pass%d. 'o the
argument is eRuivalent to int * array (this is one of the very few places where [] and * usage
are eRuivalent!. <he return statement $ecomes, si;eof(int *!& si;eof(int! that happens to $e
eRual in this case.
.->! main(!
X
static int a[8][8]3X.,9,8,-,7,>,M,O,0YK
int i,UK
static *p[]3Xa,a.,a9YK
for(i3/Ki58Ki!
X
for(U3/KU58KU!
printf("JdNtJdNtJdNtJdNn",*(*(pi!U!,
*(*(Up!i!,*(*(ip!U!,*(*(pU!i!!K
Y
Y
Answer:
. . . .
9 - 9 -
8 M 8 M
- 9 - 9
7 7 7 7
> O > O
M 8 M 8
O > O >
-8
0 0 0 0
Explanation:
*(*(pi!U! is eRuivalent to p[i][U].
.-M! main(!
X
void swap(!K
int "3./,y3OK
swap(C",Cy!K
printf(""3Jd y3Jd",",y!K
Y
void swap(int *a, int *$!
X
*a \3 *$, *$ \3 *a, *a \3 *$K
Y
Answer:
"3./ y3O
Explanation:
)sing \ li,e this is a way to swap two varia$les without using a temporary varia$le and that
too in a single statement.
(nside main(!, void swap(!K means that swap is a function that may ta,e any num$er of
arguments (not no arguments! and returns nothing. 'o this doesnHt issue a compiler error $y
the call swap(C",Cy!K that has two arguments.
<his convention is historically due to pre2B1'( style (referred to as Zernighan and Eitchie
style! style of function declaration. (n that style, the swap function will $e defined as follows,
void swap(!
int *a, int *$
X
*a \3 *$, *$ \3 *a, *a \3 *$K
Y
where the arguments follow the (!. 'o naturally the declaration for swap will loo, li,e, void
swap(! which means the swap can ta,e any num$er of arguments.
.-O! main(!
X
int i 3 97MK
int *iItr 3 CiK
printf("Jd Jd", *((char*!iItr!, *((char*!iItr.! !K
Y
Answer:
. .
Explanation:
<he integer value 97M is stored in the memory as, ///////. ///////., so the individual $ytes
are ta,en $y casting it to char * and get printed.
.-0! main(!
X
int i 3 97OK
int *iItr 3 CiK
printf("Jd Jd", *((char*!iItr!, *((char*!iItr.! !K
Y
Answer:
9 .
--
Explanation:
<he integer value 97M can $e represented in $inary as, ///////. ///////.. Eemem$er that
the (1<SA machines are Gsmall2endianH machines. Jma$$,%ndian m%ans that th% $o;%r ord%r
'(t%s ar% stor%d in th% hi=h%r m%mor( addr%ss%s and th% hi=h%r ord%r '(t%s ar% stor%d in $o;%r
addr%ss%s. <he integer value 97O is stored in memory as: ///////. //////./.
.7/! main(!
X
int i38//K
char *ptr 3 CiK
*ptr39K
printf("Jd",i!K
Y
Answer:
77>
Explanation:
<he integer value 8// in $inary notation is: ///////. //./..//. (t is stored in memory
(small2endian! as: //./..// ///////.. Eesult of the e"pression *ptr 3 9 ma,es the
memory representation as: //./..// //////./. 'o the integer corresponding to it is
//////./ //./..// 34 77>.
.7.! Linclude 5stdio.h4
main(!
X
char * str 3 "hello"K
char * ptr 3 strK
char least 3 .9MK
while (*ptr!
least 3 (*ptr5least ! [*ptr :leastK
printf("Jd",least!K
Y
Answer:
/
Explanation:
Bfter GptrH reaches the end of the string the value pointed $y GstrH is GN/H. 'o the value of GstrH is
less than that of GleastH. 'o the value of GleastH finally is /.
.79! +eclare an array of 1 pointers to functions returning pointers to functions returning pointers to
characters[
Answer:
(char*(*!( !! (*ptr[1]!( !K
.78! main(!
X
struct student
X
char name[8/]K
struct date do$K
YstudK
struct date
X
int day,month,yearK
YK
-7
scanf("JsJdJdJd", stud.rollno, Cstudent.do$.day, Cstudent.do$.month,
Cstudent.do$.year!K
Y
Answer:
Compiler Srror: )ndefined structure date
Explanation:
(nside the struct definition of GstudentH the mem$er of type struct date is given. <he compiler
doesnHt have the definition of date structure (forward reference is not allowed in C in this
case! so it issues an error.
.7-! main(!
X
struct dateK
struct student
X
char name[8/]K
struct date do$K
YstudK
struct date
X
int day,month,yearK
YK
scanf("JsJdJdJd", stud.rollno, Cstudent.do$.day, Cstudent.do$.month, Cstudent.do$.year!K
Y
Answer:
Compiler Srror: )ndefined structure date
Explanation:
Dnly declaration of struct date is availa$le inside the structure definition of GstudentH $ut to
have a varia$le of type struct date the definition of the structure is reRuired.
.77! <here were ./ records stored in Vsomefile.datW $ut the following program printed .. names. :hat
went wrong[
void main(!
X
struct student
X
char name[8/], rollno[>]K
YstudK
*(AS *fp 3 fopen(Vsomefile.datW,WrW!K
while(6feof(fp!!
X
fread(Cstud, si;eof(stud!, . , fp!K
puts(stud.name!K
Y
Y
Explanation:
fread reads ./ records and prints the names successfully. (t will return SD* only when
fread tries to read another record and fails reading SD* (and returning SD*!. 'o it
prints the last record again. Bfter this only the condition feof(fp! $ecomes false, hence
comes out of the while loop.
.7>! (s there any difference $etween the two declarations,
.. int foo(int *arr[]! and
->
9. int foo(int *arr[9]!
Answer:
1o
Explanation:
*unctions can only pass pointers and not arrays. <he num$ers that are allowed inside the [] is
Uust for more reada$ility. 'o there is no difference $etween the two declarations.
.7M! :hat is the su$tle error in the following code segment[
void fun(int n, int arr[]!
X
int *p3/K
int i3/K
while(i5n!
p 3 Carr[i]K
*p 3 /K
Y
Answer & Explanation:
(f the $ody of the loop never e"ecutes p is assigned no address. 'o p remains 1)AA
where *p 3/ may result in pro$lem (may rise to runtime error V1)AA pointer
assignmentW and terminate the program!.
.7O! :hat is wrong with the following code[
int *foo(!
X
int *s 3 malloc(si;eof(int!.//!K
assert(s 63 1)AA!K
return sK
Y
Answer & Explanation:
assert macro should $e used for de$ugging and finding out $ugs. <he chec, s 63 1)AA is for
error&e"ception handling and for that assert shouldnHt $e used. B plain if and the corresponding
remedy statement has to $e given.
.70! :hat is the hidden $ug with the following statement[
assert(val 63 /!K
Answer & Explanation:
Bssert macro is used for de$ugging and removed in release version. (n assert, the e"perssion
involves side2effects. 'o the $ehavior of the code $ecomes different in case of de$ug version
and the release version thus leading to a su$tle $ug.
Rule to Remember:
KonLt &s% %3pr%ssions that hav% sid%,%ff%cts in ass%rt stat%m%nts.
.>/! void main(!
X
int *i 3 /"-//K && i points to the address -//
*i 3 /K && set the value of memory location pointed $y iK
Y
Answer:
)ndefined $ehavior
Explanation:
-M
<he second statement results in undefined $ehavior $ecause it points to some location whose
value may not $e availa$le for modification. Ihis t(p% of point%r in ;hich th% non,avai$a'i$it(
of th% imp$%m%ntation of th% r%f%r%nc%d $ocation is 5no;n as Dincomp$%t% t(p%D.
.>.! Ldefine assert(cond! if(6(cond!! N
(fprintf(stderr, "assertion failed: Js, file Js, line Jd Nn",Lcond,N
@@*(AS@@,@@A(1S@@!, a$ort(!!
void main(!
X
int i 3 ./K
if(i33/!
assert(i 5 .//!K
else
printf("<his statement $ecomes else for if in assert macro"!K
Y
Bnswer:
1o output
Explanation:
<he else part in which the printf is there $ecomes the else for if in the assert macro. %ence nothing is
printed.
<he solution is to use conditional operator instead of if statement,
Ldefine assert(cond! ((cond![(/!: (fprintf (stderr, "assertion failed: N Js, file Js, line Jd Nn",Lcond,
@@*(AS@@,@@A(1S@@!, a$ort(!!!
1ote:
%owever this pro$lem of Vmatching with nearest elseW cannot $e solved $y the usual method
of placing the if statement inside a $loc, li,e this,
Ldefine assert(cond! X N
if(6(cond!! N
(fprintf(stderr, "assertion failed: Js, file Js, line Jd Nn",Lcond,N
@@*(AS@@,@@A(1S@@!, a$ort(!! N
Y
.>9! (s the following code legal[
struct a
X
int "K
struct a $K
Y
Bnswer:
1o
Explanation:
(s it not legal for a structure to contain a mem$er that is of the same
type as in this case. Because this will cause the structure declaration to $e recursive without
end.
.>8! (s the following code legal[
struct a
X
int "K
struct a *$K
Y
-O
Answer:
Tes.
Explanation:
*$ is a pointer to type struct a and so is legal. <he compiler ,nows, the si;e of the pointer to a
structure even $efore the si;e of the structure
is determined(as you ,now the pointer to any type is of same si;e!. <his type of structures is
,nown as Gself2referencingH structure.
.>-! (s the following code legal[
typedef struct a
X
int "K
a<ype *$K
Ya<ype
Answer:
1o
Explanation:
<he typename a<ype is not ,nown at the point of declaring the structure (forward references
are not made for typedefs!.
.>7! (s the following code legal[
typedef struct a a<ypeK
struct a
X
int "K
a<ype *$K
YK
Answer:
Tes
Explanation:
<he typename a<ype is ,nown at the point of declaring the structure, $ecause it is already
typedefined.
.>>! (s the following code legal[
void main(!
X
typedef struct a a<ypeK
a<ype someQaria$leK
struct a
X
int "K
a<ype *$K
YK
Y
Answer:
1o
Explanation:
:hen the declaration,
typedef struct a a<ypeK
is encountered $ody of struct a is not ,nown. <his is ,nown as Gincomplete typesH.

.>M! void main(!
X
-0
printf(Vsi;eof (void *! 3 Jd NnV, si;eof( void *!!K
printf(Vsi;eof (int *! 3 Jd NnW, si;eof(int *!!K
printf(Vsi;eof (dou$le *! 3 Jd NnW, si;eof(dou$le *!!K
printf(Vsi;eof(struct un,nown *! 3 Jd NnW, si;eof(struct un,nown *!!K
Y
Answer :
si;eof (void *! 3 9
si;eof (int *! 3 9
si;eof (dou$le *! 3 9
si;eof(struct un,nown *! 3 9
Explanation:
<he pointer to any type is of same si;e.
.>O! char input'tring[.//] 3 X/YK
<o get string input from the ,ey$oard which one of the following is $etter[
.! gets(input'tring!
9! fgets(input'tring, si;eof(input'tring!, fp!
Answer & Explanation:
<he second one is $etter $ecause gets(input'tring! doesn?t ,now the si;e of the string passed
and so, if a very $ig input (here, more than .// chars! the charactes will $e written past the
input string. :hen fgets is used with stdin performs the same operation as gets $ut is safe.
.>0! :hich version do you prefer of the following two,
.! printf(VJsW,str!K && or the more curt one
9! printf(str!K
Answer & Explanation:
Irefer the first one. (f the str contains any format characters li,e Jd then it will result in a
su$tle $ug.
.M/! void main(!
X
int i3./, U39K
int *ip3 Ci, *Up 3 CUK
int , 3 *ip&*UpK
printf(VJdW,,!K
Y
Answer:
Compiler Srror: V)ne"pected end of file in comment started in line 7W.
Explanation:
<he programmer intended to divide two integers, $ut $y the Vma"imum munchW rule,
the compiler treats the operator seRuence & and * as &* which happens to $e the starting
of comment. <o force what is intended $y the programmer,
int , 3 *ip& *UpK
&& give space e"plicity separating & and *
&&or
int , 3 *ip&(*Up!K
&& put $races to force the intention
will solve the pro$lem.
.M.! void main(!
X
char chK
for(ch3/Kch53.9MKch!
7/
printf(VJc Jd NnV, ch, ch!K
Y
Answer:
(mplementaion dependent
Explanation:
<he char type may $e signed or unsigned $y default. (f it is signed then ch is e"ecuted after
ch reaches .9M and rotates $ac, to 2.9O. <hus ch is always smaller than .9M.
.M9! (s this code legal[
int *ptrK
ptr 3 (int *! /"-//K
Answer:
Tes
Explanation:
<he pointer ptr will point at the integer in the memory location /"-//.
.M8! main(!
X
char a[-]3"%SAAD"K
printf("Js",a!K
Y
Answer:
Compiler error: <oo many initiali;ers
Explanation:
<he array a is of si;e - $ut the string constant reRuires > $ytes to get stored.
.M-! main(!
X
char a[-]3"%SAA"K
printf("Js",a!K
Y
Answer:
%SAAJ`6_`6`[[[`__6
Explanation:
<he character array has the memory Uust enough to hold the string V%SAAW and doesnt have
enough space to store the terminating null character. 'o it prints the %SAA correctly and
continues to print gar$age values till it accidentally comes across a 1)AA character.
.M7! main(!
X
int a3./,*UK
void *,K
U3,3CaK
UK
,K
printf("Nn Ju Ju ",U,,!K
Y
Answer:
Compiler error: Cannot increment a void pointer
Explanation:
Qoid pointers are generic pointers and they can $e used only when the type is not ,nown and
as an intermediate address storage type. 1o pointer arithmetic can $e done on it and you
cannot apply indirection operator (*! on void pointers.
7.
.M>! main(!
X
e"tern int iK
X int i39/K
X
const volatile unsigned i38/K printf("Jd",i!K
Y
printf("Jd",i!K
Y
printf("Jd",i!K
Y
int iK
.MM! Irintf can $e implemented $y using @@@@@@@@@@ list.
Answer:
Qaria$le length argument lists
.MO! char *some*un(!
X
char *temp 3 Vstring constant"K
return tempK
Y
int main(!
X
puts(some*un(!!K
Y
Bnswer:
string constant
Explanation:
<he program suffers no pro$lem and gives the output correctly $ecause the character constants are
stored in code&data area and not allocated in stac,, so this doesnHt lead to dangling pointers.
.M0! char *some*un.(!
X
char temp[ ] 3 Vstring"K
return tempK
Y
char *some*un9(!
X
char temp[ ] 3 XGsH, GtH,HrH,HiH,HnH,HgHYK
return tempK
Y
int main(!
X
puts(some*un.(!!K
puts(some*un9(!!K
Y
Bnswer:
#ar$age values.
Explanation:
Both the functions suffer from the pro$lem of dangling pointers. (n some*un.(! temp is a character
array and so the space for it is allocated in heap and is initiali;ed with character string VstringW. <his is created
dynamically as the function is called, so is also deleted dynamically on e"iting the function so the string data is
79
not availa$le in the calling function main(! leading to print some gar$age values. <he function some*un9(! also
suffers from the same pro$lem $ut the pro$lem can $e easily identified in this case.
78

S-ar putea să vă placă și