Вы находитесь на странице: 1из 32

Predict the output or error(s) for the following:

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=0;s[ i ];i++) printf(" n%c%c%c%c",s[ i ],*(s+i),*(i+s),i[s]);

} Answer:

Explanation: s i!" #$i%s&" #$s%i&" i s! are all different ways of expressing the same idea. 'enerally array name is the (ase address for that array. )ere s is the (ase address. i is the index 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()

mmmm aaaa nnnn

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 exactly. /epending on the num(er of (ytes" the precession with of the value represented varies. .loat ta0es 1 (ytes and long dou(le ta0es 23 (ytes. +o float stores 3.4 with less precision than long dou(le. 5ule of 6hum(:

7ever compare or at8least (e cautious when using floating point num(ers with relational operators $99 " :" ;" ;9" :9"<9 & . ). main() * static int var = 5; printf("%d ",var++); if(var) main(); ,

Answer: =1>?2 Explanation: @hen static storage class is given" it is initialiAed once. 6he change in the value of a static varia(le is retained even (etween the function calls. Bain is also treated li0e any other ordinary function" which can (e called recursively. 5. { main()

int c[ ]=*2.-,!.),),../,5,; int 0,*p=c,*1=c; for(0=0;025;0++) * printf(" %d ",*c); ++1; , for(0=0;025;0++)* printf(" %d ",*p); ++p; , } Answer: ??????>1C= Explanation: ,nitially pointer c is assigned to (oth p and D. ,n the first loop" since only D is incremented and not c " the value ? will (e printed = times. ,n second loop p itself is incremented. +o the values ? > 1 C = will (e printed. .. { main()

#3t#rn int i; i=20; printf("%d",i); } Answer: Ein0er Error : -ndefined sym(ol FGiF Explanation: extern storage class in the following declaration" extern int iH 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 lin0ing. But lin0er finds that no other varia(le of name i is availa(le in any other program with memory space allocated for it. )ence a lin0er error has occurred .

/. {

main() int i=+1,0=+1,4=0,"=2,m; m=i++550++554++66"++; printf("%d %d %d %d %d",i,0,4,",m);

} Answer: 332>2

Explanation : Eogical operations always give a result of 2 or 3 . And also the logical A7/ $II& operator has higher priority over the logical J5 $KK& operator. +o the expression Li%% II M%% II 0%%N is executed first. 6he result of this expression is 3 $82 II 82 II 3 9 3&. 7ow the expression is 3 KK ? which evaluates to 2 $(ecause J5 operator always gives 2 except for L3 KK 3N com(ination8 for which it gives 3&. +o the value of m is 2. 6he values of other varia(les are also incremented (y 2. -. { } Answer: 2? main() char *p; printf("%d %d ",si7#of(*p),si7#of(p));

Explanation: 6he siAeof$& operator gives the num(er of (ytes ta0en (y its operand. O is a character pointer" which needs one (yte for storing its value $a character&. )ence siAeof$#p& gives a value of 2. +ince it needs two (ytes to store the address of the character pointer siAeof$p& gives ?. 8. { main() int i=!; s9itch(i) * d#fa$"t:printf("7#ro"); cas# 1: printf("on#"); %r#a4; cas# 2:printf("t9o"); %r#a4; cas# !: printf("thr##"); %r#a4; ,

} Answer : three Explanation : 6he default case can (e placed anywhere inside the loop. ,t is executed only when all other cases doesnFt match. 10. { } Answer: fff3 Explanation : 82 is internally represented as all 2Fs. @hen left shifted four times the least significant 1 (its are filled with 3Fs.6he Px format specifier specifies that the integer value (e printed as a hexadecimal value. 11. { main() char strin;[]="<#""o =or"d"; disp"a&(strin;); } void disp"a&(char *strin;) { printf("%s",strin;); } Answer: Compiler Error : 6ype mismatch in redeclaration of function display Explanation : ,n third line" when the function display is encountered" the compiler doesnFt 0now 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 display" 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); main() printf("%3",+122));

{ }
Answer: c9?H Explanation:

)ere unary minus $or negation& operator is used twice. +ame maths rules applies" ie. minus # minus9 plus. 7ote: )owever you cannot give li0e 88?. Because 88 operator can only (e applied to varia(les as a decrement operator $eg." i88&. ? is a constant and not a varia(le. 1!. >d#fin# int char main()

{ }
Answer:

int i=.5; printf("si7#of(i)=%d",si7#of(i));

siAeof$i&92

Explanation: +ince the Qdefine replaces the string int (y the macro char 1). main()

{
int i=10; i=?i@1); Arintf ("i=%d",i);

}
Answer: i93 Explanation: ,n the expression <i:21 " 7J6 $<& operator has more precedence than L :N sym(ol. < is a unary logical operator. <i $<23& is 3 $not of true is false&. 3:21 is false $Aero&. 15. >inc"$d# main() { char s[]=*BaB,B%B,BcB,B nB,BcB,B 0B,; char *p,*str,*str1; p=5s[!]; str=p; str1=s; printf("%d",++*p + ++*str1+!2); } Answer: RR Explanation: p is pointing to character FSnF. str2 is pointing to character FaF %%#p. "p is pointing to FSnF and that is incremented (y one." the A+C,, value of FSnF is 23" which is then incremented to 22. 6he value of %%#p is 22. %%#str2" str2 is pointing to FaF that is incremented (y 2

and it (ecomes F(F. A+C,, value of F(F is 4T. 7ow performing $22 % 4T U >?&" we get RR$"B"&H +o we get the output RR :: "B" $Ascii is RR&. 1.. >inc"$d# main() { int a[2][2][2] = * *10,2,!,),, *5,.,/,-, ,; int *p,*1; p=5a[2][2][2]; *1=***a; printf("%d++++%d",*p,*1); } Answer: +ome'ar(ageValue8882 Explanation: p9Ia ?! ?! ?! you declare only two ?/ arrays" (ut you are trying to access the third ?/$which you are not declared& it will print gar(age values. #D9###a starting address of a is assigned integer pointer. 7ow D is pointing to starting address of a. ,f you print #D" it will print first element of >/ array. 1/. >inc"$d# main() { str$ct 33 { int 3=!; char nam#[]="h#""o"; ,; str$ct 33 *s; printf("%d",s+@3); printf("%s",s+@nam#); } Answer: Compiler Error Explanation: Wou should not initialiAe varia(les in declaration 1-. >inc"$d# main() { str$ct 33 { int 3; str$ct && { char s; str$ct 33 *p;

,; str$ct && *1; ,; } Answer: Compiler Error Explanation: 6he structure yy is nested within structure xx. )ence" the elements are of yy are to (e accessed through the instance of structure xx" which needs an instance of yy to (e 0nown. ,f the instance is created after defining the structure the compiler will not 0now a(out the instance relative to xx. )ence for nested structure yy you have to declare mem(er. 18. main() { printf(" na%"); printf(" %si"); printf(" rha"); } Answer: hai Explanation: Sn 8 newline S( 8 (ac0space Sr 8 linefeed 20. main() { int i=5; printf("%d%d%d%d%d%d",i++,i++,++i,++i,i); } Answer: 1==1= Explanation: 6he arguments in a function call are pushed into the stac0 from left to right. 6he evaluation is (y popping out from the stac0. and the evaluation is from right to left" hence the result. 21. >d#fin# s1$ar#(3) 3*3 main() { int i; i = .)Cs1$ar#()); printf("%d",i); }

Answer: C1 Explanation: the macro call sDuare$1& will su(stituted (y 1#1 so the expression (ecomes i 9 C1*1#1 . +ince * and # has eDual priority the expression will (e evaluated as $C1*1&#1 i.e. 2C#1 9 C1 22. main() { char *p="hai fri#nds",*p1; p1=p; 9hi"#(*p?=B 0B) ++*p++; printf("%s %s",p,p1); } Answer: i(M<gsMfoet Explanation: %%#p%% will (e parse in the given order X #p that is value at the location currently pointed (y p will (e ta0en X %%#p the retrieved value will (e incremented X when H is encountered the location will (e incremented that is p%% will (e executed )ence" in the while loop initial value pointed (y p is LhN" which is changed to LiN (y executing %%#p and pointer moves to point" LaN which is similarly changed to L(N and so on. +imilarly (lan0 space is converted to L<N. 6hus" we o(tain value in p (ecomes Yi(M< gsMfoetZ and since p reaches LS3N and p2 points to p thus p2doesnot print anything. 2!. >inc"$d# >d#fin# a 10 main() { >d#fin# a 50 printf("%d",a); } Answer: =3 Explanation: 6he preprocessor directives can (e redefined anywhere in the program. +o the most recently assigned value will (e ta0en. 2). >d#fin# c"rscr() 100 main() { c"rscr(); printf("%d n",c"rscr()); } Answer:

233 Explanation: Oreprocessor executes as a seperate pass (efore the execution of the compiler. +o textual replacement of clrscr$& to 233 occurs.6he input program to compiler loo0s li0e this : main$& { 233H printf$"PdSn""233&H } 7ote: 233H is an executa(le statement (ut with no action. +o it doesnFt give any pro(lem. 25. main() { printf("%p",main); } Answer: +ome address will (e printed.

Explanation: .unction names are Must addresses $Must li0e array names are addresses&. main$& is also a function. +o the address of function main will (e printed. Pp in printf specifies that the argument is an address. 6hey are printed as hexadecimal num(ers. 2.. main() { c"rscr(); } c"rscr(); Answer: 7o output*error Explanation: 6he first clrscr$& occurs inside a function. +o it (ecomes a function call. ,n the second clrscr$&H is a function declaration $(ecause it is not inside any function&. 2/. #n$m co"ors *DEFGH,DE(I,JKIIL, main() { printf("%d..%d..%d",DEFGH,DE(I,JKIIL); r#t$rn(1); } Answer: 3..2..?

Explanation: enum assigns num(ers starting from 3" if not explicitly defined. 2-. void main() { char far *farth#r,*farth#st; printf("%d..%d",si7#of(farth#r),si7#of(farth#st)); , Answer: 1..? Explanation: the second pointer is of char type and not a far pointer 28. main() { int i=)00,0=!00; printf("%d..%d"); } Answer: 133..>33 Explanation: printf ta0es the values of the first two assignments of the program. Any num(er of printfFs may (e given. All of them ta0e only the first two values. ,f more num(er of assignments given in the program" then printf will ta0e gar(age values. !0. main() { char *p; p="<#""o"; printf("%c n",*5*p); } Answer: ) Explanation: # is a dereference operator I is a reference operator. 6hey 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 ). Again I references it to an address and # dereferences it to the value ). !1. main() { int i=1; 9hi"# (i2=5) *

printf("%d",i); if (i@2) ;oto h#r#; i++; , } f$n() { h#r#: printf("AA"); } Answer: Compiler error: -ndefined la(el FhereF in function main Explanation: Ea(els have functions scope" in other words 6he scope of the la(els is limited to functions . 6he la(el FhereF is availa(le in function fun$& )ence it is not visi(le in function main. !2. main() { static char nam#s[5][20]=*"pasca"","ada","co%o"","fortran","p#r"",; int i; char *t; t=nam#s[!]; nam#s[!]=nam#s[)]; nam#s[)]=t; for (i=0;i2=);i++) printf$"Ps""names i!&H } Answer: Compiler error: Evalue reDuired in function main Explanation: Array names are pointer constants. +o it cannot (e modified. !!. { } Answer: Jutput Cannot (e predicted exactly. Explanation: +ide effects are involved in the evaluation of i !). { void main() void main() int i=5; printf("%d",i++ + ++i);

int i=5; printf("%d",i+++++i); } Answer: Compiler Error Explanation: 6he expression i%%%%%i is parsed as i %% %% % i which is an illegal com(ination of operators. !5. >inc"$d# main() { int i=1,0=2; s9itch(i) * cas# 1: printf("JMMN"); %r#a4; cas# 0: printf("DFN"); %r#a4; , } Answer: Compiler Error: Constant expression reDuired in function main. Explanation: 6he case statement can have only constant expressions $this implies that we cannot use varia(le names directly so an error&. 7ote: Enumerated types can (e used in case statements. !.. main() { int i; printf("%d",scanf("%d",5i)); CC va"$# 10 is ;iv#n as inp$t h#r# } Answer: 2 Explanation: +canf returns num(er of items successfully read and not 2*3. )ere 23 is given as input which should have (een scanned successfully. +o num(er of items read is 2. !/. >d#fin# f(;,;2) ;>>;2 main() { int var12=100; printf("%d",f(var,12)); ,

Answer: 233 !-. main() { int i=0; for(;i++;printf("%d",i)) ; printf("%d",i); } Answer: 2 Explanation: (efore entering into the for loop the chec0ing condition is "evaluated". )ere it evaluates to 3 $false& and comes out of the loop" and i is incremented $note the semicolon after the for loop&. !8. >inc"$d# main() { char s[]=*BaB,B%B,BcB,B nB,BcB,B 0B,; char *p,*str,*str1; p=5s[!]; str=p; str1=s; printf("%d",++*p + ++*str1+!2); } Answer: B Explanation: p is pointing to character FSnF.str2 is pointing to character FaF %%#p meAnswer:"p is pointing to FSnF and that is incremented (y one." the A+C,, value of FSnF is 23. then it is incremented to 22. the value of %%#p is 22. %%#str2 meAnswer:"str2 is pointing to FaF that is incremented (y 2 and it (ecomes F(F. A+C,, value of F(F is 4T. (oth 22 and 4T is added and result is su(tracted from >?. i.e. $22%4T8>?&9RR$"B"&H )0. >inc"$d# main() { str$ct 33 * int 3=!; char nam#[]="h#""o"; ,; str$ct 33 *s=ma""oc(si7#of(str$ct 33)); printf("%d",s+@3);

printf("%s",s+@nam#); } Answer: Compiler Error Explanation: ,nitialiAation should not (e done for structure mem(ers inside the structure declaration )1. >inc"$d# main() { str$ct 33 * int 3; str$ct && * char s; str$ct 33 *p; ,; str$ct && *1; ,; , Answer: Compiler Error Explanation: in the end of nested structure yy a mem(er have to (e declared )2. main() { #3t#rn int i; i=20; printf("%d",si7#of(i)); } Answer: Ein0er error: undefined sym(ol FGiF. Explanation: extern declaration specifies that the varia(le i is defined somewhere else. 6he compiler passes the external varia(le to (e resolved (y the lin0er. +o compiler doesnFt find an error. /uring lin0ing the lin0er searches for the definition of i. +ince it is not found the lin0er flags an error. )!. main() { printf("%d", o$t); } int out9233H

Answer: Compiler error: undefined sym(ol out in function main. Explanation: 6he rule is that a varia(le is availa(le for use from the point of declaration. Even though a is a glo(al varia(le" it is not availa(le for main. )ence an error. )). main() { #3t#rn o$t; printf("%d", o$t); } int o$t=100; Answer: 233 Explanation: 6his is the correct way of writing the previous program. )5. main() { sho9(); } void sho9() { printf("'Bm th# ;r#at#st"); } Answer: Compier error: 6ype mismatch in redeclaration of show. Explanation: @hen the compiler sees the function show it doesnFt 0now 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. 6he solutions are as follows: 2. declare void show$& in main$& . ?. define show$& (efore main$&. >. declare extern void show$& (efore the use of show$&. ).. main( ) { int a[2][!][2] = ***2,),,*/,-,,*!,),,,**2,2,,*2,!,,*!,),,,; printf(O%$ %$ %$ %d nP,a,*a,**a,***a); printf(O%$ %$ %$ %d nP,a+1,*a+1,**a+1,***a+1); , Answer: 233" 233" 233" ? 221" 231" 23?" >

)/. main( ) { int a[ ] = *10,20,!0,)0,50,,0,*p; for(0=0; 025; 0++) * printf(O%dP ,*a); a++; , p = a; for(0=0; 025; 0++) * printf(O%d P ,*p); p++; , , Answer: Compiler error: lvalue reDuired. Explanation: Error is in line with statement a%%. 6he 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 non8modifia(le lvalue. )-. main( ) { static int a[ ] = *0,1,2,!,),; int *p[ ] = *a,a+1,a+2,a+!,a+),; int **ptr = p; ptr++; printf(O n %d %d %dP, ptr+p, *ptr+a, **ptr); *ptr++; printf(O n %d %d %dP, ptr+p, *ptr+a, **ptr); *++ptr; printf(O n %d %d %dP, ptr+p, *ptr+a, **ptr); ++*ptr; printf(O n %d %d %dP, ptr+p, *ptr+a, **ptr); } Answer: 222 ??? >>> >11 )8. main( ) { void *vp; char ch = Q;R, *cp = O;oof&P; int 0 = 20; vp = 5ch; printf(O%cP, *(char *)vp);

vp = 50; printf(O%dP,*(int *)vp); vp = cp; printf(O%sP,(char *)vp + !); } Answer: g?3fy Explanation: +ince a void pointer is used it can (e type casted to any other type pointer. vp 9 Ich stores address of char ch and the next statement prints the value stored in vp after type casting it to the proper data type pointer. the output is LgN. +imilarly the output from second printf is L?3N. 6he third printf statement type casts it to print the string from the 1th value hence the output is LfyN. 50. main ( ) { static char *s[ ] = *O%"ac4P, O9hit#P, O&#""o9P, Ovio"#tP,; char **ptr[ ] = *s+!, s+2, s+1, s,, ***p; p = ptr; **++p; printf(O%sP,*++*++p + !); } Answer: c0 Explanation: ,n this pro(lem we have an array of char pointers pointing to start of 1 strings. 6hen 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 9 s%>. 6he next statement increment value in p (y 2 " thus now value of p 9 s%?. ,n the printf statement the expression is evaluated #%%p causes gets value s%2 then the pre decrement is executed and we get s%2 U 2 9 s . the indirection operator now gets the value from the array of s and adds > to the starting address. 6he string is printed starting from this position. 6hus" the output is Lc0N. 51. main() { int i, n; char *3 = O;ir"P; n = str"#n(3); *3 = 3[n]; for(i=0; i * printf(O%s nP,3); 3++; , , Answer:

$(lan0 space& irl rl l Explanation: )ere a string $a pointer to char& is initialiAed with a value YgirlZ. 6he strlen function returns the length of the string" thus n has a value 1. 6he next statement assigns value at the nth location $LS3N& to the first location. 7ow the string (ecomes YS3irlZ . 7ow the printf statement prints the string after each iteration it increments it starting position. Eoop starts from 3 to 1. 6he first time x 3! 9 LS3N hence it prints nothing and pointer value is incremented. 6he second time it prints from x 2! i.e YirlZ and the third time it prints YrlZ and the last time it prints YlZ and the loop terminates. 52. int i,0; for(i=0;i2=10;i++) * 0+=5; ass#rt(i25); ,

Answer: 5untime error: A(normal program termination. assert failed $i;=&" " Explanation: asserts are used during de(ugging to ma0e sure that certain conditions are satisfied. ,f assertion fails" the program will terminate reporting the same. After de(ugging use" Qundef 7/EB-' and this will disa(le all the assertions from the source code. Assertion is a good de(ugging tool to ma0e use of. 5!. main() * int i=+1; +i; printf("i = %d, +i = %d n",i,+i); ,

Answer: i 9 82" %i 9 82 Explanation: -nary % is the only dummy operator in C. @here8ever it comes you can Must ignore it Must (ecause it has no effect in the expressions $hence the name dummy operator&. 5). =hat ar# th# fi"#s 9hich ar# a$tomatica""& op#n#d 9h#n a G fi"# is #3#c$t#dS Answer: stdin" stdout" stderr $standard input"standard output"standard error&.

55. 9hat 9i"" %# th# position of th# fi"# mar4#rS a: fsee0$ptr"3"+EE[G+E6&H (: fsee0$ptr"3"+EE[GC-5&H Answer : a: 6he +EE[G+E6 sets the file position mar0er to the starting of the file. (: 6he +EE[GC-5 sets the file position mar0er to the current position of the file. 5.. main() * char nam#[10],s[12]; scanf(" "%[T "] "",s); , <o9 scanf 9i"" #3#c$t#S

Answer: .irst it chec0s for the leading white space and discards it.6hen it matches with a Duotation mar0 and then it reads all character upto another Duotation mar0. 5/. =hat is th# pro%"#m 9ith th# fo""o9in; cod# s#;m#ntS 9hi"# ((f;#ts(r#c#ivin; arra&,50,fi"#Uptr)) ?= IMV) H Answer I Explanation: fgets returns a pointer. +o the correct end of file chec0 is chec0ing for <9 7-EE. 5-. main() * main(); ,

Answer: 5untime error : +tac0 overflow. Explanation: main function calls itself again and again. Each time the function is called its return address is stored in the call stac0. +ince there is no condition to terminate the function call" the call stac0 overflows at runtime. +o it terminates the program and results in an error. 58. main() * char *cptr,c; void *vptr,v; c=10; v=0; cptr=5c; vptr=5v; printf("%c%v",c,v); ,

Answer: Compiler error $at line num(er 1&: siAe of v is -n0nown. Explanation:

Wou 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. .0. main() * char *str1="a%cd"; char str2[]="a%cd"; printf("%d %d %d",si7#of(str1),si7#of(str2),si7#of("a%cd")); ,

Answer: ?== Explanation: ,n first siAeof" str2 is a character pointer so it gives you the siAe of the pointer varia(le. ,n second siAeof the name str? indicates the name of the array whose siAe is = $including the FS3F termination character&. 6he third siAeof is similar to the second one. .1. main() * char not; not=?2; printf("%d",not); ,

Answer: 3 Explanation: < is a logical operator. ,n C the value 3 is considered to (e the (oolean value .AE+E" and any non8Aero value is considered to (e the (oolean value 65-E. )ere ? is a non8Aero value so 65-E. <65-E is .AE+E $3& so it prints 3. .2. >d#fin# VFEWI +1 >d#fin# XK(I 1 >d#fin# L(EE 0 main() * if(L(EE) p$ts("L(EE"); #"s# if(VFEWI) p$ts("XK(I"); #"s# p$ts("VFEWI"); ,

Answer: 65-E Explanation: 6he input program to the compiler after processing (y the preprocessor is" main$&{

if$3& else if$82& else

puts$"7-EE"&H puts$"65-E"&H

puts$".AE+E"&H } Oreprocessor doesnFt replace the values given inside the dou(le Duotes. 6he chec0 (y if condition is (oolean value false so it goes to else. ,n second if 82 is (oolean value true hence "65-E" is printed. .!. main() * int 4=1; printf("%d==1 is ""%s",4,4==1S"XK(I":"VFEWI"); ,

Answer: 2992 is 65-E Explanation: @hen two strings are placed together $or separated (y white8space& they are concatenated $this is called as "stringiAation" operation&. +o the string is as if it is given as "Pd992 is Ps". 6he conditional operator$ \: & evaluates to "65-E". .). main() * int &; scanf("%d",5&); CC inp$t ;iv#n is 2000 if( (&%)==0 55 &%100 ?= 0) 66 &%100 == 0 ) printf("%d is a "#ap &#ar"); #"s# printf("%d is not a "#ap &#ar"); ,

Answer: ?333 is a leap year Explanation: An ordinary program to chec0 if leap year or not. .5. >d#fin# ma3 5 >d#fin# int arr1[ma3] main() * t&p#d#f char arr2[ma3]; arr1 "ist=*0,1,2,!,),; arr2 nam#="nam#"; printf("%d %s","ist[0],nam#); ,

Answer:

Compiler error $in the line arr2 list 9 {3"2"?">"1}& Explanation: arr? is declared of type array of siAe = of characters. +o it can (e used to declare the varia(le name of the type arr?. But it is not the case of arr2. )ence an error. 5ule of 6hum(: Qdefines are used for textual replacement whereas typedefs are used for declaring new types. ... int i=10; main() * #3t#rn int i; * int i=20; * const vo"ati"# $nsi;n#d i=!0; printf("%d",i); , printf("%d",i); , printf("%d",i); ,

Answer: >3"?3"23 Explanation: F{F introduces new (loc0 and thus new scope. ,n the innermost (loc0 i is declared as" const volatile unsigned which is a valid declaration. i is assumed of type int. +o printf prints >3. ,n the next (loc0" i has value ?3 and so printf prints ?3. ,n the outermost (loc0" i is declared as extern" so no storage space is allocated for it. After compilation is over the lin0er resolves it to glo(al varia(le i $since it is the only varia(le visi(le there&. +o it prints iFs value as 23. ./. main() * int *0; * int i=10; 0=5i; , printf("%d",*0);

} Answer: 23 Explanation: 6he varia(le i is a (loc0 level varia(le and the visi(ility is inside that (loc0 only. But the

lifetime of i is lifetime of the function so it lives upto the exit of main function. +ince the i is still allocated space" #M prints the value stored in i since M points i. .-. main() * int i=+1; +i; printf("i = %d, +i = %d n",i,+i); ,

Answer: i 9 82" 8i 9 2 Explanation: 8i is executed and this execution doesnFt affect the value of i. ,n printf first you Must print the value of i. After that the value of the expression 8i 9 8$82& is printed. .8. >inc"$d# main() * const int i=); f"oat 0; 0 = ++i; printf("%d %f", i,++0); , Answer: Compiler error Explanation: i is a constant. you cannot change the value of constant /0. >inc"$d# main() { int a[2][2][2] = * *10,2,!,),, *5,.,/,-, ,; int *p,*1; p=5a[2][2][2]; *1=***a; printf("%d..%d",*p,*1); } Answer: gar(agevalue..2 Explanation: p9Ia ?! ?! ?! you declare only two ?/ arrays. (ut you are trying to access the third ?/$which you are not declared& it will print gar(age values. #D9###a starting address of a is assigned integer pointer. now D is pointing to starting address of a.if you print #D meAnswer:it will print first element of >/ array.

/1. >inc"$d# main() * r#;ist#r i=5; char 0[]= "h#""o"; printf("%s %d",0,i); } Answer: hello = Explanation: if you declare i as register compiler will treat it as ordinary integer and it will ta0e integer value. i value may (e stored either in register or in memory. /2. { main() int i=5,0=.,7; printf("%d",i+++0); , Answer: 22 Explanation: the expression i%%%M is treated as $i%% % M&

Y. =hat 9i"" %# th# o$tp$t of th# fo""o9in; stat#m#nt S C* C* printf("h#""o"); *C *C a) h#""o %) no o$tp$t c) #rror d) "h#""o" FLW=IK : G Explanation : 7ested comments are not allowed in FCF. Notes : Comments should (e written in *# 88888888888888 #* without any nesting in (etween. Y. =hat 9i"" %# th# o$tp$t of th# fo""o9in; stat#m#nt S printf("%i",!5,2+-*5%10+2); a) #rror %) 0 c) !5 d) !50 FLW=IK : G Explanation : printf$ "Pi "" >=" ?%T#=P238? &H As no format specifier is mentioned for the expression " ?%T#=P238? " " hence only ">=" will get printed on the screen. 7otes : .ormat specifiers int float char 8 8 8 Pi " Pd Pf Pc

Y. =hat 9i"" %# th# o$tp$t of th# fo""o9in; stat#m#nt S int a=10; printf("%d 5i",a,10); a) #rror %) 10 c) 10 10 d) non# of th#s# FLW=IK : N Explanation : int a 9 23H printf$" Pd Ii ""a"23&H

.ormat specifier for "a" is mentioned " hence value of "a" will get printed on screen . 7o format specifier is mentioned for "23" hence it will not get printed on screen . +o final output will (e "23 Ii". Y/. =hat 9i"" %# th# o$tp$t of th# fo""o9in; stat#m#nt S printf("%Z%3%ci%3",11,10,BsB,12); a) #rror %) %asc c) Das8)c d) non# of th#s# FLW=IK : N Explanation : printf$"P]PxPciPx""22"23"FsF"2?&H 22"23"2? will get converted into eDuivalent hexadecimal entities . 22 23 8: 2? 8: B a c

hence final output will (e "Basic".

Y-. =hat 9i"" %# th# o$tp$t of th# fo""o9in; stat#m#nts S int a = printf("00"); printf("%d",a); a) 0 %) 00 c) 002 d) ;ar%a;# va"$# FLW=IK : G Explanation : int a 9 printf$"33"&H printf$"Pd""a&H

printf$& function returns the num(er of characters (eing printed on screen. )ence "?" gets stored in varia(le "a". 6herefore the final output is "33?".

Y8. =hat 9i"" %# th# o$tp$t of th# fo""o9in; stat#m#nts S int a = !,% = -; printf("%d", a2=%); a) #rror %) ;ar%a;# va"$# c) ! d) 1 FLW=IK : N Explanation : int a 9 >"( 9 TH printf$"Pd"" a;9(&H

6he condition "a;9(" is true " hence "2" gets printed out. ,f the condition is false "3" gets printed out. Notes : 'eneral .orm : printf$"format string""list&H Eist can contain varia(les " constants or expressions . )owever list is optional. ,n the a(ove given pro(lem "a;9(" is an expression which is a perfectly valid syntax.

Y10. =hat 9i"" %# th# o$tp$t of th# fo""o9in; stat#m#nts S int a = ), % = /,c; c = a = = %; printf("%i",c); a) 0 %) #rror c) 1 d) ;ar%a;# va"$# Explanation : int a 9 1" ( 9 R"cH c 9 a 9 9 (H printf$"Pi""c&H

6he condition $a 9 9 (& is false . )ence "3" gets stored in varia(le "c" which eventually gets printed out. Y12. =hat 9i"" %# th# o$tp$t of th# fo""o9in; pro;ram S >inc"$d#2stdio.h@ >inc"$d#2math.h@ void main() * f"oat a = !.2.; printf("%f",c#i"(a)); , a) ).0 %) !.0 c) !.2 d) non# of th#s# FLW=IK : F Explanation : float a 9 >.?CH printf$"Pf""ceil$a&&H

"ceil$&" function present in the header file "math.h" rounds up the value of "a" from ">.?C" to "1.3".

7otes : "floor$&" function does the opposite of "ceil$&" function .i.e "floor$&" function rounds down the value. .or example : >.?C to >.3 Y 1!. =hat 9i"" %# th# o$tp$t of th# fo""o9in; stat#m#nts S int a = 5, % = 2, c = 10, i = a@%2c; printf("%d",i); a) #rror

%) 1 c) ;ar%a;# va"$# d) 5 FLW=IK : D Explanation : int a 9 =" ( 9 ?" c 9 23" i 9 a:(;cH printf$"Pd""i&H

+ince $a:(& is true it is replaced (y "2". 7ow "2" is compared with "c". +ince $2;23& is true it is replaced (y "2".

Y 1). =hat 9i"" %# th# o$tp$t of th# fo""o9in; pro;ram S >inc"$d#2stdio.h@ int a = 10; void main() * int a = 50; printf("%d",a); , a) 50 %) #rror c) 10 d) ;ar%a;# va"$# FLW=IK : F Explanation : Qinclude;stdio.h: int a 9 23H void main$& { int a 9 =3H printf$"Pd""a&H } ** "a" is a local varia(le here ** "a" is a glo(al varia(le here

@henever there is a conflict " local varia(le enMoys a higher priority than glo(al varia(le. Y 15. =hat 9i"" %# th# o$tp$t of th# fo""o9in; pro;ram S >inc"$d#2stdio.h@ void main() * #3t#rn int 3; printf("%d",3); ,int 3 = 10; a) #rror %) 0

c) ;ar%a;# va"$# d) 10 FLW=IK : N Explanation : Qinclude;stdio.h: void main$& { extern int xH printf$"Pd""x&H }int x 9 23H ** definition ** declaration not definition

As declaration of external varia(le is already done in main$& " hence definition of external varia(le can (e done outside main$&. Y 1.. =hat 9i"" %# th# o$tp$t of th# fo""o9in; pro;ram S >inc"$d#2stdio.h@ void main() * str$ct p * int a,c ; f"oat %; ,d = *1,; printf("%d%d%f",d.a,d.c,d.%); , a) ;ar%a;# va"$# %) 100.0 c) #rror d) 000.0 FLW=IK : D Explanation : void main$& { struct p { int a"c H float (H }d 9 {2}H ** partiallyinitialised

printf$"PdPdPf""d.a"d.c"d.(&H }

)ere the structure $automatic storage class& is partially initialised. )ence the remaining elements of structure are initialised to "3" Y1-. =hat 9i"" %# th# o$tp$t of th# fo""o9in; stat#m#nts S int *a; f"oat far *%; char h$;#*c printf("%d,%d,%d",si7#of(a),si7#of(%),si7#of(c)); a) 222 %) ))) c) 2)) d) 22) FLW=IK : G Explanation : 6hree types of pointers are near " far and huge. By default every pointer is a "near pointer" unless and until "far" and "huge" 0eywords are defined with it. +iAe of all the "near pointers" is "?" under /J+. +iAe of all "far" and "huge" pointers is "1" under /J+. Y18. =hat 9i"" %# th# o$tp$t of th# fo""o9in; stat#m#nts S f"oat 4 = !.-); printf("%d",(int)4); a) !.%) !.0 c) ! d) ) FLW=IK : G Explanation : float 0 9 >.T1H printf$"Pd""$int&0&H

6ypecasting is (eing done i.e float is (eing converted into int. >.T1 gets truncated to >. Y20. <o9 man& tim#s th# fo""o9in; pro;ram 9i"" print "h#""o" S >inc"$d#2stdio.h@ void main() * printf("h#""o"); main(); ,

a) 1 %) 2 c) infinit# n$m%#r of tim#s d) non# of th#s# FLW=IK : N Explanation : Qinclude;stdio.h: void main$& { printf$"hello"&H main$&H }

6he program will printf "hello" till the stac0 doesnFt overflow.

Вам также может понравиться