ebook img

Test Your C Skills PDF

129 Pages·2006·59.64 MB·English
by  
Save to my drive
Quick download
Download
Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.

Preview Test Your C Skills

Contents Introduction Declarations an Initializations Control Instuetions Expressions Flowing Point Issues Functions ‘The C Preprocessor Pointers ‘More About Pointers Amys Strings Structates, Unions and Faumerations InpavOuput ‘Gonumand Line Arguments ptwise Operas Sublletes of npedlef “The cones Phenomenon Memory Allocation ‘Variable Number of Arguments Complicated Destoraions Liveary Functions wv 169 1 231 Introduction “There is no dearth of goud C programming books im the market However [found that there is mot much material which could help a (C programmer to res his programming sttengts, help improve his ‘ponfidence and inthe pracess hone his (skis. Hence this book ‘This is not text Book om C. Infact tis far Feom Tteontsins alot ot questions segreyated topie-wise ascardtg to my perception ofthe larguage, Almost all the questions ae real one's Asked by real people llempting to Tear or program in C, “Thore is no reason why you should read the questons in the same border es they appear in ‘his book. You can pick pany topic tht you think you are good at (or poor at} an try to test your skills on that topic. “There isa good chance that you ate earning a vsing C and you hheve questions abput that aren'tanswered in any ofthe ether books you'vechecked, you woud find hem ansroted here. Itwouldbe too Toeh to expect that you world find in this book answer 10 every {question you would have when you're progeemming in C. This is Because many of the questions that may some up in yrur program ming sould have to do with your problem domain, whereas evs bok Concentrates only on the C language, Also it docsn’s enver every aspect of every operaling system under whic’ C isrunm ny, Problems vecifc to nn operating systems, and ponera purpose elgorishms ae proper y discussed in hooks devated io those tape. At the end of each ehaprer you would find correct answer to the {questions in that chapter, You would tind some answers more Claborate than oshers. AL fist sight this may seem unnecessary However, [lave done this to give you the complete pictute rather than oversimpiitying or Ieaving wut important detail. have tiv w avoiu the questions whose answers ane ron. obvious beens the idea sas nt fo increase the nummer questions, but tO present.questiors winch wold oroe the readers tink wre before answering, That sin tune withthe spirit oC he presse, bceviy bas its own importance. Chapter 1 Soroll your sleeves und get on with eve real cuestions, Good lush! Declarations and Initializations ‘Yashavant P. Kanetkir Nov 1996 Qu ‘What would be the output 3 eFotewing progeaun? ( charfar‘st*62; Dent "ed size st, siomt <P }) Qn ‘What would ke the ouspu of the Flowing program? ints = 4 Prin { intx=20; print (“tse x) } Qs What would be the nut ofthe following program? , <a 2 2 Test Your € Skills Chapter 1: Declarations ont! fnitialisulons 3 nh) te Q is Tsiluuetata global viable ny have several dearatensbacenly ne defttiion? <YexNe> paint) Qu Isitrme hot frien may have saver decratons, but only ne Qu definition? <Yes!No~ Icha follwing minersena declaration ora eke anuen” Q 18 oni Inthe fllovirg program wheres tac vial u geting Uefined and ‘wheres it geting dec-arod? Qa rain) 5 ‘Whar would he the output ofthe fallosing program! extominta. rin (ic mmsin() } ina=20; ext it; 120: @ 19 rine sizgot (1) What wom be tke ouput ofthe folosying progearn? eo a 4 main’) ©. Would vary from compiler to compiler 1D. Eiror, (defined extom inca; pai nr a) ) naz; 3 ‘ost Yur € Skil mn a Garbage vulus Enor a 8 © D. Qs What's the difference between a defintan und declaration of varie? Qu Itghe definidon oan exter! variable uceurs mthe source tle before its use m x particular function, then there 1S ne noed for an extern slecluaticn inthe Function, «TsuctFalse> Q in Suppose program is divided irto three files), 2 aud al ‘variable is defined i le ile? hue used in the files 2 anf. n seh «4ease would We need the external declaralion for ‘he variables the files andj3?-<y estNo> Q us When we mention the protarype of a function are we definin funatian or devlaring i? Q i ‘What's the uifference between the following declarations? 2. Chaptor 1: Declarations ona hi oem int fin} ietun() Q us Why Hoes th Futon ig Fopeet a redeclaraion enor bY ( splay) ‘Wiha would be the output of te Following progr’? ain) ‘ cota tun fat} ita; atin 344); pei (a ' tun} relur {{ int) aa 6 est Your C Sills 3 344 a Enor Poin out the errr, ifany, in the Following program. smuctemp chat nae intoge; 1 7 some meso cade nay goer" fun( ita} ‘ ino: =a" retum bb); ) slo) t ina: aziin(20); inf ("WC 8); ) Chapter I: Declarations and initiatisations Q us Tf you are to share the varies or functions across several souroe files haw would you ensure tht all definitions and deckarasions are ceansisere? Q 129 ‘How would you reetity the error in the following program? {fstuct emp); any cher proltypes may oo here" fmt emp charroms[20); Intage sructor = {“Soicher’ 34); te); } ${etue omp 00) i Q 120 Glohal variables are available to all functions. Does there exist a mechanism by way of which Ian make it wvalable to some and not tw others rn, ks "e" e:name, 02.98 5 Pont tate (Sit (Chapter 1: Declareions and Iniiatisations 9 A 0.900000 fee oc ae Garbage values ‘What eo you mean ba trstation mnt eo D. None af euhove Q 12 Q 124 Wher weold bee oulpu ot folowing poate? ae Some books suggest chat the ollnwing definitions shone \ preceded by the word state Tse core Wr ie aoa intel] (2,3, 4, 12.02 i 55, of 3. stuctemp e~("saney", 23) A. Garbage valucs Q wos Bb 2a3 ins2'2 pint ou the coi aay, he Following program See Point out the enor. if any nthe following progran maint) Q ix { in(*9 ) =tuns ‘Wheat would be the output th following progra ae fun) rn t sired wg oo charrama(2] 7 tage * Q 126 float sal iH Point out be eror, if any, inthe Following prograte shuctempe= ("Tiger } Pi "nsieP, e299. o.el] an) ( i se esas eet est Your © Skil ini cha’ ef } urionaz=812 prin “Ld eh, zeh] Ss Q wr ‘What do you mean by scope of a variable What ere the 4uiffereat types of scopes that a variable cam have? Q 128 ‘What are the differenc types of inkages? Answers. eet a2 A 12 20. Whenever there isu conflict between a local variahleand a global Satiabe it isthe local variable which gels a privity ons aud lnitatiations Ht 20 40. Incase of a cantict between local vatiaoles, the one whichis ‘more local that gets The priority Aw Declaration A is extern inv iis wdeeleation and not x definition, hence the eer. A i6 Yes Aw Ves A 1s {ektern int cis the declaration whereas i. A 19 10s the definition. A 2 ‘os Your € Skills A 1 Inthe definton of a variuhle space is reserved forthe variable andl sonic inital values given tole, whereas coslaration only identifies the type of the variable Fors Function. Taus definition isthe place "aor te varahle iscrcatod orassigaed storage whorees declaration referto places where raturcal the variable slated bueno storage isallocate. We are deeiaring il. Wha the fonction alongwith the statements belonging to iare mentioned we are defining the Function, Ai ou “There leno difference except forthe fact thatthe fis one gives. bint that te fonction fund) is probably in unolbe: saute file. A165 Hore display jis called betore:tis defined. In such assumes thal the functor display is declared ay Chapter 1: Declarations and tnialisasions int spay): ‘That is, n undeclared Fonction is assumed to setuinan int aud aoe an unspecified mumber of arguments. Then when we define the ‘onction che compiler finds hat tisreursing void hence tre complet report the discrepancy, A 16 , The enorocenrs bovause we have mixed the ANST ps K& Rotyle of fusction definition, ype with Wien we use ANSI protptype fora funesion and passa leut tothe fantion itis prennoted ty a double, When the fonetion accepts this, douse into afloat a ype mismatch occurs hence the errr The remedy for ths ewor could he to define te fonction as intun (feat 29} i } Aiur Revaure af she missing semicolon atthe end of the structure deela- fution (ihe intervening comment further abscures if) the couupiler believes ts fant! would reuur sorneting ofthe the ty30 struct em, whoress in acua ity vis avempring to rete ar int. This causes a mismatch, hence an ercor results. 7 ‘Test Your € Skits (Chapror 1: Declurutions ene Initiatisations is A i ‘Te best arrangement is place cach definition in relevant efile ‘Then, gut an extemal declaration in a header file (h ile) and uso inlaw bring inthe deleration wherever needed. ‘The .¢ file which contains the definition should alsa include Ge Ineader ile, o thal the compiler san check Ihat the definition matches the declaration, Declare the structure before the prototype off A 120 'No. The ouly way this canbe achieved ist define the variable Locally in main) snstead of defining it globally and then passing it wv the Tunetions which need it A 121 ‘A teanslacon gait isa set of source Miles seen hy the compiler and translated ara unit: generally one file, plus all header files men ‘oned in include directives. A 12 D. When an automatic array is partly initialised, the remaining aay clement ae niilisedt00. A 123 A. When un autonsatie stuctone is patally iia ise, the remaining Clemens ofthe structure are initalisod 0. A 124 Pre-ANST C compilers had such ¢ requisemsnt, Compilers whic ‘Son’orm co ANS! € standard do no: have such x requirement A 125 Tere we are iniiatising tho function pointer p tothe adress of the function vi. Bul during thisintialsation the function has noc heen defined. Hence an ercor ‘To eliminate this enc add the protoryps ofthe funt} before declara- lion ofp, as shown belov: ‘eden int): orsimply inting); A 126 Ina pre-ANSI compiler a uni varlable cannot be Intallsed. How- cf, ANSI C permits imualisation of first memeber ofthe union. 16 est Your (kills A 127 ‘Scope indices the region ever whieh the varable's declaration has an effect, The Four kinds of scopes arc’ file, funetion, Rluck and proioype. A 128 Trore arw three different types of linkagess external, internal and ‘one. External inkage means global, no-satc varies an fune- Uns, internal Linksge mans static variables and fonctions with le Scope, ad no linkage means local variables. Chapter 2 Control Instructions Qx ‘What would be the ouput ofthe following pragrarr? bata print nf mauseieanelpant but 2y te Jaserese! 9601 pint “needing abi share aling expe ‘sat hen raed Q 22 Point out the errr, 1 any, nthe fo" loop.

See more

The list of books you might like

Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.