You are on page 1of 65

C Sample Question

Note : All the C sample programs are tested under Turbo C/C++ compilers. It is assumed that, Programs run under !S en"ironment, The underl#ing machine is an $%& s#stem,

Program is compiled using Turbo C/C++ compiler.

1.void main() { int const * p=5; printf("%d",++(*p)); } Ans'er: Compiler error: Cannot modify a constant value.

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]); } Ans'er: mmmm aaaa nnnn ($planation: s[i], *(i+s), *(s+i), i[s] are all different ways of expressin t!e same idea. "enerally array name is t!e #ase address for t!at array. $ere s is t!e #ase address. i is t!e index num#er%displacement from t!e #ase address. &o, indirectin it wit! * is same as s[i]. i[s] may #e surprisin . 'ut in t!e 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# ("); } Ans'er: ) !ate * ($planation: +or floatin point num#ers (float, dou#le, lon dou#le) t!e values cannot #e predicted exactly. ,ependin on t!e num#er of #ytes, t!e precession wit! of t!e value represented varies. +loat ta-es . #ytes and lon dou#le ta-es /0 #ytes. &o float stores 0.1 wit! less precision t!an lon dou#le. )ule o* Thumb: Never compare or at-least be cautious when using floating point numbers with relational operators (== , >, <, <=, >=,!= ).

.. main() { int i = )5*; int *i+tr = ,i; printf("%d %d", *((char*)i+tr), *((char*)i+tr+1) ); } Ans'er: // ($planation: 2!e inte er value 234 is stored in t!e memory as, 0000000/ 0000000/, so t!e individual #ytes are ta-en #y castin it to c!ar * and et printed.

