Note : All the programs are tested under Turbo C/C++ compilers. It is assumed that, Programs run under DOS environment, The underling machine is an !"# sstem, Program is compiled using Turbo C/C++ compiler. The program output ma depend on the in$ormation based on this assumptions %$or e!ample si&eo$%int' (( ) ma be assumed'. Predict the output or error%s' $or the $ollo*ing+ 1. void main() { int const * p=5; printf("%d",++(*p)); } Answer:Compiler error+ Cannot modi$ a constant value. Explanation+ p is a pointer to a ,constant integer,. -ut *e tried to change the value o$ 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/, 0%i+s', 0%s+i', i.s/ are all di$$erent *as o$ e!pressing the same idea. 1enerall arra name is the base address $or that arra. 2ere s is the base address. i is the inde! number/displacement $rom the base address. So, indirecting it *ith 0 is same as s.i/. i.s/ ma be surprising. -ut in the case o$ 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: I hate 3 Explanation+ 4or $loating point numbers ($loat, double, long double the values cannot be predicted e!actl. Depending on the number o$ btes, the precession *ith o$ the value represented varies. 4loat ta5es 6 btes and long double ta5es 78 btes. So $loat stores 8.9 *ith less precision than long double. Ru!e "# T$u%&' :ever compare or at;least be cautious *hen using $loating point numbers *ith relational operators ((( , <, =, =(, <(,>( . +. main() {s tatic int var = 5; printf("%d ",var,,); if(var) main(); } Answer: ? 6 @ ) 7 Explanation: Ahen static storage class is given, it is initiali&ed once. The change in the value o$ a static variable is retained even bet*een the $unction calls. Bain is also treated li5e an other ordinar $unction, *hich can be called recursivel. 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: ) ) ) ) ) ) @ 6 # ? Explanation: Initiall pointer c is assigned to both p and (. In the $irst loop, since onl ( is incremented and not c , the value ) *ill be printed ? times. In second loop p itsel$ is incremented. So the values ) @ 6 # ? *ill be printed. .. main() { %3t%rn int i; i=2!; printf("%d",i); } Answer: 4in5%r 6rror + 3nde$ined smbol CDiC Explanation: e!tern storage class in the $ollo*ing declaration, e)tern int i* speci$ies to the compiler that the memor $or i is allocated in some other program and that address *ill be given to the current program at the time o$ lin5ing. -ut lin5er $inds that no other variable o$ name i is available in an other program *ith memor space allocated $or it. 2ence a lin5er 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 8 7 @ 7 Explanation : Eogical operations al*as give a result o$ + "r , . And also the logical A:D %FF' operator has higher priorit over the logical OG %HH' operator. So the e!pression Ii-- .. /-- .. 0- - is e!ecuted $irst. The result o$ this e!pression is 8 %;7 FF ;7 FF 8 ( 8'. :o* the e!pression is 8 HH ) *hich evaluates to 7 %because OG operator al*as gives 7 e!cept $or I8 HH 8J combination; $or *hich it gives 8'. So the value o$ m is 7. The values o$ other variables are also incremented b 7. -. main() { char *p; printf("%d %d ",si9%of(*p),si9%of(p)); } Answer: 7 ) Explanation: The si&eo$%' operator gives the number o$ btes ta5en b its operand. P is a character pointer, *hich needs one bte $or storing its value %a character'. 2ence si&eo$%0p' gives a value o$ 7. Since it needs t*o btes to store the address o$ the character pointer si&eo$%p' gives ). :. 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 : The de$ault case can be placed an*here inside the loop. It is e!ecuted onl *hen all other cases doesnCt match. 1!. main() { printf("%3",,122+); } Answer: $$$8 Explanation : ;7 is internall represented as all 7Cs. Ahen le$t shi$ted $our times the least signi$icant 6 bits are $illed *ith 8Cs.The K! $ormat speci$ier speci$ies that the integer value be printed as a he!adecimal value. 11. main() { char strin=[=">%$$o ?or$d"; disp$a((strin=); } void disp$a((char *strin=) { printf("%s",strin=); } Answer:@ompi$%r 6rror < Tpe mismatch in redeclaration o$ $unction displa Explanation : In third line, *hen the $unction disp!a1 is encountered, the compiler doesnCt 5no* anthing about the $unction displa. It assumes the arguments and return tpes to be integers, %*hich is the de$ault tpe'. Ahen it sees the actual $unction disp!a12 the arguments and tpe contradicts *ith *hat it has assumed reviousl. 2ence a compile time error occurs. 12. main() { int c=, ,2; printf("c=%d",c); } Answer: c()L Explanation: 2ere unar minus %or negation' operator is used t*ice. Same maths rules applies, ie. Binus 0 minus( plus. N"te' 2o*ever ou cannot give li5e ;;). -ecause ;; operator can onl be applied to variables as a de3re%ent operator %eg., i;;'. ) is a constant and not a variable. 1#. Ad%fin% int char main() { int i=.5; printf("si9%of(i)=%d",si9%of(i)); } Answer: si&eo$%i'(7 Explanation: Since the Mde$ine replaces the string int b the macro 3$ar 1+. main() { int i=1!; i=BiC1+; Drintf ("i=%d",i); } Answer: i(8 Explanation: In the e!pression 4i5+6 , :OT %>' operator has more precedence than I <J smbol. 4 is a unar logical operator. >i %>78' is 8 %not o$ true is $alse'. 8<76 is $alse %&ero'. 15. Ainc$&d%2stdio.hC main() { char s[={EaE,E'E,EcE,E"nE,EcE,E"!E}; char *p,*str,*str1; p=7s[#; str=p; str1=s; printf("%d",++*p + ++*str1,#2); } Answer: NN Explanation: p is pointing to character COnC. str7 is pointing to character CaC ++0p. ,p is pointing to COnC and that is incremented b one., the ASCII value o$ COnC is 78, *hich is then incremented to 77. The value o$ ++0p is 77. ++0str7, str7 is pointing to CaC that is incremented b 7 and it becomes CbC. ASCII value o$ CbC is 9". :o* per$orming %77 + 9" P @)', *e get NN%,B,'L So *e get the output NN ++ ,B, %Ascii is NN'. 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:Some1arbageQalue;;;7 Explanation: p(Fa.)/.)/.)/ ou declare onl t*o )D arras, but ou are tring to access the third )D%*hich ou are not declared' it *ill print garbage values. 0R(000a starting address o$ a is assigned integer pointer. :o* R is pointing to starting address o$ a. I$ ou print 0R, it *ill print $irst element o$ @D arra. 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 variables in declaration 1-. Ainc$&d%2stdio.hC main() { str&ct 33 { int 3; str&ct (( { char s; str&ct 33 *p; }; str&ct (( *1; }; } Answer:Compiler Srror Explanation: The structure is nested *ithin structure !!. 2ence, the elements are o$ are to be accessed through the instance o$ structure !!, *hich needs an instance o$ to be 5no*n. I$ the instance is created a$ter de$ining the structure the compiler *ill not 5no* about the instance relative to !!. 2ence $or nested structure ou have to declare member. 1:. main() { printf(""na'"); printf(""'si"); printf(""rha"); } Answer: hai Explanation: On ; ne*line Ob ; bac5space Or ; line$eed 2!. main() { int i=5; printf("%d%d%d%d%d%d",i++,i,,,++i,,,i,i); } Answer: 6??6? Explanation: The arguments in a $unction call are pushed into the stac5 $rom le$t to right. The evaluation is b popping out $rom the stac5. and the evaluation is $rom right to le$t, hence the result. 21. Ad%fin% s1&ar%(3) 3*3 main() { int i; i = .+Fs1&ar%(+); printf("%d",i); } Answer: #6 Explanation: the macro call sRuare%6' *ill substituted b 606 so the e!pression becomes i ( #6/606 . Since / and 0 has eRual priorit the e!pression *ill be evaluated as %#6/6'06 i.e. 7#06 ( #6 22. main() { char *p="hai fri%nds",*p1; p1=p; ;hi$%(*pB=E"!E) ++*p++; printf("%s %s",p,p1); } Answer: ibU>gsU$oet Explanation: ++0p++ *ill be parse in the given order 0p that is value at the location currentl pointed b p *ill be ta5en ++0p the retrieved value *ill be incremented *hen L is encountered the location *ill be incremented that is p++ *ill be e!ecuted 2ence, in the *hile loop initial value pointed b p is IhJ, *hich is changed to IiJ b e!ecuting ++0p and pointer moves to point, IaJ *hich is similarl changed to IbJ and so on. Similarl blan5 space is converted to I>J. Thus, *e obtain value in p becomes VibU>gsU$oetW and since p reaches IO8J and p7 points to p thus p7doesnot print anthing. 2#. Ainc$&d% 2stdio.hC Ad%fin% a 1! main() { Ad%fin% a 5! printf("%d",a); } Answer: ?8 Explanation: The preprocessor directives can be rede$ined an*here in the program. So the most recentl assigned value *ill be ta5en. 2+. Ad%fin% c$rscr() 1!! main() { c$rscr(); printf("%d"n",c$rscr()); } Answer: 788 Explanation: Preprocessor e!ecutes as a seperate pass be$ore the e!ecution o$ the compiler. So te!tual replacement o$ clrscr%' to 788 occurs.The input program to compiler loo5s li5e this + main%' X 788L print$%,KdOn,,788'L Y N"te' 788L is an e!ecutable statement but *ith no action. So it doesnCt give an problem 25. main() { +1printf("%p",main); }-Answer: Some address *ill be printed. Explanation: 4unction names are Uust addresses %Uust li5e arra names are addresses'. main%' is also a $unction. So the address o$ $unction main *ill be printed. Kp in print$ speci$ies that the argument is an address. The are printed as he!adecimal numbers. )N' main%' X clrscr%'L Y clrscr%'L Answer: :o output/error Explanation: The $irst clrscr%' occurs inside a $unction. So it becomes a $unction call. In the second clrscr%'L is a $unction declaration %because it is not inside an $unction'. )"' enum colors X-EACZ,-E3S,1GSS:Y main%' X print$%,Kd..Kd..Kd,,-EACZ,-E3S,1GSS:'L return%7'L Y Answer: 8..7..) Explanation: enum assigns numbers starting $rom 8, i$ not e!plicitl de$ined. )9' void main%' X char $ar 0$arther,0$arthestL print$%,Kd..Kd,,si&eo$%$arther',si&eo$%$arthest''L Y Answer: 6..) Explanation: the second pointer is o$ char tpe and not a $ar pointer @8' main%' X int i(688,U(@88L print$%,Kd..Kd,'L Y Answer: 688..@88 Explanation: print$ ta5es the values o$ the $irst t*o assignments o$ the program. An number o$ print$Cs ma be given. All o$ them ta5e onl the $irst t*o values. I$ more number o$ assignments given in the program,then print$ *ill ta5e garbage values. @7' main%' X char 0pL p(,2ello,L print$%,KcOn,,0F0p'L Y Answer:2 Explanation: 0 is a dere$erence operator F is a re$erence operator. The can be applied an number o$ times provided it is meaning$ul. 2ere p points to the $irst character in the string ,2ello,. 0p dere$erences it and so its value is 2. Again F re$erences it to an address and 0 dere$erences it to the value 2. @)' main%' X int i(7L *hile %i=(?' X print$%,Kd,,i'L i$ %i<)' goto hereL i++L Y Y $un%' X here+ print$%,PP,'L Y Answer:Compiler error+ 3nde$ined label ChereC in $unction main Explanation: Eabels have $unctions scope, in other *ords the scope o$ the labels is limited to $unctions. The label ChereC is available in $unction $un%' 2ence it is not visible in $unction main. @@' main%' X static char names.?/.)8/(X,pascal,,,ada,,,cobol,,,$ortran,,,perl,YL int iL char 0tL t(names.@/L names.@/(names.6/L names.6/(tL $or %i(8Li=(6Li++' print$%,Ks,,names.i/'L Y Answer:Compiler error+ Evalue reRuired in $unction main Explanation: Arra names are pointer constants. So it cannot be modi$ied. @6' void main%' X int i(?L print$%,Kd,,i++ + ++i'L Y Answer: Output Cannot be predicted e!actl. Explanation: Side e$$ects are involved in the evaluation o$ i @?' void main%' X int i(?L print$%,Kd,,i+++++i'L Y Answer:Compiler Srror Explanation: The e!pression i+++++i is parsed as i ++ ++ + i *hich is an illegal combination o$ operators. @#' Minclude=stdio.h< main%' X int i(7,U()L s*itch%i' X case 7+ print$%,1OOD,'L brea5L case U+ print$%,-AD,'L brea5L Y Y Answer:Compiler Srror+ Constant e!pression reRuired in $unction main. Explanation: The case statement can have onl constant e!pressions %this implies that *e cannot use variable names directl so an error'. N"te' Snumerated tpes can be used in case statements. @N' main%' X int iL print$%,Kd,,scan$%,Kd,,Fi''L // value 78 is given as input here Y Answer:7 Explanation: Scan$ returns number o$ items success$ull read and not 7/8. 2ere 78 is given as input *hich should have been scanned success$ull. So number o$ items read is 7. @"' Mde$ine $%g,g)' gMMg) main%' X int var7)(788L print$%,Kd,,$%var,7)''L Y Answer: 788 @9' main%' X int i(8L $or%Li++Lprint$%,Kd,,i'' L print$%,Kd,,i'L Y Answer:7 Explanation: be$ore entering into the $or loop the chec5ing condition is ,evaluated,. 2ere it evaluates to 8 %$alse' and comes out o$ the loop, and i is incremented %note the semicolon a$ter the $or loop'. 68' Minclude=stdio.h< main%' X char s./(XCaC,CbC,CcC,COnC,CcC,CO8CYL char 0p,0str,0str7L p(Fs.@/L str(pL str7(sL print$%,Kd,,++0p + ++0str7;@)'L Y Answer:B Explanation: p is pointing to character COnC.str7 is pointing to character CaC ++0p meAns*er+,p is pointing to COnC and that is incremented b one., the ASCII value o$ COnC is 78. then it is incremented to 77. the value o$ ++0p is 77. ++0str7 meAns*er+,str7 is pointing to CaC that is incremented b 7 and it becomes CbC. ASCII value o$ CbC is 9". both 77 and 9" is added and result is subtracted $rom @). i.e. %77+9"; @)'(NN%,B,'L 67' Minclude=stdio.h< main%' X struct !! X int !(@L char name./(,hello,L YL struct !! 0s(malloc%si&eo$%struct !!''L print$%,Kd,,s;<!'L print$%,Ks,,s;<name'L Y Answer:Compiler Srror Explanation: Initiali&ation should not be done $or structure members inside the structure declaration 6)' Minclude=stdio.h< main%' Xs truct !! X int !L struct X char sL struct !! 0pL YL struct 0RL YL Y Answer:Compiler Srror Explanation: in the end o$ nested structure a member have to be declared. 6@' main%' X e!tern int iL i()8L print$%,Kd,,si&eo$%i''L Y Answer: Ein5er error+ unde$ined smbol CDiC. Explanation: e!tern declaration speci$ies that the variable i is de$ined some*here else. The compiler passes the e!ternal variable to be resolved b the lin5er. So compiler doesnCt $ind an error. During lin5ing the lin5er searches $or the de$inition o$ i. Since it is not $ound the lin5er $lags an error. 66' main%' X print$%,Kd,, out'L Y int out(788L Answer:Compiler error+ unde$ined smbol out in $unction main. Explanation: The rule is that a variable is available $or use $rom the point o$ declaration. Sven though a is a global variable, it is not available $or main. 2ence an error. 6?' main%' X e!tern outL print$%,Kd,, out'L Y int out(788L Answer: 788 Explanation: This is the correct *a o$ *riting the previous program. 6#' main%' X sho*%'L Y void sho*%' X print$%,ICm the greatest,'L Y Answer:Compier error+ Tpe mismatch in redeclaration o$ sho*. Explanation: Ahen the compiler sees the $unction sho* it doesnCt 5no* anthing about it. So the de$ault return tpe %ie, int' is assumed. -ut *hen compiler sees the actual de$inition o$ sho* mismatch occurs since it is declared as void. 2ence the error. The solutions are as $ollo*s+ 7. declare void sho*%' in main%' . ). de$ine sho*%' be$ore main%'. @. declare e!tern void sho*%' be$ore the use o$ sho*%'. 6N' main% ' X int a.)/.@/.)/ ( XXX),6Y,XN,"Y,X@,6YY,XX),)Y,X),@Y,X@,6YYYL print$%VKu Ku Ku Kd OnW,a,0a,00a,000a'L print$%VKu Ku Ku Kd OnW,a+7,0a+7,00a+7,000a+7'L Y Answer: 788, 788, 788, ) 776, 786, 78), @ Explanation: The given arra is a @;D one. It can also be vie*ed as a 7;D arra. ) 6 N " @ 6 ) ) ) @ @ 6 788 78) 786 78# 78" 778 77) 776 77# 77" 7)8 7)) thus, $or the $irst print$ statement a, 0a, 00a give address o$ $irst element . since the indirection 000a gives the value. 2ence, the $irst line o$ the output. $or the second print$ a+7 increases in the third dimension thus points to value at 776, 0a+7 increments in second dimension thus points to 786, 00a +7 increments the $irst dimension thus points to 78) and 000a+7 $irst gets the value at $irst location and then increments it b 7. 2ence, the output. 6"' main% ' X int a. / ( X78,)8,@8,68,?8Y,U,0pL $or%U(8L U=?L U++' X print$%VKdW ,0a'L a++L Y p ( aL $or%U(8L U=?L U++' X print$%VKd W ,0p'L p++L Y Y Answer:Compiler error+ lvalue reRuired. Explanation: Srror is in line *ith statement a++. The operand must be an lvalue and ma be o$ an o$ scalar tpe $or the an operator, arra name onl *hen subscripted is an lvalue. Simpl arra name is a non;modi$iable lvalue. 0069' main% ' X static int a. / ( X8,7,),@,6YL int 0p. / ( Xa,a+7,a+),a+@,a+6YL int 00ptr ( pL ptr++L print$%VOn Kd Kd KdW, ptr;p, 0ptr;a, 00ptr'L 0ptr++L print$%VOn Kd Kd KdW, ptr;p, 0ptr;a, 00ptr'L 0++ptrL print$%VOn Kd Kd KdW, ptr;p, 0ptr;a, 00ptr'L ++0ptrL print$%VOn Kd Kd KdW, ptr;p, 0ptr;a, 00ptr'L Y Answer: 111 222 ### #++ ?8' main% ' X char 0RL int UL $or %U(8L U=@L U++' scan$%VKsW ,%R+U''L $or %U(8L U=@L U++' print$%VKcW ,0%R+U''L $or %U(8L U=@L U++' print$%VKsW ,%R+U''L Y ?7' main% ' X void 0vpL char ch ( IgJ, 0cp ( Vgoo$WL int U ( )8L vp ( FchL print$%VKcW, 0%char 0'vp'L vp ( FUL print$%VKdW,0%int 0'vp'L vp ( cpL print$%VKsW,%char 0'vp + @'L Y Answer: g)8$ ?)' main % ' X static char 0s. / ( XVblac5W, V*hiteW, Vello*W, VvioletWYL char 00ptr. / ( Xs+@, s+), s+7, sY, 000pL p ( ptrL 00++pL print$%VKsW,0;;0++p + @'L Y Answer: c5 ?@' main%' X int i, nL char 0! ( VgirlWL n ( strlen%!'L 0! ( !.n/L $or%i(8L i=nL ++i' X print$%VKsOnW,!'L !++L Y Y Answer: %blan5 space' irl rl l ?6' int i,UL $or%i(8Li=(78Li++' X U+(?L assert%i=?'L Y Answer: Guntime error+ Abnormal program termination. assert $ailed %i=?', =$ile name<,=line number< Explanation: asserts are used during debugging to ma5e sure that certain conditions are satis$ied. I$ assertion $ails, the program *ill terminate reporting the same. A$ter debugging use, Munde$ :DS-31 and this *ill disable all the assertions $rom the source code. Assertion is a good debugging tool to ma5e use o$. ??' main%' X int i(;7L +iL print$%,i ( Kd, +i ( Kd On,,i,+i'L Y Answer: i ( ;7, +i ( ;7 Explanation: 3nar + is the onl dumm operator in C. Ahere;ever it comes ou can Uust ignore it Uust because it has no e$$ect in the e!pressions %hence the name dumm operator'. ?#' Ahat are the $iles *hich are automaticall opened *hen a C $ile is e!ecuted[ Answer: stdin, stdout, stderr %standard input,standard output,standard error'. ?N' *hat *ill be the position o$ the $ile mar5er[ a+ $see5%ptr,8,SSSZDSST'L b+ $see5%ptr,8,SSSZDC3G'L Answer : a+ The SSSZDSST sets the $ile position mar5er to the starting o$ the $ile. b+ The SSSZDC3G sets the $ile position mar5er to the current position o$ the $ile. ?"' main%' X char name.78/,s.7)/L scan$%, O,K.\O,/O,,,s'L Y 2o* scan$ *ill e!ecute[ Answer:4irst it chec5s $or the leading *hite space and discards it.Then it matches *ith a Ruotation mar5 and then it reads all character upto another Ruotation mar5. ?9' Ahat is the problem *ith the $ollo*ing code segment[ *hile %%$gets%receiving arra,?8,$ileDptr'' >( SO4' L Answer & Explanation: $gets returns a pointer. So the correct end o$ $ile chec5 is chec5ing $or >( :3EE. #8' main%' X main%'L Y Answer: Guntime error + Stac5 over$lo*. Explanation: main $unction calls itsel$ again and again. Sach time the $unction is called its return address is stored in the call stac5. Since there is no condition to terminate the $unction call, the call stac5 over$lo*s at runtime. So it terminates the program and results in an error. #7' main%' X char 0 cptr,cL void 0vptr,vL c(78L v(8L cptr(FcL vptr(FvL print$%,KcKv,,c,v'L Y Answer:Compiler error %at line number 6'+ si&e o$ v is 3n5no*n. Explanation: Tou can create a variable o$ tpe void 0 but not o$ tpe void, since void is an empt tpe. In the second line ou are creating variable vptr o$ tpe void 0 and v o$ tpe void hence an error. #)' main%' X char 0 str7(,abcd,L char str)./(,abcd,L print$%,Kd Kd Kd,,si&eo$%str7',si&eo$%str)',si&eo$%,abcd,''L Y Answer: ) ? ? Explanation: In $irst si&eo$, str7 is a character pointer so it gives ou the si&e o$ the pointer variable. In second si&eo$ the name str) indicates the name o$ the arra *hose si&e is ? %including the CO8C termination character'. The third si&eo$ is similar to the second one. #@' main%' X char notL not(>)L print$%,Kd,,not'L Y Answer:8 Explanation: > is a logical operator. In C the value 8 is considered to be the boolean value 4AESS, and an non;&ero value is considered to be the boolean value TG3S. 2ere ) is a non;&ero value so TG3S. >TG3S is 4AESS %8' so it prints 8. #6' Mde$ine 4AESS ;7 Mde$ine TG3S 7 Mde$ine :3EE 8 main%' X i$%:3EE' puts%,:3EE,'L else i$%4AESS' puts%,TG3S,'L else puts%,4AESS,'L Y Answer: TG3S Explanation+ The input program to the compiler a$ter processing b the preprocessor is, main%'X i$%8' puts%,:3EE,'L else i$%;7' puts%,TG3S,'L else puts%,4AESS,'L Y Preprocessor doesnCt replace the values given inside the double Ruotes. The chec5 b i$ condition is boolean value $alse so it goes to else. In second i$ ;7 is boolean value true hence ,TG3S, is printed. #?' main%' X int 5(7L print$%,Kd((7 is ,,Ks,,5,5((7[,TG3S,+,4AESS,'L Y Answer: 7((7 is TG3S Explanation: Ahen t*o strings are placed together %or separated b *hite;space' the are concatenated %this is called as ,stringi&ation, operation'. So the string is as i$ it is given as ,Kd((7 is Ks,. The conditional operator% [+ ' evaluates to ,TG3S,. ##' main%' X int L scan$%,Kd,,F'L // input given is )888 i$% %K6((8 FF K788 >( 8' HH K788 (( 8 ' print$%,Kd is a leap ear,'L else print$%,Kd is not a leap ear,'L Y Answer: )888 is a leap ear Explanation: An ordinar program to chec5 i$ leap ear or not. #N' Mde$ine ma! ? Mde$ine int arr7.ma!/ main%' X tpede$ char arr).ma!/L arr7 list(X8,7,),@,6YL arr) name(,name,L print$%,Kd Ks,,list.8/,name'L Y Answer:Compiler error %in the line arr7 list ( X8,7,),@,6Y' Explanation: arr) is declared o$ tpe arra o$ si&e ? o$ characters. So it can be used to declare the variable name o$ the tpe arr). -ut it is not the case o$ arr7. 2ence an error. Ru!e "# T$u%&' Mde$ines are used $or te!tual replacement *hereas tpede$s are used $or declaring ne* tpes. #"' int i(78L main%' X e!tern int iL X int i()8L X const volatile unsigned i(@8L print$%,Kd,,i'L Y print$%,Kd,,i'L Y print$%,Kd,,i'L Y Answer: @8,)8,78 #9' main%' X int 0UL X int i(78L U(FiL Y print$%,Kd,,0U'L Y Answer: 78 N8' main%' X int i(;7L ;iL print$%,i ( Kd, ;i ( Kd On,,i,;i'L Y Answer: i ( ;7, ;i ( 7 Explanation: ;i is e!ecuted and this e!ecution doesnCt a$$ect the value o$ i. In print$ $irst ou Uust print the value o$ i. A$ter that the value o$ the e!pression ;i ( ;%;7' is printed. N7' Minclude=stdio.h< main%' X const int i(6L $loat UL U ( ++iL print$%,Kd K$,, i,++U'L Y Answer+Compiler error Explanation+ i is a constant. ou cannot change the value o$ constant N)' Minclude=stdio.h< main%' X int a.)/.)/.)/ ( X X78,),@,6Y, X?,#,N,"Y YL int 0p,0RL p(Fa.)/.)/.)/L 0R(000aL print$%,Kd..Kd,,0p,0R'L Y Answer: garbagevalue..7 N@' Minclude=stdio.h< main%' X register i(?L char U./( ,hello,L print$%,Ks Kd,,U,i'L Y Answer: hello ? Explanation+ i$ ou declare i as register compiler *ill treat it as ordinar integer and it *ill ta5e integer value. i value ma be stored either in register or in memor. N6' main%' X int i(?,U(#,&L print$%,Kd,,i+++U'L Y Answer: 77 Explanation: the e!pression i+++U is treated as %i++ + U' N#' struct aaaX struct aaa 0prevL int iL struct aaa 0ne!tL YL main%' X struct aaa abc,de$,ghi,U5lL int !(788L abc.i(8Labc.prev(FU5lL abc.ne!t(Fde$L de$.i(7Lde$.prev(FabcLde$.ne!t(FghiL ghi.i()Lghi.prev(Fde$L ghi.ne!t(FU5lL U5l.i(@LU5l.prev(FghiLU5l.ne!t(FabcL !(abc.ne!t;<ne!t;<prev;<ne!t;<iL print$%,Kd,,!'L Y Answer:) Explanation: above all statements $orm a double circular lin5ed listL abc.ne!t;<ne!t;<prev;<ne!t;<I this one points to ,ghi, node the value o$ at particular node is ). NN' struct point X int !L int L YL struct point origin,0ppL main%' X pp(ForiginL print$%,origin is%KdKd'On,,%0pp'.!,%0pp'.'L print$%,origin is %KdKd'On,,pp;<!,pp;<'L Y Answer: origin is%8,8' origin is%8,8' Explanation+ pp is a pointer to structure. *e can access the elements o$ the structure either *ith arro* mar5 or *ith indirection operator. N"' main%' X int i(DlDabc%78'L print$%,KdOn,,;;i'L Y int D lDabc%int i' X return%i++'L Y Answer:9 Explanation: return%i++' it *ill $irst return i and then increments. i.e. 78 *ill be returned. N9' main%' X char 0pL int 0RL long 0rL p(R(r(8L p++L R++L r++L print$%,Kp...Kp...Kp,,p,R,r'L Y Answer: 8887...888)...8886 Explanation: ++ operator *hen applied to pointers increments address according to their corresponding data;tpes. "8' main%' X char c(C C,!,convert%&'L getc%c'L i$%%c<(CaC' FF %c=(C&C'' !(convert%c'L print$%,Kc,,!'L Y convert%&' X return &;@)L Y Answer: Compiler error Explanation: declaration o$ convert and $ormat o$ getc%' are *rong. "7' main%int argc, char 00argv' X print$%,enter the character,'L getchar%'L sum%argv.7/,argv.)/'L Y sum%num7,num)' int num7,num)L X return num7+num)L Y Answer:Compiler error. Explanation: argv.7/ F argv.)/ are strings. The are passed to the $unction sum *ithout converting it to integer values. ")' M include =stdio.h< int oneDd./(X7,),@YL main%' X int 0ptrL ptr(oneDdL ptr+(@L print$%,Kd,,0ptr'L Y Answer+ garbage value Explanation: ptr pointer is pointing to out o$ the arra range o$ oneDd. "@' M include=stdio.h< aaa%' X print$%,hi,'L Y bbb%'X print$%,hello,'L Y ccc%'X print$%,be,'L Y main%' X int %0ptr.@/'%'L ptr.8/(aaaL ptr.7/(bbbL ptr.)/(cccL ptr.)/%'L Y Answer+ be Explanation: ptr is arra o$ pointers to $unctions o$ return tpe int.ptr.8/ is assigned to address o$ the $unction aaa. Similarl ptr.7/ and ptr.)/ $or bbb and ccc respectivel. ptr.)/%' is in e$$ect o$ *riting ccc%', since ptr.)/ points to ccc. "?' Minclude=stdio.h< main%' X 4IES 0ptrL char iL ptr($open%,&&&.c,,,r,'L *hile%%i($getch%ptr''>(SO4' print$%,Kc,,i'L Y Answer: contents o$ &&&.c $ollo*ed b an in$inite loop Explanation: The condition is chec5ed against SO4, it should be chec5ed against :3EE. "#' main%' X int i (8LU(8L i$%i FF U++' print$%,Kd..Kd,,i++,U'L print$%,Kd..Kd,i,U'L Y Answer: 8..8 Explanation: The value o$ i is 8. Since this in$ormation is enough to determine the truth value o$ the boolean e!pression. So the statement $ollo*ing the i$ statement is not e!ecuted. The values o$ i and U remain unchanged and get printed. "N' main%' X int iL i ( abc%'L print$%,Kd,,i'L Y abc%' X DA] ( 7888L Y Answer: 7888 Explanation: :ormall the return value $rom the $unction is through the in$ormation $rom the accumulator. 2ere DA2 is the pseudo global variable denoting the accumulator. 2ence, the value o$ the accumulator is set 7888 so the $unction returns value 7888. ""' int iL main%'X int tL $or % t(6Lscan$%,Kd,,Fi';tLprint$%,KdOn,,i'' print$%,Kd;;,,t;;'L Y // I$ the inputs are 8,7,),@ $ind the o/p Answer: 6;;8 @;;7 );;) Explanation: Eet us assume some !( scan$%,Kd,,Fi';t the values during e!ecution *ill be, t i ! 6 8 ;6 @ 7 ;) ) ) 8 "9' main%'X int a( 8Lint b ( )8Lchar ! (7Lchar (78L i$%a,b,!,' print$%,hello,'L Y Answer: 2ello 98' main%'X unsigned int iL $or%i(7Li<;)Li;;' print$%,c aptitude,'L Y 97' In the $ollo*ing pgm add a stmt in the $unction $un such that the address o$ CaC gets stored in CUC. main%'X int 0 UL void $un%int 00'L $un%FU'L Y void $un%int 005' X int a (8L /0 add a stmt here0/ Y Answer: 05 ( Fa Explanation: The argument o$ the $unction is a pointer to a pointer. 9)' Ahat are the $ollo*ing notations o$ de$ining $unctions 5no*n as[ i. int abc%int a,$loat b' X /0 some code 0/ Y ii. int abc%a,b' int aL $loat bL X /0 some code0/ Y Answer: i. A:SI C notation ii. Zernighan F Gitche notation 9@' main%' X char 0 pL p(,KdOn,L p++L p++L print$%p;),@88'L Y Answer: @88 Explanation: The pointer points to K since it is incremented t*ice and again decremented b ), it points to CKdOnC and @88 is printed. 96' main%'X char a.788/L a.8/(CaCLa.7//(CbCLa.)/(CcCLa.6/(CdCL abc%a'L Y abc%char a./'X a++L print$%,Kc,,0a'L a++L print$%,Kc,,0a'L Y Explanation: The base address is modi$ied onl in $unction and as a result a points to CbC then a$ter incrementing to CcC so bc *ill be printed. 9?' $unc%a,b' int a,bL X return% a( %a((b' 'L Y main%' X int process%',$unc%'L print$%,The value o$ process is Kd >On ,,process%$unc,@,#''L Y process%p$,val7,val)' int %0p$' %'L int val7,val)L X return%%0p$' %val7,val)''L Y Answer: The value i$ process is 8 > 9#' void main%' X static int i(?L i$%;;i'X main%'L print$%,Kd ,,i'L Y Y Answer: 8 8 8 8 Explanation: The variable ,I, is declared as static, hence memor $or I *ill be allocated $or onl once, as it encounters the statement. The $unction main%' *ill be called ecursivel unless I becomes eRual to 8, and since main%' is recursivel called, so the value o$ static I ie., 8 *ill be printed ever time the control is returned. 9N' void main%' X int 5(ret%si&eo$%$loat''L print$%,On here value is Kd,,++5'L Y int ret%int ret' X ret +( ).?L return%ret'L Y Answer: 2ere value is N Explanation: The int ret%int ret', ie., the $unction name and the argument name can be the same. 4irstl, the $unction ret%' is called in *hich the si&eo$%$loat' ie., 6 is passed, a$ter the $irst e!pression the value in ret *ill be #, as ret is integer hence the value stored in ret *ill have implicit tpe conversion $rom $loat to int. The ret is returned in main%' it is printed a$ter and preincrement. 9"' void main%' X char a./(,7)@6?O8,L int i(strlen%a'L print$%,here in @ KdOn,,++i'L Y Answer: here in @ # 99' void main%' X unsigned giveit(;7L int gotitL print$%,Ku ,,++giveit'L print$%,Ku On,,gotit(;;giveit'L Y Answer: 8 #??@? Explanation: 788' void main%' X int iL char a./(,O8,L i$%print$%,KsOn,,a'' print$%,O5 here On,'L else print$%,4orget itOn,'L Y Answer: O5 here Explanation: Print$ *ill return ho* man characters does it print. 2ence printing a null character returns 7 *hich ma5es the i$ statement true, thus ,O5 here, is printed. 787' void main%' X void 0vL int integer()L int 0i(FintegerL v(iL print$%,Kd,,%int0'0v'L Y Answer: Compiler Srror. Ae cannot appl indirection on tpe void0. Explanation: Qoid pointer is a generic pointer tpe. :o pointer arithmetic can be done on it. Qoid pointers are normall used $or, 7. Passing generic pointers to $unctions and returning such pointers. ). As a intermediate pointer tpe. @. 3sed *hen the e!act pointer tpe *ill be 5no*n at a later point o$ time. 78)' void main%' X int i(i++,U(U++,5(5++L print$%VKdKdKdW,i,U,5'L Y Answer: 1arbage values. Explanation: Gn id%ntifi%r is avai$a'$% to &s% in pro=ram cod% from th% point of its d%c$aration. So e!pressions such as i ( i++ are valid statements. The i, U and 5 are automatic variables and so the contain some garbage value. Har'a=% in is =ar'a=% o&t (H)HI). 78@' void main%' X static int i(i++, U(U++, 5(5++L print$%Vi ( Kd U ( Kd 5 ( KdW, i, U, 5'L Y Answer: i ( 7 U ( 7 5 ( 7 Explanation: Since static variables are initiali&ed to &ero b de$ault. 786' void main%' X *hile%7'X i$%print$%,Kd,,print$%,Kd,''' brea5L else continueL Y Y Answer: 1arbage values 786' main%' X unsigned int i(78L *hile%i;;<(8' print$%,Ku ,,i'L Y Answer: 78 9 " N # ? 6 @ ) 7 8 #??@? #??@6^.. Explanation: Since i is an unsigned integer it can never become negative. So the e!pression i;; <(8 *ill al*as be true, leading to an in$inite loop. 78?' Minclude=conio.h< main%' X int !,(),&,aL i$%!(K)' &()L a()L print$%,Kd Kd ,,&,!'L Y Answer: 1arbage;value 8 Explanation: The value o$ K) is 8. This value is assigned to !. The condition reduces to i$ %!' or in other *ords i$%8' and so & goes uninitiali&ed. Thumb Rule: Chec5 all control paths to *rite bug $ree code. 78#' main%' X int a.78/L print$%,Kd,,0a+7;0a+@'L Y Answer: 6 Explanation: 0a and ;0a cancels out. The result is as simple as 7 + @ ( 6 > 78N' Mde$ine prod%a,b' a0b main%' X int !(@,(6L print$%,Kd,,prod%!+),;7''L Y Answer: 78 Explanation: The macro e!pands and evaluates to as+ !+)0;7 (< !+%)0';7 (< 78 78"' main%' X unsigned int i(#?888L *hile%i++>(8'L print$%,Kd,,i'L Y Answer: 7 Explanation: :ote the semicolon a$ter the *hile statement. Ahen the value o$ i becomes 8 it comes out o$ *hile loop. Due to post;increment on i the value o$ i *hile printing is 7. 789' main%' X int i(8L *hile%+%+i;;'>(8' i;(i++L print$%,Kd,,i'L Y Answer: ;7 Explanation: *nar( + is th% on$( d&mm( op%rator in @. So it has no e$$ect on the e!pression and no* the *hile loop is, *hile%i;;>(8' *hich is $alse and so brea5s out o$ *hile loop. The value P7 is printed due to the post;decrement operator. 77@' main%' X $loat $(?,g(78L enumXi(78,U()8,5(?8YL print$%,KdOn,,++5'L print$%,K$On,,$==)'L print$%,Kl$On,,$Kg'L print$%,Kl$On,,$mod%$,g''L Y Answer: Eine no ?+ Srror+ Evalue reRuired Eine no #+ Cannot appl le$tshi$t to $loat Eine no N+ Cannot appl mod to $loat . 778' main%' X int i(78L void pascal $%int,int,int'L $%i++,i++,i++'L print$%, Kd,,i'L Y void pascal $%integer +i,integer+U,integer +5' X *rite%i,U,5'L Y Answer:Compiler error+ un5no*n tpe integer Compiler error+ undeclared $unction *rite Explanation: Pascal 5e*ord doesnJt mean that pascal code can be used. It means that the $unction $ollo*s Pascal argument passing mechanism in calling the $unctions. 777' void pascal $%int i,int U,int 5' X print$%VKd Kd KdW,i, U, 5'L Y void cdecl $%int i,int U,int 5' X print$%VKd Kd KdW,i, U, 5'L Y main%' X int i(78L $%i++,i++,i++'L print$%, KdOn,,i'L i(78L $%i++,i++,i++'L print$%, Kd,,i'L Y Answer: 78 77 7) 7@ 7) 77 78 7@ Explanation: Pascal argument passing mechanism $orces the arguments to be called $rom le$t to right. cdecl is the normal C argument passing mechanism *here the rguments are passed $rom right to le$t. 77)'. Ahat is the output o$ the program given belo* main%' X signed char i(8L $or%Li<(8Li++' L print$%,KdOn,,i'L Y Answer ;7)" Explanation :otice the semicolon at the end o$ the $or loop. T2e initial value o$ the i is set to 8. The inner loop e!ecutes to increment the value $rom 8 to 7)N %the positive range o$ char' and then it rotates to the negative value o$ ;7)". The condition in the $or loop $ails and so comes out o$ the $or loop. It prints the current value o$ i that is ;7)". 77@' main%' X unsigned char i(8L $or%Li<(8Li++' L print$%,KdOn,,i'L Y Answer in$inite loop Explanation The di$$erence bet*een the previous Ruestion and this one is that the char is declared to be unsigned. So the i++ can never ield negative value and i<(8 never becomes $alse so that it can come out o$ the $or loop. 776' main%' X char i(8L $or%Li<(8Li++' L print$%,KdOn,,i'L Y Answer:-ehavior is implementation dependent. Explanation: The detail i$ the char is signed/unsigned b de$ault is implementation dependent. I$ the implementation treats the char to be signed b de$ault the program *ill print P7)" and terminate. On the other hand i$ it considers char to be unsigned b de$ault, it goes to in$inite loop. Ru!e' Tou can *rite programs that have implementation dependent behavior. -ut dont *rite programs that depend on such behavior. 77?' Is the $ollo*ing statement a declaration/de$inition. 4ind *hat does it mean[ int %0!'.78/L Answer De$inition. ! is a pointer to arra o$%si&e 78' integers. Appl cloc5;*ise rule to $ind the meaning o$ this de$inition. 77#'. Ahat is the output $or the program given belo* tpede$ enum errorTpeX*arning, error, e!ception,YerrorL main%' X error g7L g7(7L print$%,Kd,,g7'L Y Answer Compiler error+ Bultiple declaration $or error 77N' tpede$ struct errorXint *arning, error, e!ceptionLYerrorL main%' X error g7L g7.error (7L print$%,Kd,,g7.error'L Y Answer 7 77"' Mi$de$ something int some(8L Mendi$ main%' X int thing ( 8L print$%,Kd KdOn,, some ,thing'L Y Answer:Compiler error + unde$ined smbol some Explanation: This is a ver simple e!ample $or conditional compilation. The name something is not alread 5no*n to the compiler ma5ing the declaration int some ( 8L e$$ectivel removed $rom the source code. 779' Mi$ something (( 8 int some(8L Mendi$ main%' X int thing ( 8L print$%,Kd KdOn,, some ,thing'L Y Answer 8 8 Explanation This code is to sho* that preprocessor e!pressions are not the same as the ordinar e!pressions. I$ a name is not 5no*n the preprocessor treats it to be eRual to &ero. 7)8'. Ahat is the output $or the $ollo*ing program main%' X int arr)D.@/.@/L print$%,KdOn,, %%arr)D((0 arr)D'FF%0 arr)D (( arr)D.8/'' 'L Y Answer 7 7)7' void main%' X i$%_8 (( %unsigned int';7' print$%VTou can ans*er this i$ ou 5no* ho* values are represented in memorW'L Y Ans*er Tou can ans*er this i$ ou 5no* ho* values are represented in memor 7))' int s*ap%int 0a,int 0b' X 0a(0a+0bL0b(0a;0bL0a(0a;0bL Y main%' X int !(78,()8L s*ap%F!,F'L print$%,!( Kd ( KdOn,,!,'L Y Ans*er ! ( )8 ( 78 S!planation This is one *a o$ s*apping t*o values. Simple chec5ing *ill help understand this. 7)@' main%' X char 0 p ( V aRmWL print$%VKcW,++0%p++''L Y Ans*er+ - 7)6' main%' X int i(?L print$%,Kd,,++i++'L Y Answer:Compiler error+ Evalue reRuired in $unction main Explanation: ++i ields an rvalue. 4or post$i! ++ to operate an lvalue is reRuired. 7)?' main%' X char 0p ( VaRmWL char cL c ( ++0p++L print$%VKcW,c'L Y Answer:b Explanation: There is no di$$erence bet*een the e!pression ++0%p++' and ++0p++. Parenthesis Uust *or5s as a visual clue $or the reader to see *hich e!pression is $irst evaluated. 7)#' int aaa%' Xprint$%V2iW'LY int bbb%'Xprint$%VhelloW'LY in ccc%'Xprint$%VbeW'LY main%' X int % 0 ptr.@/' %'L ptr.8/ ( aaaL ptr.7/ ( bbbL ptr.)/ (cccL ptr.)/%'L Y Ans*er+ &1e S!planation+ int %0 ptr.@/'%' sas that ptr is an arra o$ pointers to $unctions that ta5es no arguments and returns the tpe int. - the assignment ptr.8/ ( aaaL it means that the $irst $unction pointer in the arra is initiali&ed *ith the address o$ the $unction aaa. Similarl, the other t*o arra elements also get initiali&ed *ith the ddresses o$ the $unctions bbb and ccc. Since ptr.)/ contains the address o$ the $unction ccc, the call to the $unction ptr.)/%' is same as calling ccc%'. So it results in printing ,be,. 7)N' main%' X int i(?L print$%VKdW,i(++i ((#'L Y Answer:+ 7)"' main%' X char p. /(,KdOn,L p.7/ ( CcCL print$%p,#?'L Y Answer:A Explanation: Due to the assignment p.7/ ( IcJ the string becomes, VKcOnW. Since this string becomes the $ormat string $or print$ and ASCII value o$ #? is IAJ, the same gets printed. 7)9' void % 0 abc% int, void % 0de$' %' ' ' %'L Ans*er++ abc is a ptr to a $unction *hich ta5es ) parameters .%a'. an integer variable.%b'. a ptrto a $untion *hich returns void. the return tpe o$ the $unction is void. Explanation: Appl the cloc5;*ise rule to $ind the result. 7@8' main%' X *hile %strcmp%VsomeW,WsomeO8W'' print$%VStrings are not eRualOnW'L Y Answer: :o output Explanation: Snding the string constant *ith O8 e!plicitl ma5es no di$$erence. So VsomeW and VsomeO8W are eRuivalent. So, strcmp returns 8 %$alse' hence brea5ing out o$ the *hile loop. 7@7' main%' X char str7./ ( XIsJ,JoJ,JmJ,JeJYL char str)./ ( XIsJ,JoJ,JmJ,JeJ,JO8JYL *hile %strcmp%str7,str)'' print$%VStrings are not eRualOnW'L Y Answer: VStrings are not eRualW VStrings are not eRualW ^. 7@)' main%' X int i ( @L $or %Li++(8L' print$%VKdW,i'L Y Answer: Compiler Srror+ Evalue reRuired. Explanation: As *e 5no* that increment operators return rvalues and hence it cannot appear on the le$t hand side o$ an assignment operation. 7@@' void main%' X int 0mptr, 0cptrL mptr ( %int0'malloc%si&eo$%int''L print$%VKdW,0mptr'L int 0cptr ( %int0'calloc%si&eo$%int',7'L print$%VKdW,0cptr'L Y Answer: garbage;value 8 Explanation: The memor space allocated b malloc is uninitiali&ed, *hereas calloc returns the allocated memor space initiali&ed to &eros. 7@6' void main%' X static int iL *hile%i=(78' %i<)'[i+++i;;L print$%VKdW, i'L Y Answer: @)N#N Explanation: Since i is static it is initiali&ed to 8. Inside the *hile loop the conditional operator evaluates to $alse, e!ecuting i;;. This continues till the integer value rotates to positive value %@)N#N'. The *hile condition becomes $alse and hence, comes out o$ the *hile loop, printing the I value. 7@?' main%' X int i(78,U()8L U ( i, U[%i,U'[i+U+UL print$%,Kd Kd,,i,U'L Y Answer: 78 78 Explanation: The Ternar operator % [ + ' is eRuivalent $or i$;then;else statement. So the Ruestion can be *ritten as+ i$%i,U' X i$%i,U' U ( iL else U ( UL Y else U ( UL 7@#' 7. const char 0aL ). char0 const aL @. char const 0aL ;Di$$erentiate the above declarations. Answer: 7. CconstC applies to char 0 rather than CaC % pointer to a constant char ' 0a(C4C + illegal a(,2i, + legal ). CconstC applies to CaC rather than to the value o$ a %constant pointer to char ' 0a(C4C + legal a(,2i, + illegal @. Same as 7. 7@N' main%' X int i(?,U(78L i(iF(UFF78L print$%,Kd Kd,,i,U'L Y Answer: 7 78 Explanation: The e!pression can be *ritten as i(%iF(%UFF78''L The inner e!pression %UFF78' evaluates to 7 because U((78. i is ?. i ( ?F7 is 7. 2ence the result. 7@"' main%' X int i(6,U(NL U ( U HH i++ FF print$%,TO3 CA:,'L print$%,Kd Kd,, i, U'L Y Answer: 6 7 7@9' main%' X register int a()L print$%,Address o$ a ( Kd,,Fa'L print$%,Qalue o$ a ( Kd,,a'L Y Answer:Compier Srror+ CFC on register variable Rule to Remember: & (address of ) operator cannot be applied on register variables 768' main%' X $loat i(7.?L s*itch%i' X case 7+ print$%,7,'L case )+ print$%,),'L de$ault + print$%,8,'L Y Y Answer:Compiler Srror+ s*itch e!pression not integral Explanation: J;itch stat%m%nts can '% app$i%d on$( to int%=ra$ t(p%s. 767' main%' X e!tern iL print$%,KdOn,,i'L X int i()8L print$%,KdOn,,i'L Y Y Answer: Ein5er Srror + 3nresolved e!ternal smbol i Explanation: The identi$ier i is available in the inner bloc5 and so using e!tern has no use in resolving it. 76)' main%' X int a(),0$7,0$)L $7($)(FaL 0$)+(0$)+(a+().?L print$%,OnKd Kd Kd,,a,0$7,0$)'L Y Answer: 7# 7# 7# Explanation:
$7 and $) both re$er to the same memor location a. So changes through $7 and $) ultimatel a$$ects onl the value o$ a. 76@' main%' X char 0p(,1OOD,L char a. /(,1OOD,L print$%,On si&eo$%p' ( Kd, si&eo$%0p' ( Kd, strlen%p' ( Kd,, si&eo$%p', si&eo$%0p', strlen%p''L print$%,On si&eo$%a' ( Kd, strlen%a' ( Kd,, si&eo$%a', strlen%a''L Y Answer: si&eo$%p' ( ), si&eo$%0p' ( 7, strlen%p' ( 6 si&eo$%a' ( ?, strlen%a' ( 6 Explanation: si&eo$%p' (< si&eo$%char0' (< ) si&eo$%0p' (< si&eo$%char' (< 7 Similarl, si&eo$%a' (< si&e o$ the character arra (< ? ?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&eo$ the pointer variable. 2ere the si&eo$%a' *here a is the character arra and the si&e o$ the arra is ? because the space necessar $or the terminating :3EE character should also be ta5en into account. 766' Mde$ine DIB% arra, tpe' si&eo$%arra'/si&eo$%tpe' main%' X int arr.78/L print$%VThe dimension o$ the arra is KdW, DIB%arr, int''L Y Answer: 78 Explanation: The si&e o$ integer arra o$ 78 elements is 78 0 si&eo$%int'. The macro e!pands to si&eo$%arr'/si&eo$%int' (< 78 0 si&eo$%int' / si&eo$%int' (< 78. 76?' int DIB%int arra./' X return si&eo$%arra'/si&eo$%int 'L Y main%' X int arr.78/L print$%VThe dimension o$ the arra is KdW, DIB%arr''L Y Answer: 7 Explanation: Grra(s cannot '% pass%d to f&nctions as ar=&m%nts and on$( th% point%rs can '% pass%d. So the argument is eRuivalent to int 0 arra %this is one o$ the ver $e* places *here ./ and 0 usage are eRuivalent'. The return statement becomes, si&eo$%int 0'/ si&eo$%int' that happens to be eRual in this case.
76#' main%' X static int a.@/.@/(X7,),@,6,?,#,N,",9YL int i,UL static 0p./(Xa,a+7,a+)YL $or%i(8Li=@Li++' X $or%U(8LU=@LU++' print$%,KdOtKdOtKdOtKdOn,,0%0%p+i'+U', 0%0%U+p'+i',0%0%i+p'+U',0%0%p+U'+i''L Y Y Answer: 7 7 7 7 ) 6 ) 6 @ N @ N 6 ) 6 ) ? ? ? ? # " # " N @ N @ " # " # 9 9 9 9 Explanation: 0%0%p+i'+U' is eRuivalent to p.i/.U/. 76N' main%' X void s*ap%'L int !(78,("L s*ap%F!,F'L print$%,!(Kd (Kd,,!,'L Y void s*ap%int 0a, int 0b' X 0a \( 0b, 0b \( 0a, 0a \( 0bL Y Answer: !(78 (" 76"' main%' X int i ( )?NL int 0iPtr ( FiL print$%,Kd Kd,, 0%%char0'iPtr', 0%%char0'iPtr+7' 'L Y Answer: 7 7 Explanation: The integer value )?N is stored in the memor as, 88888887 88888887, so the individual btes are ta5en b casting it to char 0 and get printed. 769' main%' X int i ( )?"L int 0iPtr ( FiL print$%,Kd Kd,, 0%%char0'iPtr', 0%%char0'iPtr+7' 'L Y Answer: ) 7 Explanation: The integer value )?N can be represented in binar as, 88888887 88888887. Gemember that the I:TSE machines are Ismall;endianJ 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. The integer value )?" is stored in memor as+ 88888887 88888878. 7?8' main%' X int i(@88L char 0ptr ( FiL 0++ptr()L print$%,Kd,,i'L Y Answer: ??# Explanation: The integer value @88 in binar notation is+ 88888887 88787788. It is stored in memor %small;endian' as+ 88787788 88888887. Gesult o$ the e!pression 0++ptr ( ) ma5es the memor representation as+ 88787788 88888878. So the integer corresponding to it is 88888878 88787788 (< ??#. 7?7' Minclude =stdio.h< main%' X char 0 str ( ,hello,L char 0 ptr ( strL char least ( 7)NL *hile %0ptr++' least ( %0ptr=least ' [0ptr +leastL print$%,Kd,,least'L Y Answer:8 Explanation: A$ter IptrJ reaches the end o$ the string the value pointed b IstrJ is IO8J. So the value o$ IstrJ is less than that o$ IleastJ. So the value o$ IleastJ $inall is 8. 7?)' Declare an arra o$ : pointers to $unctions returning pointers to $unctions returning pointers to characters[ Answer: %char0%0'% '' %0ptr.:/'% 'L 7?@' main%' X struct student X char name.@8/L struct date dobL YstudL struct date X int da,month,earL YL scan$%,KsKdKdKd,, stud.rollno, Fstudent.dob.da, Fstudent.dob.month, Fstudent.dob.ear'L Y Answer:Compiler Srror+ 3nde$ined structure date Explanation: Inside the struct de$inition o$ IstudentJ the member o$ tpe struct date is given. The compiler doesnJt have the de$inition o$ date structure %$or*ard re$erence is not allo*ed in C in this case' so it issues an error. 7?6' main%' X struct dateL struct student X char name.@8/L struct date dobL YstudL struct date X int da,month,earL YL scan$%,KsKdKdKd,, stud.rollno, Fstudent.dob.da, Fstudent.dob.month, Fstudent.dob.ear'L Y Answer:Compiler Srror+ 3nde$ined structure date Explanation:
Onl declaration o$ struct date is available inside the structure de$inition o$ IstudentJ but to have a variable o$ tpe struct date the de$inition o$ the structure is reRuired. 7??' There *ere 78 records stored in Vsome$ile.datW but the $ollo*ing program printed 77 names. Ahat *ent *rong[ void main%' X struct student X char name.@8/, rollno.#/L YstudL 4IES 0$p ( $open%Vsome$ile.datW,WrW'L *hile%>$eo$%$p'' X $read%Fstud, si&eo$%stud', 7 , $p'L puts%stud.name'L Y Y Explanation: $read reads 78 records and prints the names success$ull. It *ill return SO4 onl *hen $read tries to read another record and $ails reading SO4 %and returning SO4'. So it prints the last record again. A$ter this onl the condition $eo$%$p' becomes $alse, hence comes out o$ the *hile loop. 7?#' Is there an di$$erence bet*een the t*o declarations, 7. int $oo%int 0arr./' and ). int $oo%int 0arr.)/' Answer: :o Explanation: 4unctions can onl pass pointers and not arras. The numbers that are allo*ed inside the ./ is Uust $or more readabilit. So there is no di$$erence bet*een the t*o declarations. 7?N' Ahat is the subtle error in the $ollo*ing code segment[ void $un%int n, int arr./' X int 0p(8L int i(8L *hile%i++=n' p ( Farr.i/L 0p ( 8L Y Answer & Explanation: I$ the bod o$ the loop never e!ecutes p is assigned no address. So p remains :3EE *here 0p (8 ma result in problem %ma rise to runtime error V:3EE pointer assignmentW and terminate the program'. 7?"' Ahat is *rong *ith the $ollo*ing code[ int 0$oo%' X int 0s ( malloc%si&eo$%int'788'L
assert%s >( :3EE'L return sL Y Answer & Explanation: assert macro should be used $or debugging and $inding out bugs. The chec5 s >( :3EE is $or error/e!ception handling and $or that assert shouldnJt be used. A plain i$ and the corresponding remed statement has to be given. 7?9' Ahat is the hidden bug *ith the $ollo*ing statement[ assert%val++ >( 8'L Answer & Explanation: Assert macro is used $or debugging and removed in release version. In assert, the e!pression involves side;e$$ects. So the behavior o$ the code becomes di$$erent in case o$ debug version and the release version thus leading to a subtle bug. Rule to Remember: KonLt &s% %3pr%ssions that hav% sid%,%ff%cts in ass%rt stat%m%nts. 7#8' void main%' X int 0 i ( 8!688L // i points to the address 688 0i ( 8L // set the value o$ memor location pointed b iL Y Answer: 3nde$ined behavior Explanation: The second statement results in unde$ined behavior because it points to some location *hose value ma not be available $or modi$ication. Mhis t(p% of point%r in ;hich th% nonavai$a'i$it( of th% imp$%m%ntation of th% r%f%r%nc%d $ocation is 5no;n as Eincomp$%t% t(p%E. 7#7' Mde$ine assert%cond' i$%>%cond'' O %$print$%stderr, ,assertion $ailed+ Ks, $ile Ks, line Kd On,,Mcond,O DD4IESDD,DDEI:SDD', abort%'' void main%' X int i ( 78L i$%i((8' assert%i = 788'L else print$%,This statement becomes else $or i$ in assert macro,'L Y Ans*er+ :o output Explanation: The else part in *hich the print$ is there becomes the else $or i$ in the assert macro. 2ence nothing is printed. The solution is to use conditional operator instead o$ i$ statement, Mde$ine assert%cond' %%cond'[%8'+ %$print$ %stderr, ,assertion $ailed+ O Ks, $ile Ks, line Kd On,,Mcond, DD4IESDD,DDEI:SDD', abort%''' :ote+
2o*ever this problem o$ Vmatching *ith nearest elseW cannot be solved b the usual method o$ placing the i$ statement inside a bloc5 li5e this, Mde$ine assert%cond' X O i$%>%cond'' O %$print$%stderr, ,assertion $ailed+ Ks, $ile Ks, line Kd On,,Mcond,O DD4IESDD,DDEI:SDD', abort%'' O Y 7#)' Is the $ollo*ing code legal[ struct a X int !L struct a bL Y Ans7er' :o Explanation: Is it not legal $or a structure to contain a member that is o$ the same tpe as in this case. -ecause this *ill cause the structure declaration to be recursive *ithout end. 7#@' Is the $ollo*ing code legal[ struct a X int !L struct a 0bL Y Answer:Tes. Explanation: 0b is a pointer to tpe struct a and so is legal. The compiler 5no*s, the si&e o$ the pointer to a structure even be$ore the si&e o$ the structure is determined%as ou 5no* the pointer to an tpe is o$ same si&e'. This tpe o$ structures is 5no*n as Isel$;re$erencingJ structure. 7#6' Is the $ollo*ing code legal[ tpede$ struct a X int !L aTpe 0bL YaTpe Answer: :o Explanation: The tpename aTpe is not 5no*n at the point o$ declaring the structure %$or*ard re$erences are not made $or tpede$s'. 7#?' Is the $ollo*ing code legal[ tpede$ struct a aTpeL struct a X int !L
aTpe 0bL YL Answer:Tes Explanation: The tpename aTpe is 5no*n at the point o$ declaring the structure, because it is alread tpede$ined. 7##' Is the $ollo*ing code legal[ void main%' X tpede$ struct a aTpeL aTpe someQariableL struct a X int !L aTpe 0bL YL Y Answer: :o Explanation: Ahen the declaration, tpede$ struct a aTpeL is encountered bod o$ struct a is not 5no*n. This is 5no*n as Iincomplete tpesJ. 7#N' void main%' X print$%Vsi&eo$ %void 0' ( Kd OnV, si&eo$% void 0''L print$%Vsi&eo$ %int 0' ( Kd OnW, si&eo$%int 0''L print$%Vsi&eo$ %double 0' ( Kd OnW, si&eo$%double 0''L print$%Vsi&eo$%struct un5no*n 0' ( Kd OnW, si&eo$%struct un5no*n 0''L Y Answer : si&eo$ %void 0' ( ) si&eo$ %int 0' ( ) si&eo$ %double 0' ( ) si&eo$%struct un5no*n 0' ( ) Explanation: The pointer to an tpe is o$ same si&e. 7#"' char inputString.788/ ( X8YL To get string input $rom the 5eboard *hich one o$ the $ollo*ing is better[ 7' gets%inputString' )' $gets%inputString, si&eo$%inputString', $p' Answer & Explanation: The second one is better because gets%inputString' doesnCt 5no* the si&e o$ the string passed and so, i$ a ver big input %here, more than 788 chars' the charactes *ill be *ritten past the input string. Ahen $gets is used *ith stdin per$orms the same operation as gets but is sa$e. 7#9' Ahich version do ou pre$er o$ the $ollo*ing t*o, 7' print$%VKsW,str'L // or the more curt one
)' print$%str'L Answer & Explanation: Pre$er the $irst one. I$ the str contains an $ormat characters li5e Kd then it *ill result in a subtle bug. 7N8' void main%' X int i(78, U()L int 0ip( Fi, 0Up ( FUL int 5 ( 0ip/0UpL print$%VKdW,5'L Y Answer: Compiler Srror+ V3ne!pected end o$ $ile in comment started in line ?W. Explanation: The programmer intended to divide t*o integers, but b the Vma!imum munchW rule, the compiler treats the operator seRuence / and 0 as /0 *hich happens to be the starting o$ comment. To $orce *hat is intended b the programmer, int 5 ( 0ip/ 0UpL // give space e!plicit separating / and 0 //or int 5 ( 0ip/%0Up'L // put braces to $orce the intention *ill solve the problem. 7N7' void main%' X char chL $or%ch(8Lch=(7)NLch++' print$%VKc Kd OnV, ch, ch'L Y Answer: Implementaion dependent Explanation: The char tpe ma be signed or unsigned b de$ault. I$ it is signed then ch++ is e!ecuted a$ter ch reaches 7)N and rotates bac5 to ;7)". Thus ch is al*as smaller than 7)N. 7N)' Is this code legal[ int 0ptrL ptr ( %int 0' 8!688L Answer: Tes Explanation: The pointer ptr *ill point at the integer in the memor location 8!688. 7N@' main%' X char a.6/(,2SEEO,L print$%,Ks,,a'L Y Answer: Compiler error+ Too man initiali&ers
Explanation: The arra a is o$ si&e 6 but the string constant reRuires # btes to get stored. 7N6' main%' X char a.6/(,2SEE,L print$%,Ks,,a'L Y Answer: 2SEEK`>_`>`[[[`__> 7N?' main%' X int a(78,0UL void 05L U(5(FaL U++L 5++L print$%,On Ku Ku ,,U,5'L Y Answer: Compiler error+ Cannot increment a void pointer 7N#' char 0some4un7%' X char temp. / ( V string,L return tempL Y char 0 some4un)%' X char temp. / ( XIsJ, I tJ,JrJ,JiJ,JnJ,JgJYL return tempL Y int main%' X puts%some4un7%''L puts%some4un)%''L Y Ans*er+ 1arbage values.