main() { int i = )5-; int *i+tr = ,i; printf("%d %d", *((char*)i+tr), *((char*)i+tr+1) ); } Ans'er: 2/ ($planation: 2!e inte er value 234 can #e represented in #inary as, 0000000/ 0000000/. 5emem#er t!at t!e )6278 mac!ines are 9small:endian9 mac!ines. &mall:endian means t!at t!e lower order #ytes are stored in t!e !i !er memory addresses and t!e !i !er order #ytes are stored in lower addresses. 2!e inte er value 23; is stored in memory as: 0000000/ 000000/0. main() { int i=. ; char *ptr = ,i; *++ptr=); printf("%d",i); } Ans'er: 33< ($planation: 2!e inte er value (00 in #inary notation is: 0000000/ 00/0//00. )t is stored in memory (small:endian) as: 00/0//00 0000000/. 5esult of t!e expression *++ptr = 2 ma-es t!e memory representation as: 00/0//00 000000/0. &o t!e inte er correspondin to it is 000000/0 00/0//00 => 33<. /inc"$d# 0stdio.h1 main() { char * str = "h#""o"; char * ptr = str; char "#ast = 1)*; 2hi"# (*ptr++) "#ast = (*ptr 3 "#ast ) 4*ptr 5"#ast;

printf("%d","#ast); } Ans'er: 0 ($planation: ?fter 9ptr9 reac!es t!e end of t!e strin t!e value pointed #y 9str9 is 9@09. &o t!e value of 9str9 is less t!an t!at of 9least9. &o t!e value of 9least9 finally is 0. main() { str$ct st$d#nt { char nam#[. ]; str$ct dat# do%; }st$d; str$ct dat# { int da&,month,&#ar; }; scanf("%s%d%d%d", st$d.ro""no, ,st$d#nt.do%.da&, ,st$d#nt.do%.month, ,st$d#nt.do%.&#ar); } Ans'er: Compiler 7rror: *ndefined structure date ($planation: )nside t!e struct definition of 9student9 t!e mem#er of type struct date is iven. 2!e compiler doesn9t !ave t!e definition of date structure (forward reference is not allowed in C in t!is case) so it issues an error. main() { char *p="6778"; char a[ ]="6778"; printf("!n si9#of(p) = %d, si9#of(*p) = %d, str"#n(p) = %d", si9#of(p), si9#of(*p), str"#n(p)); printf("!n si9#of(a) = %d, str"#n(a) = %d", si9#of(a), str"#n(a));

} Ans'er: siAeof(p) = 2, siAeof(*p) = /, strlen(p) = . siAeof(a) = 3, strlen(a) = . ($planation: siAeof(p) => siAeof(c!ar*) => 2 siAeof(*p) => siAeof(c!ar) => / &imilarly, siAeof(a) => siAe of t!e c!aracter array => 3 B!en siAeof operator is applied to an array it returns t!e siAeof t!e array and it is not t!e same as t!e siAeof t!e pointer varia#le. $ere t!e siAeof(a) w!ere a is t!e c!aracter array and t!e siAe of t!e array is 3 #ecause t!e space necessary for t!e terminatin 6*88 c!aracter s!ould also #e ta-en into account. /d#fin# 8':( arra&, t&p#) si9#of(arra&);si9#of(t&p#) main() { int arr[1 ]; printf("<h# dim#nsion of th# arra& is %d", 8':(arr, int)); } Ans'er: /0 ($planation: 2!e siAe of inte er array of /0 elements is /0 * siAeof(int). 2!e macro expands to siAeof(arr)%siAeof(int) => /0 * siAeof(int) % siAeof(int) => /0. int 8':(int arra&[]) { r#t$rn si9#of(arra&);si9#of(int ); } main() { int arr[1 ]; printf("<h# dim#nsion of th# arra& is %d", 8':(arr)); } Ans'er:

/ ($planation: ?rrays cannot #e passed to functions as ar uments and only t!e pointers can #e passed. &o t!e ar ument is eCuivalent to int * array (t!is is one of t!e very few places w!ere [] and * usa e are eCuivalent). 2!e return statement #ecomes, siAeof(int *)% siAeof(int) t!at !appens to #e eCual in t!is case. main() { static int a[.][.]={1,),.,=,5,>,*,-,?}; int i,@; static *p[]={a,a+1,a+)}; for(i= ;i3.;i++) { for(@= ;@3.;@++) printf("%d!t%d!t%d!t%d!n",*(*(p+i)+@), *(*(@+p)+i),*(*(i+p)+@),*(*(p+@)+i)); } } Ans'er: 1 ) . = 5 > * ? ($planation: *(*(p+i)+D) is eCuivalent to p[i][D]. main() { void s2ap(); int A=1 ,&=-; s2ap(,A,,&); printf("A=%d &=%d",A,&); 1 = * ) 5 . > ? 1 ) . = 5 > * ? 1 = * ) 5 . > ?

} void s2ap(int *a, int *%) { *a B= *%, *% B= *a, *a B= *%; } Ans'er: x=/0 y=; ($planation: *sin E li-e t!is is a way to swap two varia#les wit!out usin a temporary varia#le and t!at too in a sin le statement. )nside main(), void swap()F means t!at swap is a function t!at may ta-e any num#er of ar uments (not no ar uments) and returns not!in . &o t!is doesn9t issue a compiler error #y t!e call swap(Gx,Gy)F t!at !as two ar uments. 2!is convention is !istorically due to pre:?6&) style (referred to as Herni !an and 5itc!ie style) style of functiondeclaration. )n t!at style, t!e swap function will #e defined as follows, void swap() int *a, int *% { *a B= *%, *% B= *a, *a B= *%; } w!ere t!e ar uments follow t!e (). &o naturally t!e declaration for swap will loo- li-e, void swap() w!ic! means t!e swap can ta-e any num#er of ar uments.

main() { int i==,@=*; @ = @ CC i++ ,, printf("D7( EFG"); printf("%d %d", i, @); }

Answer
./ ($planation:

2!e #oolean expression needs to #e evaluated only till t!e trut! value of t!e expression is not -nown. D is not eCual to Aero itself means t!at t!e expression9s trut! value is /. 'ecause it is followed #y II and true II (anyt!in ) => true w!ere (anyt!in ) will not #e evaluated. &o t!e remainin expression is not evaluated and so t!e value of i remains t!e same. &imilarly w!en GG operator is involved in an expression, w!en any of t!e operands #ecome false, t!e w!ole expression9s trut! value #ecomes false and !ence t!e remainin expression will not #e evaluated. false GG (anyt!in ) => false w!ere (anyt!in ) will not #e evaluated. main() { r#Hist#r int a=); printf("Fddr#ss of a = %d",,a); printf("Ia"$# of a = %d",a); } Ans'er: Compier 7rror: 9G9 on re ister varia#le 5ule to 5emem#er: G (address of ) operator cannot #e applied on re ister varia#les. main() { f"oat i=1.5; s2itch(i) { cas# 15 printf("1"); cas# )5 printf(")"); d#fa$"t 5 printf(" "); } } Ans'er: Compiler 7rror: switc! expression not inte ral ($planation: &witc! statements can #e applied only to inte ral types. main() { #At#rn i; printf("%d!n",i); { int i=) ; printf("%d!n",i);

} } Ans'er: 8in-er 7rror : *nresolved external sym#ol i ($planation: 2!e identifier i is availa#le in t!e inner #loc- and so usin extern !as no use in resolvin it. main() { int a=),*f1,*f); f1=f)=,a; *f)+=*f)+=a+=).5; printf("!n%d %d %d",a,*f1,*f)); } Ans'er: /< /< /< ($planation: f/ and f2 #ot! refer to t!e same memory location a. &o c!an es t!rou ! f/ and f2 ultimately affects only t!e value of a. void main() { int *mptr, *cptr; mptr = (int*)ma""oc(si9#of(int)); printf("%d",*mptr); int *cptr = (int*)ca""oc(si9#of(int),1); printf("%d",*cptr); } Ans'er: ar#a e:value 0 ($planation:

2!e memory space allocated #y malloc is uninitialiAed, w!ereas calloc returns t!e allocated memory space initialiAed to Aeros. void main() { static int i; 2hi"#(i3=1 ) (iJ))4i++5iKK; printf("%d", i); } Ans'er: (24<4 ($planation: &ince i is static it is initialiAed to 0. )nside t!e w!ile loop t!e conditional operator evaluates to false, executin i::. 2!is continues till t!e inte er value rotates to positive value ((24<4). 2!e w!ile condition #ecomes false and !ence, comes out of t!e w!ile loop, printin t!e i value. main() { int i=1 ,@=) ; @ = i, @4(i,@)4i5@5@; printf("%d %d",i,@); } Ans'er: /0 /0 ($planation: 2!e 2ernary operator ( J : ) is eCuivalent for if:t!en:else statement. &o t!e Cuestion can #e written as: if(i,@) { if(i,@) @ = i; #"s# @ = @;

} #"s# @ = @;

/. const c!ar *aF 2. c!ar* const aF (. c!ar const *aF

:,ifferentiate t!e a#ove declarations. Ans'er: .. 9const9 applies to c!ar * rat!er t!an 9a9 ( pointer to a constant c!ar ) *a=9+9 : ille al a=K$iK : le al 3. 9const9 applies to 9a9 rat!er t!an to t!e value of a (constant pointer to c!ar ) *a=9+9 : le al a=K$iK : ille al <. &ame as /. main() { int i=5,@=1 ; i=i,=@,,1 ; printf("%d %d",i,@); } Ans'er: / /0 ($planation: 2!e expression can #e written as i=(iG=(DGG/0))F 2!e inner expression (DGG/0) evaluates to / #ecause D==/0. i is 3. i = 3G/ is /. $ence t!e result. main() { char p[ ]="%d!n";

p[1] = LcL; printf(p,>5); } Ans'er: ? ($planation: ,ue to t!e assi nment p[/] = 9c9 t!e strin #ecomes, KLc@nK. &ince t!is strin #ecomes t!e format strin for printf and ?&C)) value of <3 is 9?9, t!e same ets printed. void ( * a%c( int, void ( *d#f) () ) ) (); Ans'er: a#c is a ptr to a function w!ic! ta-es 2 parameters (a) an inte er varia#le (#)a ptrto a funtion w!ic! returns void. t!e return type of t!e function is void. ($planation: ?pply t!e cloc-:wise rule to find t!e result. main() { 2hi"# (strcmp("som#","som#! ")) printf("MtrinHs ar# not #N$a"!n"); } Ans'er: 6o output ($planation: 7ndin t!e strin constant wit! @0 explicitly ma-es no difference. &o KsomeK and Ksome@0K are eCuivalent. &o, strcmp returns 0 (false) !ence #rea-in out of t!e w!ile loop. main() { char str1[] = {LsL,LoL,LmL,L#L}; char str)[] = {LsL,LoL,LmL,L#L,L! L}; 2hi"# (strcmp(str1,str))) printf("MtrinHs ar# not #N$a"!n"); }

Ans'er: K&trin s are not eCualK K&trin s are not eCualK ... ($planation: )f a strin constant is initialiAed explicitly wit! c!aracters, 9@09 is not appended automatically to t!e strin . &ince str/ doesn9t !ave null termination, it treats w!atever t!e values t!at are in t!e followin positions as part of t!e strin until it randomly reac!es a 9@09. &o str/ and str2 are not t!e same, !ence t!e result. main() { int i = .; for (;i++= ;) printf("%d",i); } Ans'er: Compiler 7rror: 8value reCuired. ($planation: ?s we -now t!at increment operators return rvalues and !ence it cannot appear on t!e left !and side of an assi nment operation. main() { char *p = "a&Nm"; printf("%c",++*(p++)); } Answer: ' main() { int i=5; printf("%d",++i++); } Ans'er: Compiler error: 8value reCuired in function main ($planation:

++i yields an rvalue. +or postfix ++ to operate an lvalue is reCuired. main() { char *p = "a&Nm"; char c; c = ++*p++; printf("%c",c); } Ans'er: # ($planation: 2!ere is no difference #etween t!e expression ++*(p++) and ++*p++. Marent!esis Dust wor-s as a visual clue for t!e reader to see w!ic! expression is first evaluated. int aaa() {printf("Oi");} int %%%(){printf("h#""o");} in& ccc(){printf("%&#");} main() { int ( * ptr[.]) (); ptr[ ] = aaa; ptr[1] = %%%; ptr[)] =ccc; ptr[)](); } Ans'er: #ye ($planation: int (* ptr[(])() says t!at ptr is an array of pointers to functions t!at ta-es no ar uments and returns t!e type int. 'y t!e assi nment ptr[0] = aaaF it means t!at t!e first function pointer in t!e array is initialiAed wit! t!e address of t!efunction aaa. &imilarly, t!e ot!er two array elements also et initialiAed wit! t!e addresses of t!e functions ### and ccc. &ince ptr[2] contains t!e address of t!e function ccc, t!e call to t!e function ptr[2]() is same as callin ccc(). &o it results in printin K#yeK. main()

{ int i=5; printf("%d",i=++i ==>); } Ans'er: / ($planation: 2!e expression can #e treated as i = (++i==<), #ecause == is of !i !er precedence t!an = operator. )n t!e inner expression, ++i is eCual to < yieldin true(/). $ence t!e result. t&p#d#f str$ct #rror{int 2arninH, #rror, #Ac#ption;}#rror; main() { #rror H1; H1.#rror =1; printf("%d",H1.#rror); } Ans'er: / ($planation: 2!e t!ree usa es of name errors can #e distin uis!a#le #y t!e compiler at any instance, so valid (t!ey are in different namespaces). 2ypedef struct errorNint warnin , error, exceptionFOerrorF 2!is error can #e used only #y precedin t!e error #y struct -ayword as in: struct error some7rrorF typedef struct errorNint warnin , error, exceptionFOerrorF 2!is can #e used only after . (dot) or :> (arrow) operator preceded #y t!e varia#le name as in : /.error =/F printf(KLdK, /.error)F typedef struct errorNint warnin , error, exceptionFOerrorF 2!is can #e used to define varia#les wit!out usin t!e precedin struct -eyword as in: error /F &ince t!e compiler can perfectly distin uis! #etween t!ese t!ree usa es, it is perfectly le al and valid. Note 2!is code is iven !ere to Dust explain t!e concept #e!ind. )n real pro rammin don9t use suc! overloadin of names. )t reduces t!e reada#ility of t!e code. Mossi#le doesn9t mean t!at we s!ould use itP

/ifd#f som#thinH int som#= ; /#ndif main() { int thinH = ; printf("%d %d!n", som# ,thinH); } Ans'er: Compiler error : undefined sym#ol some ($planation: 2!is is a very simple example for conditional compilation. 2!e name somet!in is not already -nown to t!e compiler ma-in t!e declaration int some = 0F effectively removed from t!e source code. /if som#thinH == int som#= ; /#ndif main() { int thinH = ; printf("%d %d!n", som# ,thinH); } Ans'er: 00 ($planation: 2!is code is to s!ow t!at preprocessor expressions are not t!e same as t!e ordinary expressions. )f a name is not -nown t!e preprocessor treats it to #e eCual to Aero. void main() { if(P == ($nsiHn#d int)K1) printf("Do$ can ans2#r this if &o$ Qno2 ho2 va"$#s ar# r#pr#s#nt#d in m#mor&"); }

Ans'er Qou can answer t!is if you -now !ow values are represented in memory ($planation R (tilde operator or #it:wise ne ation operator) operates on 0 to produce all ones to fill t!e space for an inte er. :/ is represented in unsi ned value as all /9s and so #ot! are eCual. int s2ap(int *a,int *%) { *a=*a+*%;*%=*aK*%;*a=*aK*%; } main() { int A=1 ,&=) ; s2ap(,A,,&); printf("A= %d & = %d!n",A,&); } Ans'er: x = 20 y = /0 ($planation 2!is is one way of swappin two values. &imple c!ec-in will !elp understand t!is. B!at is t!e output of t!e pro ram iven #elow main() { siHn#d char i= ; for(;iJ= ;i++) ; printf("%d!n",i); } Ans'er: :/2; ($planation: 6otice t!e semicolon at t!e end of t!e for loop. 2$e initial value of t!e i is set to 0. 2!e inner loop executes to increment t!e value from 0 to /24 (t!e positive ran e of c!ar) and t!en it rotates to t!e ne ative value of :/2;. 2!e condition in t!e for loop fails and so comes out of t!e for loop. )t prints t!e current value of i t!at is :/2;. main()

{ $nsiHn#d char i= ; for(;iJ= ;i++) ; printf("%d!n",i); } Ans'er: infinite loop ($planation: 2!e difference #etween t!e previous Cuestion and t!is one is t!at t!e c!ar is declared to #e unsi ned. &o t!e i++ can never yield ne ative value and i>=0 never #ecomes false so t!at it can come out of t!e for loop. main() { char i= ; for(;iJ= ;i++) ; printf("%d!n",i); } Ans'er: 'e!avior is implementation dependent. ($planation: 2!e detail if t!e c!ar is si ned%unsi ned #y default is implementation dependent. )f t!e implementation treats t!e c!ar to #e si ned #y default t!e pro ram will print STU/2; and terminate. Vn t!e ot!er !and if it considers c!ar to #e unsi ned #y default, it oes to infinite loop. )ule: Qou can write pro rams t!at !ave implementation dependent #e!avior. 'ut dont write pro rams t!at depend on suc! #e!avior. )s t!e followin statement a declaration%definition. +ind w!at does it meanJ int (*x)[/0]F Ans'er:

,efinition. x is a pointer to array of(siAe /0) inte ers. ?pply cloc-:wise rule to find t!e meanin of t!is definition. B!at is t!e output for t!e pro ram iven #elow t&p#d#f #n$m #rror<&p#{2arninH, #rror, #Ac#ption,}#rror; main() { #rror H1; H1=1; printf("%d",H1); } Ans'er: Compiler error: Wultiple declaration for error ($planation: 2!e name error is used in t!e two meanin s. Vne means t!at it is a enumerator constant wit! value /. 2!e anot!er use is t!at it is a type name (due to typedef) for enum error2ype. "iven a situation t!e compiler cannot distin uis! t!e meanin of error to -now in w!at sense t!e error is used: error /F /=errorF %% w!ic! error it refers in eac! caseJ B!en t!e compiler can distin uis! #etween usa es t!en it will not issue error (in pure tec!nical terms, names can only #e overloaded in different namespaces). Note: t!e extra comma in t!e declaration, enum error2ypeNwarnin , error, exception,O is not an error. ?n extra comma is valid and is provided Dust for pro rammer9s convenience. main() { $nsiHn#d int i=>5 2hi"#(i++R= ); printf("%d",i); } Ans'er: / ;

($planation: 6ote t!e semicolon after t!e w!ile statement. B!en t!e value of i #ecomes 0 it comes out of w!ile loop. ,ue to post:increment on i t!e value of i w!ile printin is /. main() { int i= ; 2hi"#(+(+iKK)R= ) iK=i++; printf("%d",i); } Ans'er: :/ ($planation: *nary + is t!e only dummy operator in C. &o it !as no effect on t!e expression and now t!e w!ile loop is, w!ile(i::P=0) w!ic! is false and so #rea-s out of w!ile loop. 2!e value STU/ is printed due to t!e post:decrement operator. main() { f"oat f=5,H=1 ; #n$m{i=1 ,@=) ,Q=5 }; printf("%d!n",++Q); printf("%f!n",f33)); printf("%"f!n",f%H); printf("%"f!n",fmod(f,H)); } Ans'er: 8ine no 3: 7rror: 8value reCuired 8ine no <: Cannot apply lefts!ift to float 8ine no 4: Cannot apply mod to float ($planation: 7numeration constants cannot #e modified, so you cannot apply ++. 'it:wise operators and L operators cannot #e applied on float values. fmod() is to find t!e modulus values for floats as L operator is for ints. main()

{ int i=1 ; void pasca" f(int,int,int); f(i++,i++,i++); printf(" %d",i); } void pasca" f(int#H#r 5i,int#H#r5@,int#H#r 5Q) { 2rit#(i,@,Q); } Ans'er: Compiler error: un-nown type inte er Compiler error: undeclared function write ($planation: Mascal -eyword doesnSTXt mean t!at pascal code can #e used. )t means t!at t!e function follows Mascal ar ument passin mec!anism in callin t!e functions. void pasca" f(int i,int @,int Q) { printf(STU%d %d %dSTV,i, @, Q); } void cd#c" f(int i,int @,int Q) { printf(STU%d %d %dSTV,i, @, Q); } main() { int i=1 ; f(i++,i++,i++); printf(" %d!n",i); i=1 ; f(i++,i++,i++); printf(" %d",i); } Ans'er: /0 // /2 /( /2 // /0 /( ($planation:

Mascal ar ument passin mec!anism forces t!e ar uments to #e called from left to ri !t. cdecl is t!e normal C ar ument passin mec!anism w!ere t!e ar uments are passed from ri !t to left. void main() { 2hi"#(1){ if(printf("%d",printf("%d"))) %r#aQ; #"s# contin$#; } } Ans'er: "ar#a e values ($planation: 2!e inner printf executes first to print some ar#a e value. 2!e printf returns no of c!aracters printed and t!is value also cannot #e predicted. &till t!e outer printf prints somet!in and so returns a non:Aero value. &o it encounters t!e #rea- statement and comes out of t!e w!ile statement. main() { $nsiHn#d int i=1 ; 2hi"#(iKKJ= ) printf("%$ ",i); } Ans'er: /0 1 ; 4 < 3 . ( 2 / 0 <33(3 <33(.... ($planation: &ince i is an unsi ned inte er it can never #ecome ne ative. &o t!e expression i:: >=0 will always #e true, leadin to an infinite loop. /inc"$d#0conio.h1 main() { int A,&=),9,a;

if(A=&%)) 9=); a=); printf("%d %d ",9,A); } Ans'er: "ar#a e:value 0 ($planation: 2!e value of yL2 is 0. 2!is value is assi ned to x. 2!e condition reduces to if (x) or in ot!er words if(0) and so A oes uninitialiAed. Thumb )ule: C!ec- all control pat!s to write #u free code. main() { int a[1 ]; printf("%d",*a+1K*a+.); } Ans'er: . ($planation: *a and :*a cancels out. 2!e result is as simple as / + ( = . P /d#fin# prod(a,%) a*% main() { int A=.,&==; printf("%d",prod(A+),&K1)); } Ans'er: /0 ($planation:

2!e macro expands and evaluates to as: x+2*y:/ => x+(2*y):/ => /0 void main() { $nsiHn#d Hiv#it=K1; int Hotit; printf("%$ ",++Hiv#it); printf("%$ !n",Hotit=KKHiv#it); } Ans'er: 0 <33(3 void main() { int i; char a[]="! "; if(printf("%s!n",a)) printf("7Q h#r# !n"); #"s# printf("WorH#t it!n"); } Ans'er: V- !ere ($planation: Mrintf will return !ow many c!aracters does it print. $ence printin a null c!aracter returns / w!ic! ma-es t!e if statement true, t!us KV- !ereK is printed. void main() { void *v; int int#H#r=); int *i=,int#H#r; v=i; printf("%d",(int*)*v); } Ans'er: Compiler 7rror. Be cannot apply indirection on type void*.

($planation: Yoid pointer is a eneric pointer type. 6o pointer arit!metic can #e done on it. Yoid pointers are normally used for, /. Massin eneric pointers to functions and returnin suc! pointers. 2. ?s a intermediate pointer type. (. *sed w!en t!e exact pointer type will #e -nown at a later point of time.

void main() { static int i=i++, @=@++, Q=Q++; printf("i = %d @ = %d Q = %d", i, @, Q); } Ans'er: i=/D=/-=/ ($planation: &ince static varia#les are initialiAed to Aero #y default. void main() { int i=i++,@=@++,Q=Q++; printf("%d%d%d",i,@,Q); } Ans'er: "ar#a e values. ($planation: ?n identifier is availa#le to use in pro ram code from t!e point of its declaration. &o expressions suc! as i = i++ are valid statements. 2!e i, D and - are automatic varia#les and so t!ey contain some ar#a e value. "ar#a e in is ar#a e out (")"V). f$nc(a,%) int a,%; { r#t$rn( a= (a==%) ); }

main() { int proc#ss(),f$nc(); printf("<h# va"$# of proc#ss is %d R!n ",proc#ss(f$nc,.,>)); } proc#ss(pf,va"1,va")) int (*pf) (); int va"1,va"); { r#t$rn((*pf) (va"1,va"))); } Ans'er: 2!e value if process is 0 P ($planation: 2!e function 9process9 !as ( parameters : /, a pointer to anot!er function 2 and (, inte ers. B!en t!is function is invo-ed from main, t!e followin su#stitutions for formal parameters ta-e place: func for pf, ( for val/ and < for val2. 2!is function returns t!e result of t!e operation performed #y t!e function 9func9. 2!e function func !as two inte er parameters. 2!e formal parameters are su#stituted as ( for a and < for #. since ( is not eCual to <, a==# returns 0. t!erefore t!e function returns 0 w!ic! in turn is returned #y t!e function 9process9. void main() { static int i=5; if(KKi){ main(); printf("%d ",i); } } Ans'er: 0000 ($planation: 2!e varia#le K)K is declared as static, !ence memory for ) will #e allocated for only once, as it encounters t!e statement. 2!e function main() will #e called recursively unless ) #ecomes eCual to 0, and since main() is recursively called, so t!e value of static ) ie., 0 will #e printed every time t!e control is returned.

void main() { int Q=r#t(si9#of(f"oat)); printf("!n h#r# va"$# is %d",++Q); } int r#t(int r#t) { r#t += ).5; r#t$rn(r#t); } Ans'er: $ere value is 4 ($planation: 2!e int ret(int ret), ie., t!e function name and t!e ar ument name can #e t!e same. +irstly, t!e function ret() is called in w!ic! t!e siAeof(float) ie., . is passed, after t!e first expression t!e value in ret will #e <, as ret is inte er !ence t!e value stored in ret will !ave implicit type conversion from float to int. 2!e ret is returned in main() it is printed after and preincrement. void main() { char a[]="1).=5! "; int i=str"#n(a); printf("h#r# in . %d!n",++i); } Ans'er: !ere in ( < ($planation: 2!e c!ar array 9a9 will !old t!e initialiAed strin , w!ose len t! will #e counted from 0 till t!e nullc!aracter. $ence t!e 9)9 will !old t!e value eCual to 3, after t!e pre:increment in t!e printf statement, t!e < will #e printed. main(){ int * @; void f$n(int **); f$n(,@); }

void f$n(int **Q) { int a = ; ;* add a stmt h#r#*; } Ans'er: *- = Ga ($planation: 2!e ar ument of t!e function is a pointer to a pointer. B!at are t!e followin notations of definin functions -nown asJ i. int a#c(int a,float #) N %* some code *% O ii. int a#c(a,#) int aF float #F N %* some code*% O Ans'er: i. ?6&) C notation ii. Herni !an G 5itc!e notation (HG5 6otation) main() { char *p; p="%d!n"; p++; p++; printf(pK),. } Ans'er: (00 ($planation: 2!e pointer points to L since it is incremented twice and a ain decremented #y 2, it points to 9Ld@n9 and (00 is printed. --. main(){ -?. char a[1 ];

);

? . a[ ]=LaL;a[1]]=L%L;a[)]=LcL;a[=]=LdL; ?1. a%c(a); ?). } ?.. a%c(char a[]){ ?=. a++; ?5. printf("%c",*a); ?>. a++; ?*. printf("%c",*a); ?-. } ($planation: 2!e #ase address is modified only in function and as a result a points to 9#9 t!en after incrementin to 9c9 so #c will #e printed. main() { int i; i = a%c(); printf("%d",i); } a%c() { XFY = 1 ; } Ans'er: /000 ($planation: Z #loc-Cuote>6ormally t!e return value from t!e function is t!rou ! t!e information from t!e accumulator. $ere [?$ is t!e pseudo lo#al varia#le denotin t!e accumulator. $ence, t!e value of t!e accumulator is set /000 so t!e function returns value /000. int i; main(){ int t; for ( t==;scanf("%d",,i)Kt;printf("%d!n",i)) printf("%dKK",tKK); } ;; 'f th# inp$ts ar# ,1,),. find th# o;p Ans'er:

.::0 (::/ 2::2 ($planation: 8et us assume some x= scanf(KLdK,Gi):t t!e values durin execution will #e, t i x : . : 2 0

main(){ int a= ;int % = ) ;char A =1;char & =1 ; if(a,%,A,&) printf("h#""o"); } Ans'er: !ello ($planation: 2!e comma operator !as associativity from left to ri !t. Vnly t!e ri !tmost value is returned and t!e ot!er values are evaluated and i nored. 2!us t!e value of last varia#le y is returned to c!ec- in if. &ince it is a non Aero value if #ecomes true so, K!elloK will #e printed. main(){ $nsiHn#d int i; for(i=1;iJK);iKK) printf("c aptit$d#");

} ($planation: i is an unsi ned inte er. )t is compared wit! a si ned value. &ince t!e #ot! types doesn9t matc!, si ned is promoted to unsi ned value. 2!e unsi ned eCuivalent of :2 is a !u e value so condition #ecomes false and control comes out of t!e loop. / inc"$d# 0stdio.h1 int on#Xd[]={1,),.}; main() { int *ptr; ptr=on#Xd; ptr+=.; printf("%d",*ptr); } Ans'er: ar#a e value ($planation: ptr pointer is pointin to out of t!e array ran e of one[d. / inc"$d#0stdio.h1 aaa() { printf("hi"); } %%%(){ printf("h#""o"); } ccc(){ printf("%&#"); } main() { int (*ptr[.])(); ptr[ ]=aaa; ptr[1]=%%%; ptr[)]=ccc; ptr[)](); } Ans'er:

#ye ($planation: ptr is array of pointers to functions of return type int.ptr[0] is assi ned to address of t!e function aaa. &imilarly ptr[/] and ptr[2] for ### and ccc respectively. ptr[2]() is in effect of writin ccc(), since ptr[2] points to ccc. /inc"$d#0stdio.h1 main() { W'Z[ *ptr; char i; ptr=fop#n("999.c","r"); 2hi"#((i=fH#tch(ptr))R=[7W) printf("%c",i); } Ans'er: contents of AAA.c followed #y an infinite loop ($planation: 2!e condition is c!ec-ed a ainst 7V+, it s!ould #e c!ec-ed a ainst 6*88. main() { int i = ;@= ; if(i ,, @++) printf("%d..%d",i++,@); printf("%d..%d,i,@); } Ans'er: 0..0 ($planation: 2!e value of i is 0. &ince t!is information is enou ! to determine t!e trut! value of t!e #oolean expression. &o t!estatement followin t!e if statement is not executed. 2!e values of i and D remain unc!an ed and et printed. str$ct point {

int A; int &; }; str$ct point oriHin,*pp; main() { pp=,oriHin; printf("oriHin is(%d%d)!n",(*pp).A,(*pp).&); printf("oriHin is (%d%d)!n",ppKJA,ppKJ&); } Ans'er: ori in is(0,0) ori in is(0,0) ($planation: pp is a pointer to structure. we can access t!e elements of t!e structure eit!er wit! arrow mar- or wit! indirectionoperator. Note: &ince structure point is lo#ally declared x G y are initialiAed as Aeroes main() { int i=X"Xa%c(1 ); printf("%d!n",KKi); } int X"Xa%c(int i) { r#t$rn(i++); } Ans'er: 1 ($planation: return(i++) it will first return i and t!en increments. i.e. /0 will #e returned. main() { char *p;

int *N; "onH *r; p=N=r= ; p++; N++; r++; printf("%p...%p...%p",p,N,r); } Ans'er: 000/...0002...000. ($planation: ++ operator w!en applied to pointers increments address accordin to t!eir correspondin data:types. main() { char c=L L,A,conv#rt(9); H#tc(c); if((cJ=LaL) ,, (c3=L9L)) A=conv#rt(c); printf("%c",A); } conv#rt(9) { r#t$rn 9K.); } Ans'er: Compiler error ($planation: declaration of convert and format of etc() are wron . main(int arHc, char **arHv) { printf("#nt#r th# charact#r"); H#tchar(); s$m(arHv[1],arHv[)]); } s$m(n$m1,n$m))

int n$m1,n$m); { r#t$rn n$m1+n$m); } Ans'er: Compiler error. ($planation: ar v[/] G ar v[2] are strin s. 2!ey are passed to t!e function sum wit!out convertin it to inte er values. /inc"$d#0stdio.h1 main() { const int i==; f"oat @; @ = ++i; printf("%d %f", i,++@); } Ans'er: Compiler error ($planation: i is a constant. you cannot c!an e t!e value of constant /inc"$d#0stdio.h1 main() { int a[)][)][)] = { {1 ,),.,=}, {5,>,*,-} int *p,*N; p=,a[)][)][)]; *N=***a; printf("%d..%d",*p,*N); } Ans'er: ar#a evalue../ ($planation:

};

p=Ga[2][2][2] you declare only two 2, arrays. #ut you are tryin to access t!e t!ird 2,(w!ic! you are not declared) it will print ar#a e values. *C=***a startin address of a is assi ned inte er pointer. now C is pointin to startin address of a.if you print *C me?nswer:it will print first element of (, array. /inc"$d#0stdio.h1 main() { r#Hist#r i=5; char @[]= "h#""o"; printf("%s %d",@,i); } Ans'er: !ello 3 ($planation: if you declare i as re ister compiler will treat it as ordinary inte er and it will ta-e inte er value. i value may #e stored eit!er in re ister or in memory. main() { int i=5,@=>,9; printf("%d",i+++@); } Ans'er: // ($planation: t!e expression i+++D is treated as (i++ + D) str$ct aaa{ str$ct aaa *pr#v; int i; str$ct aaa *n#At; }; main() { str$ct aaa a%c,d#f,Hhi,@Q"; int A=1 ; a%c.i= ;a%c.pr#v=,@Q";

a%c.n#At=,d#f; d#f.i=1;d#f.pr#v=,a%c;d#f.n#At=,Hhi; Hhi.i=);Hhi.pr#v=,d#f; Hhi.n#At=,@Q"; @Q".i=.;@Q".pr#v=,Hhi;@Q".n#At=,a%c; A=a%c.n#AtKJn#AtKJpr#vKJn#AtKJi; printf("%d",A); } Ans'er: 2 ($planation: a#ove all statements form a dou#le circular lin-ed listF a#c.next:>next:>prev:>next:>i t!is one points to K !iK node t!e value of at particular node is 2.

Ans'er: 2000 is a leap year ($planation: ?n ordinary pro ram to c!ec- if leap year or not. /d#fin# maA 5 /d#fin# int arr1[maA] main() { t&p#d#f char arr)[maA]; arr1 "ist={ ,1,),.,=}; arr) nam#="nam#"; printf("%d %s","ist[ ],nam#); } Ans'er: Compiler error (in t!e line arr/ list = N0,/,2,(,.O) ($planation:

arr2 is declared of type array of siAe 3 of c!aracters. &o it can #e used to declare t!e varia#le name of t!e type arr2. 'ut it is not t!e case of arr/. $ence an error. )ule o* Thumb: \defines are used for textual replacement w!ereas typedefs are used for declarin new types. int i=1 ; main() { #At#rn int i; { int i=) ; { const vo"ati"# $nsiHn#d i=. ; printf("%d",i); } printf("%d",i); } printf("%d",i); } Ans'er: (0,20,/0 ($planation: 9N9 introduces new #loc- and t!us new scope. )n t!e innermost #loc- i is declared as, const volatile unsi ned w!ic! is a valid declaration. i is assumed of type int. &o printf prints (0. )n t!e next #loc-, i !as value 20 and so printfprints 20. )n t!e outermost #loc-, i is declared as extern, so no stora e space is allocated for it. ?fter compilation is over t!e lin-er resolves it to lo#al varia#le i (since it is t!e only varia#le visi#le t!ere). &o it prints i9s value as /0. main() { int *@; { int i=1 ; @=,i; } printf("%d",*@); }

Ans'er: /0 ($planation: 2!e varia#le i is a #loc- level varia#le and t!e visi#ility is inside t!at #loc- only. 'ut t!e lifetime of i is lifetime of t!e function so it lives upto t!e exit of main function. &ince t!e i is still allocated space, *D prints t!e value stored in i since D points i. main() { int i=K1; Ki; printf("i = %d, Ki = %d !n",i,Ki); } Ans'er: i = :/, :i = / ($planation: :i is executed and t!is execution doesn9t affect t!e value of i. )n printf first you Dust print t!e value of i. ?fter t!at t!evalue of t!e expression :i = :(:/) is printed. main() { char *cptr,c; void *vptr,v; c=1 ; v= ; cptr=,c; vptr=,v; printf("%c%v",c,v); } Ans'er: Compiler error (at line num#er .): siAe of v is *n-nown. ($planation: Qou can create a varia#le of type void * #ut not of type void, since void is an empty type. )n t!e second line you are creatin varia#le vptr of type void * and v of type void !ence an error. main()

{ char *str1="a%cd"; char str)[]="a%cd"; printf("%d %d %d",si9#of(str1),si9#of(str)),si9#of("a%cd")); } Ans'er: 233 ($planation: )n first siAeof, str/ is a c!aracter pointer so it ives you t!e siAe of t!e pointer varia#le. )n second siAeof t!e name str2 indicates t!e name of t!e array w!ose siAe is 3 (includin t!e 9@09 termination c!aracter). 2!e t!ird siAeof is similar to t!e second one. main() { char not; not=R); printf("%d",not); } Ans'er: 0 ($planation: P is a lo ical operator. )n C t!e value 0 is considered to #e t!e #oolean value +?8&7, and any non:Aero value is considered to #e t!e #oolean value 25*7. $ere 2 is a non:Aero value so 25*7. P25*7 is +?8&7 (0) so it prints 0. /d#fin# WFZM[ K1 /d#fin# <\([ 1 /d#fin# G(ZZ main() { if(G(ZZ) p$ts("G(ZZ"); #"s# if(WFZM[) p$ts("<\(["); #"s# p$ts("WFZM["); } Ans'er:

25*7 ($planation: 2!e input pro ram to t!e compiler after processin #y t!e preprocessor is, main(){ if( ) p$ts("G(ZZ"); #"s# if(K1) p$ts("<\(["); #"s# p$ts("WFZM["); } Mreprocessor doesn9t replace t!e values iven inside t!e dou#le Cuotes. 2!e c!ec- #y if condition is #oolean value false so it oes to else. )n second if :/ is #oolean value true !ence K25*7K is printed. main() { int Q=1; printf("%d==1 is ""%s",Q,Q==14"<\(["5"WFZM["); } Ans'er: /==/ is 25*7 ($planation: B!en two strin s are placed to et!er (or separated #y w!ite:space) t!ey are concatenated (t!is is called as Kstrin iAationK operation). &o t!e strin is as if it is iven as KLd==/ is LsK. 2!e conditional operator( J: ) evaluates to K25*7K. int i,@; for(i= ;i3=1 ;i++) { @+=5; ass#rt(i35); } Ans'er: 5untime error: ?#normal pro ram termination. assert failed (iZ3), ]file name^,]line num#er^

($planation: asserts are used durin de#u in to ma-e sure t!at certain conditions are satisfied. )f assertion fails, t!e pro ram will terminate reportin t!e same. ?fter de#u in use,

\undef 6,7'*" and t!is will disa#le all t!e assertions from t!e source code. ?ssertion is a ood de#u tool to ma-e use of. main() { int i=K1; +i; printf("i = %d, +i = %d !n",i,+i); } Ans'er: i = :/, +i = :/ ($planation: *nary + is t!e only dummy operator in C. B!ere:ever it comes you can Dust i nore it Dust #ecause it !as no effect in t!e expressions (!ence t!e name dummy operator). B!at are t!e files w!ic! are automatically opened w!en a C file is executedJ Ans'er: stdin, stdout, stderr (standard input,standard output,standard error). w!at will #e t!e position of t!e file mar-erJ a: fsee-(ptr,0,&77H[&72)F #: fsee-(ptr,0,&77H[C*5)F Ans'er : a: 2!e &77H[&72 sets t!e file position mar-er to t!e startin of t!e file. #: 2!e &77H[C*5 sets t!e file position mar-er to t!e current position of t!e file. main() { char nam#[1 ],s[1)]; scanf(" !"%[B!"]!"",s); } $ow scanf will executeJ in

Ans'er: +irst it c!ec-s for t!e leadin w!ite space and discards it.2!en it matc!es wit! a Cuotation mar- and t!en it reads all c!aracter upto anot!er Cuotation mar-. B!at is t!e pro#lem wit! t!e followin code se mentJ w!ile ((f ets(receivin array,30,file[ptr)) P= 7V+)F Ans'er + ($planation: f ets returns a pointer. &o t!e correct end of file c!ec- is c!ec-in for P= 6*88. main() { main(); } Ans'er: 5untime error : &tac- overflow. ($planation: main function calls itself a ain and a ain. 7ac! time t!e function is called its return address is stored in t!e call stac-. &ince t!ere is no condition to terminate t!e function call, t!e call stac- overflows at runtime. &o it terminates t!e pro ram and results in an error. main( ) { char *N; int @; for (@= ; @3.; @++) scanf("%s" ,(N+@)); for (@= ; @3.; @++) printf("%c" ,*(N+@)); for (@= ; @3.; @++) printf("%s" ,(N+@)); } ($planation: $ere we !ave only one pointer to type c!ar and since we ta-e input in t!e same pointer t!us we -eep writin over in t!e same location, eac! time s!iftin t!e pointer value #y /. &uppose t!e inputs are WV*&7, 25?CH and Y)52*?8. 2!en for t!e first input suppose t!e pointer starts at location /00 t!en t!e input one is stored as W V * & 7 @ 0

B!en t!e second input is iven t!e pointer is incremented as D value #ecomes /, so t!e input is filled in memory startin from /0/. W 2 5 ? C H @0

2!e t!ird input starts fillin from t!e location /02 W 2 Y ) 5 2 * ? 8 @0

2!is is t!e final value stored . 2!e first printf prints t!e values at t!e position C, C+/ and C+2 = W 2 Y 2!e second printf prints t!ree strin s startin from locations C, C+/, C+2 i.e W2Y)52*?8, 2Y)52*?8 and Y)52*?8. main( ) { void *vp; char ch = LHL, *cp = "Hoof&"; int @ = ) ; vp = ,ch; printf("%c", *(char *)vp); vp = ,@; printf("%d",*(int *)vp); vp = cp; printf("%s",(char *)vp + .); } Ans'er: 20fy ($planation: &ince a void pointer is used it can #e type casted to any ot!er type pointer. vp = Gc! stores address of c!ar c! and t!e next statement prints t!e value stored in vp after type castin it to t!e proper data type pointer. t!e output is 9 9. &imilarly t!e output from second printf is 9209. 2!e t!ird printf statement type casts it to print t!e strin from t!e .t! value !ence t!e output is 9fy9. main ( ) { static char *s[ ] = {"%"acQ", "2hit#", "&#""o2", "vio"#t"}; char **ptr[ ] = {s+., s+), s+1, s}, ***p;

p = ptr; **++p; printf("%s",*KK*++p + .); } Ans'er: c($planation: )n t!is pro#lem we !ave an array of c!ar pointers pointin to start of . strin s. 2!en we !ave ptr w!ic! is a pointer to a pointer of type c!ar and a varia#le p w!ic! is a pointer to a pointer to a pointer of type c!ar. p !old t!e initial value of ptr, i.e. p = s+(. 2!e next statement increment value in p #y / , t!us now value of p = s+2. )n t!e printf statement t!e expression is evaluated *++p causes ets value s+/ t!en t!e pre decrement is executed and we et s+/:/ = s . t!e indirection operator now ets t!e value from t!e array of s and adds ( to t!e startin address. 2!e strin is printed startin from t!is position. 2!us, t!e output is 9c-9. main() { int i, n; char *A = "Hir""; n = str"#n(A); *A = A[n]; for(i= ; i0n; ++i) { printf("%s!n",A); A++; } } Ans'er: (#lan- space) irl rl l ($planation: $ere a strin (a pointer to c!ar) is initialiAed wit! a value K irlK. 2!e strlen function returns t!e len t! of t!e strin , t!us n !as a value .. 2!e next statement assi ns value at t!e nt! location (9@09) to t!e first location. 6ow t!e strin #ecomes K@0irlK . 6ow t!e printf statement

prints t!e strin after eac! iteration it increments it startin position. 8oop starts from 0 to .. 2!e first time x[0] = 9@09 !ence it prints not!in and pointer value is incremented. 2!e second time it prints from x[/] i.e KirlK and t!e t!ird time it prints KrlK and t!e last time it prints KlK and t!e loop terminates. main( ) { int a[)][.][)] = {{{),=},{*,-},{.,=}},{{),)},{),.},{.,=}}}; printf("%$ %$ %$ %d !n",a,*a,**a,***a); printf("%$ %$ %$ %d !n",a+1,*a+1,**a+1,***a+1); } Ans'er: /00, /00, /00, 2 //., /0., /02, ( ($planation: 2!e iven array is a (:, one. )t can also #e viewed as a /:, array. 2 /00 . /02 4 /0. ; /0< ( /0; . //0 2 //2 2 //. 2 //< ( //; ( /20 . /22

t!us, for t!e first printf statement a, *a, **a ive address of first element . since t!e indirection ***a ives t!e value. $ence, t!e first line of t!e output. for t!e second printf a+/ increases in t!e t!ird dimension t!us points to value at //., *a+/ increments in second dimension t!us points to /0., **a +/ increments t!e first dimension t!us points to /02 and ***a+/ first ets t!e value at first location and t!en increments it #y /. $ence, t!e output. main( ) { int a[ ] = {1 ,) ,. ,= ,5 },@,*p; for(@= ; @35; @++) { printf("%d" ,*a); a++; } p = a; for(@= ; @35; @++) { printf("%d " ,*p); p++;

} } Ans'er: Compiler error: lvalue reCuired. ($planation: 7rror is in line wit! statement a++. 2!e operand must #e an lvalue and may #e of any of scalar type for t!e anyoperator, array name only w!en su#scripted is an lvalue. &imply array name is a non:modifia#le lvalue. main( ) { static int a[ ] = { ,1,),.,=}; int *p[ ] = {a,a+1,a+),a+.,a+=}; int **ptr = p; ptr++; printf("!n %d %d %d", ptrKp, *ptrKa, *ptr++; printf("!n %d %d %d", ptrKp, *ptrKa, *++ptr; printf("!n %d %d %d", ptrKp, *ptrKa, ++*ptr; printf("!n %d %d %d", ptrKp, } Ans'er: /// 222 ((( (.. ($planation: 8et us consider t!e array and t!e two pointers wit! some address a 0 /00 /00 /000 / /02 /02 /002 2 /0. p /0. /00. ( /0< /0< /00< . /0; /0; /00;

**ptr); **ptr); **ptr); *ptrKa, **ptr);

ptr /000 2000 ?fter execution of t!e instruction ptr++ value in ptr #ecomes /002, if scalin factor for inte er is 2 #ytes. 6ow ptr : p is value in ptr : startin location of array p, (/002 : /000) % (scalin factor) = /, *ptr : a = value at address pointed #y ptr : startin value of array a, /002 !as a value /02 so t!e value is (/02 : /00)%(scalin factor) = /, **ptr is t!e value stored in t!e location pointed #y t!e pointer of ptr = value pointed #y value pointed #y /002 = value pointed #y /02 = /. $ence t!e output of t!e firs printf is /, /, /. ?fter execution of *ptr++ increments value of t!e value in ptr #y scalin factor, so it #ecomes/00.. $ence, t!e outputs for t!e second printf are ptr : p = 2, *ptr : a = 2, **ptr = 2. ?fter execution of *++ptr increments value of t!e value in ptr #y scalin factor, so it #ecomes/00.. $ence, t!e outputs for t!e t!ird printf are ptr : p = (, *ptr : a = (, **ptr = (. ?fter execution of ++*ptr value in ptr remains t!e same, t!e value pointed #y t!e value is incremented #y t!e scalin factor. &o t!e value in array p at location /00< c!an es from /0< /0 /0;,. $ence, t!e outputs for t!e fourt! printf are ptr : p = /00< : /000 = (, *ptr : a = /0; : /00 = ., **ptr = .. /inc"$d#0stdio.h1 main() { str$ct AA { int A; str$ct && { char s; str$ct AA *p; }; str$ct && *N; }; } Ans'er: Compiler 7rror ($planation:

in t!e end of nested structure yy a mem#er !ave to #e declared. main() { #At#rn int i; i=) ; printf("%d",si9#of(i)); } Ans'er: 8in-er error: undefined sym#ol 9[i9. ($planation: extern declaration specifies t!at t!e varia#le i is defined somew!ere else. 2!e compiler passes t!e external varia#le to #e resolved #y t!e lin-er. &o compiler doesn9t find an error. ,urin lin-in t!e lin-er searc!es for t!e definition of i. &ince it is not found t!e lin-er fla s an error. main() { printf("%d", o$t); } int o$t=1 ; Ans'er: Compiler error: undefined sym#ol out in function main. ($planation: 2!e rule is t!at a varia#le is availa#le for use from t!e point of declaration. 7ven t!ou ! a is a lo#al varia#le, it is not availa#le for main. $ence an error. main() { #At#rn o$t; printf("%d", o$t); } int o$t=1 ; Ans'er: /00

($planation: 2!is is t!e correct way of writin t!e previous pro ram. main() { sho2(); } void sho2() { printf("'Lm th# Hr#at#st"); } Ans'er: Compier error: 2ype mismatc! in redeclaration of s!ow. ($planation: B!en t!e compiler sees t!e function s!ow it doesn9t -now anyt!in a#out it. &o t!e default return type (ie, int) is assumed. 'ut w!en compiler sees t!e actual definition of s!ow mismatc! occurs since it is declared as void. $ence t!e error. 2!e solutions are as follows: /. declare void s!ow() in main() . 2. define s!ow() #efore main(). (. declare extern void s!ow() #efore t!e use of s!ow(). main() { int i; printf("%d",scanf("%d",,i)); } Ans'er: / ($planation: &canf returns num#er of items successfully read and not /%0. $ere /0 is iven as input w!ic! s!ould !ave #een scanned successfully. &o num#er of items read is /. /d#fin# f(H,H)) H//H) main() {

;; va"$# 1

is Hiv#n as inp$t h#r#

int var1)=1 ; printf("%d",f(var,1))); } Ans'er: /00 main() { int i= ; for(;i++;printf("%d",i)) ; printf("%d",i); } Ans'er: / ($planation: #efore enterin into t!e for loop t!e c!ec-in condition is KevaluatedK. $ere it evaluates to 0 (false) and comes out of t!e loop, and i is incremented (note t!e semicolon after t!e for loop). /inc"$d#0stdio.h1 main() { char s[]={LaL,L%L,LcL,L!nL,LcL,L! L}; char *p,*str,*str1; p=,s[.]; str=p; str1=s; printf("%d",++*p + ++*str1K.)); } Ans'er: W ($planation: p is pointin to c!aracter 9@n9.str/ is pointin to c!aracter 9a9 ++*p me?nswer:Kp is pointin to 9@n9 and t!at is incremented #y one.K t!e ?&C)) value of 9@n9 is /0. t!en it is incremented to //. t!e value of ++*p is //. ++*str/ me?nswer:Kstr/ is pointin to 9a9 t!at is

incremented #y / and it #ecomes 9#9. ?&C)) value of 9#9 is 1;. #ot! // and 1; is added and result is su#tracted from (2K. i.e. (//+1;:(2)=44(KWK)F /inc"$d#0stdio.h1 main() { str$ct AA { int A=.; char nam#[]="h#""o"; }; str$ct AA *s=ma""oc(si9#of(str$ct AA)); printf("%d",sKJA); printf("%s",sKJnam#); } Ans'er: Compiler 7rror ($planation: )nitialiAation s!ould not #e done for structure mem#ers inside t!e structure declaration main() { int i=1; 2hi"# (i3=5) { printf("%d",i); if (iJ)) Hoto h#r#; i++; } } f$n() { h#r#5 printf("++"); } Ans'er: Compiler error: *ndefined la#el 9!ere9 in function main ($planation:

8a#els !ave functions scope, in ot!er words 2!e scope of t!e la#els is limited to functions . 2!e la#el 9!ere9 is availa#le in function fun() $ence it is not visi#le in function main. main() { static char nam#s[5][) ]={"pasca"","ada","co%o"","fortran","p#r""}; int i; char *t; t=nam#s[.]; nam#s[.]=nam#s[=]; nam#s[=]=t; for (i= ;i3==;i++) printf("%s",nam#s[i]); } Ans'er: Compiler error: 8value reCuired in function main ($planation: ?rray names are pointer constants. &o it cannot #e modified. void main() { int i=5; printf("%d",i++ + ++i); } Ans'er: Vutput Cannot #e predicted exactly. ($planation: &ide effects are involved in t!e evaluation of i void main() { int i=5; printf("%d",i+++++i); } Ans'er: Compiler 7rror

($planation: 2!e expression i+++++i is parsed as i ++ ++ + i w!ic! is an ille al com#ination of operators. /inc"$d#0stdio.h1 main() { int i=1,@=); s2itch(i) { cas# 15 printf("6778"); %r#aQ; cas# @5 printf("]F8"); %r#aQ; } } Ans'er: Compiler 7rror: Constant expression reCuired in function main. ($planation: 2!e case statement can !ave only constant expressions (t!is implies t!at we cannot use varia#le names directly so an error). Note: 7numerated types can #e used in case statements. main() { c"rscr(); } c"rscr();

Answer6o output%error
($planation: 2!e first clrscr() occurs inside a function. &o it #ecomes a function call. )n t!e second clrscr()F is a functiondeclaration (#ecause it is not inside any function). #n$m co"ors {]ZFE^,]Z([,6\[[G} main() {

printf("%d..%d..%d",]ZFE^,]Z([,6\[[G); r#t$rn(1); } Ans'er: 0../..2 ($planation: enum assi ns num#ers startin from 0, if not explicitly defined. void main() { char far *farth#r,*farth#st; printf("%d..%d",si9#of(farth#r),si9#of(farth#st)); } Ans'er: ...2 ($planation: t!e second pointer is of c!ar type and not a far pointer main() { int i== ,@=. ; printf("%d..%d"); } Ans'er: .00..(00 ($planation: printf ta-es t!e values of t!e first two assi nments of t!e pro ram. ?ny num#er of printf9s may #e iven. ?ll of t!em ta-e only t!e first two values. )f more num#er of assi nments iven in t!e pro ram,t!en printf will ta-e ar#a e values. main() {

char *p; p="O#""o"; printf("%c!n",*,*p); } Ans'er: $ ($planation: * is a dereference operator G is a reference operator. 2!ey can #e applied any num#er of times provided it is meanin ful. $ere p points to t!e first c!aracter in t!e strin K$elloK. *p dereferences it and so its value is $. ? ain G references it to an address and * dereferences it to t!e value $. main() { char *p="hai fri#nds",*p1; p1=p; 2hi"#(*pR=L! L) ++*p++; printf("%s %s",p,p1); } Ans'er: i#DP sDfoet ($planation: ++*p++ will #e parse in t!e iven order *p t!at is value at t!e location currently pointed #y p will #e ta-en ++*p t!e retrieved value will #e incremented w!en F is encountered t!e location will #e incremented t!at is p++ will #e executed

$ence, in t!e w!ile loop initial value pointed #y p is 9!9, w!ic! is c!an ed to 9i9 #y executin ++*p and pointer moves to point, 9a9 w!ic! is similarly c!an ed to 9#9 and so on. &imilarly #lan- space is converted to 9P9. 2!us, we o#tain value in p #ecomes ST_i#DP sDfoetST` and since p reac!es 9@09 and p/ points to p t!us p/doesnot print anyt!in . /inc"$d#0stdio.h1 /d#fin# a 1 main()

{ /d#fin# a 5 printf("%d",a); }

Answer: 5

Explanation: <h# pr#proc#ssor dir#ctiv#s can %# r#d#fin#d an&2h#r# in th# proHram. Mo th# most r#c#nt"& assiHn#d va"$# 2i"" %# taQ#n. /d#fin# c"rscr() 1 main() { c"rscr(); printf("%d!n",c"rscr()); }

Answer: 1

Explanation: +r#proc#ssor #A#c$t#s as a s#p#rat# pass %#for# th# #A#c$tion of th# compi"#r. Mo t#At$a" r#p"ac#m#nt of c"rscr() to 1 occ$rs.<h# inp$t to compi"#r "ooQs "iQ# this 5 main() { 1 ; printf("%d!n",1 } );

proHram

Note:

1 ; is an #A#c$ta%"# stat#m#nt %$t 2ith no action. Mo it do#snLt Hiv# an& pro%"#m main() { printf("%p",main); }

Answer: Mom# addr#ss 2i"" %# print#d.

Explanation: W$nction nam#s ar# @$st addr#ss#s (@$st "iQ# arra& nam#s ar# addr#ss#s). main() is a"so a f$nction. Mo th# addr#ss of f$nction main 2i"" %# print#d. %p in printf sp#cifi#s that th# arH$m#nt is an addr#ss. <h#& ar# print#d as h#Aad#cima" n$m%#rs. /inc"$d#0stdio.h1 main() { str$ct AA { int A=.; char nam#[]="h#""o"; }; str$ct AA *s; printf("%d",sKJA); printf("%s",sKJnam#); } Ans'er: Compiler 7rror ($planation: Qou s!ould not initialiAe varia#les in declaration /inc"$d#0stdio.h1 main() { str$ct AA

{ int A; str$ct && { char s; str$ct AA *p; }; str$ct && *N; }; } Ans'er: Compiler 7rror ($planation: 2!e structure yy is nested wit!in structure xx. $ence, t!e elements are of yy are to #e accessed t!rou ! t!e instance of structure xx, w!ic! needs an instance of yy to #e -nown. )f t!e instance is created after definin t!e structure t!e compiler will not -now a#out t!e instance relative to xx. $ence for nested structure yy you !ave todeclare mem#er. main() { printf("!na%"); printf("!%si"); printf("!rha"); } Ans'er: !ai ($planation: @n : newline @# : #ac-space @r : linefeed main() { int i=5; printf("%d%d%d%d%d%d",i++,iKK,++i,KKi,i); } Ans'er: .33.3

($planation: 2!e ar uments in a function call are pus!ed into t!e stac- from left to ri !t. 2!e evaluation is #y poppin out from t!e stac-. and t!e evaluation is from ri !t to left, !ence t!e result. /d#fin# sN$ar#(A) A*A main() { int i; i = >=;sN$ar#(=); printf("%d",i); } Ans'er: <. ($planation: t!e macro call sCuare(.) will su#stituted #y .*. so t!e expression #ecomes i = <.%.*. . &ince % and * !as eCual priority t!e expression will #e evaluated as (<.%.)*. i.e. /<*. = <. main() { int c=K K); printf("c=%d",c); } Ans'er: c=2F ($planation: $ere unary minus (or ne ation) operator is used twice. &ame mat!s rules applies, ie. minus * minus= plus. Note: $owever you cannot ive li-e ::2. 'ecause :: operator can only #e applied to varia#les as a decrement operator (e ., i::). 2 is a constant and not a varia#le. main() { int i=1 ; i=RiJ1=; printf("i=%d",i); } Ans'er: i=0

($planation: )n t!e expression Pi>/. , 6V2 (P) operator !as more precedence t!an STa >STX sym#ol. P is a unary lo ical operator. Pi (P/0) is 0 (not of true is false). 0>/. is false (Aero). /inc"$d#0stdio.h1 main() { char s[]={LaL,L%L,LcL,L!nL,LcL,L! L}; char *p,*str,*str1; p=,s[.]; str=p; str1=s; printf("%d",++*p + ++*str1K.)); } Ans'er: 44 ($planation: p is pointin to c!aracter 9@n9. str/ is pointin to c!aracter 9a9 ++*p. Kp is pointin to 9@n9 and t!at is incremented #y one.K t!e ?&C)) value of 9@n9 is /0, w!ic! is t!en incremented to //. 2!e value of ++*p is //. ++*str/, str/ is pointin to 9a9 t!at is incremented #y / and it #ecomes 9#9. ?&C)) value of 9#9 is 1;. 6ow performin (// + 1; : (2), we et 44(KWK)F &o we et t!e output 44 :: KWK (?scii is 44). /inc"$d#0stdio.h1 main() { int a[)][)][)] = { {1 ,),.,=}, {5,>,*,-} int *p,*N; p=,a[)][)][)]; *N=***a; printf("%dKKKK%d",*p,*N); } Ans'er: &ome"ar#a eYalue:::/ ($planation:

};

p=Ga[2][2][2] you declare only two 2, arrays, #ut you are tryin to access t!e t!ird 2,(w!ic! you are not declared) it will print ar#a e values. *C=***a startin address of a is assi ned inte er pointer. 6ow C is pointin to startin address of a. )f you print *C, it will print first element of (, array. main() { char *p; printf("%d %d ",si9#of(*p),si9#of(p)); } Ans'er: /2 ($planation: 2!e siAeof() operator ives t!e num#er of #ytes ta-en #y its operand. M is a c!aracter pointer, w!ic! needs one #yte for storin its value (a c!aracter). $ence siAeof(*p) ives a value of /. &ince it needs two #ytes to store t!e address of t!e c!aracter pointer siAeof(p) ives 2. main() { int i=.; s2itch(i) { d#fa$"t5printf("9#ro"); cas# 15 printf("on#"); %r#aQ; cas# )5printf("t2o"); %r#aQ; cas# .5 printf("thr##"); %r#aQ; } } Ans'er : t!ree ($planation : 2!e default case can #e placed anyw!ere inside t!e loop. )t is executed only w!en all ot!er cases doesn9t matc!.

main() { printf("%A",K133=); } Ans'er: fff0 ($planation : :/ is internally represented as all /9s. B!en left s!ifted four times t!e least si nificant . #its are filled wit! 09s.2!e Lx format specifier specifies t!at t!e inte er value #e printed as a !exadecimal value. main() { char strinH[]="O#""o _or"d"; disp"a&(strinH); } void disp"a&(char *strinH) { printf("%s",strinH); } Ans'er: Compiler 7rror : 2ype mismatc! in redeclaration of function display ($planation : )n t!ird line, w!en t!e function display is encountered, t!e compiler doesn9t -now anyt!in a#out t!e function display. )t assumes t!e ar uments and return types to #e inte ers, (w!ic! is t!e default type). B!en it sees t!e actual function display, t!e ar uments and type contradicts wit! w!at it !as assumed previously. $ence a compile time error occurs. main() { static int var = 5; printf("%d ",varKK); if(var) main(); } Ans'er:

3.(2/ ($planation: B!en static stora e class is iven, it is initialiAed once. 2!e c!an e in t!e value of a static varia#le is retained even #etween t!e function calls. Wain is also treated li-e any ot!er ordinary function, w!ic! can #e called recursively. main() { int c[ ]={).-,..=,=,>.*,5}; int @,*p=c,*N=c; for(@= ;@35;@++) { printf(" %d ",*c); ++N; } for(@= ;@35;@++){ printf(" %d ",*p); ++p; } } Ans'er: 222222(.<3 ($planation: )nitially pointer c is assi ned to #ot! p and C. )n t!e first loop, since only C is incremented and not c , t!e value 2 will #e printed 3 times. )n second loop p itself is incremented. &o t!e values 2 ( . < 3 will #e printed. main() { #At#rn int i; i=) ; printf("%d",i); } Ans'er: 8in-er 7rror : *ndefined sym#ol 9[i9 ($planation: extern stora e class in t!e followin declaration, extern int iF

specifies to t!e compiler t!at t!e memory for i is allocated in some ot!er pro ram and t!at address will #e iven to t!e current pro ram at t!e time of lin-in . 'ut lin-er finds t!at no ot!er varia#le of name i is availa#le in any ot!er pro ram wit! memory space allocated for it. $ence a lin-er error !as occurred . main() { int i=K1,@=K1,Q= ,"=),m; m=i++,,@++,,Q++CC"++; printf("%d %d %d %d %d",i,@,Q,",m); } Ans'er: 00/(/ ($planation : 8o ical operations always ive a result of / or 0 . ?nd also t!e lo ical ?6, (GG) operator !as !i !er priority over t!e lo ical V5 (II) operator. &o t!e expression 9i++ GG D++ GG -++9 is executed first. 2!e result of t!is expression is 0 (:/ GG :/ GG 0 = 0). 6ow t!e expression is 0 II 2 w!ic! evaluates to / (#ecause V5 operator always ives / except for 90 II 09 com#ination: for w!ic! it ives 0). &o t!e value of m is /. 2!e values of ot!er varia#les are also incremented #y /.

You might also like