Pointer Constants
4.11 Summary of Cautions
ȱ
1. Writingȱexpressionsȱthatȱhaveȱnoȱresultȱ(pageȱ72).ȱ
2. Beȱsureȱtoȱuseȱbracesȱaroundȱstatementȱlistȱinȱanȱifȱstatementȱ(pageȱ73).ȱ
3. Executionȱflowingȱunexpectedlyȱfromȱoneȱcaseȱofȱaȱswitchȱstatementȱintoȱtheȱnextȱ (pageȱ81).ȱ
ȱ ȱ ȱ
4.12 Summary of Programming Tips
ȱ
1. Inȱaȱloopȱwithoutȱaȱbody,ȱputȱtheȱsemicolonȱforȱtheȱemptyȱstatementȱonȱaȱlineȱbyȱ itselfȱ(pageȱ77.)ȱ
2. Itȱisȱeasierȱtoȱreadȱforȱloopsȱthanȱwhileȱloopsȱbecauseȱtheȱexpressionsȱthatȱcontrolȱ theȱloopȱareȱallȱtogetherȱ(pageȱ78).ȱ
3. Useȱaȱdefault:ȱclauseȱinȱeveryȱswitchȱstatementȱ(pageȱ82).ȱ ȱ ȱ ȱ
4.13 Questions
ȱ 1. Isȱtheȱfollowingȱstatementȱlegal?ȱIfȱso,ȱwhatȱdoesȱitȱdo?ȱ ȱ 3 * x * x – 4 * x + 6; ȱ 2. Whatȱisȱtheȱsyntaxȱofȱtheȱassignmentȱstatement?ȱ 3. Isȱitȱlegalȱtoȱuseȱaȱblockȱinȱthisȱmanner?ȱIfȱso,ȱwhyȱwouldȱyouȱeverȱwantȱtoȱuseȱit?ȱ ȱ ... statement { statement statement } statement4. Howȱ wouldȱ youȱ writeȱ anȱifȱ statementȱ thatȱ hadȱ noȱ statementsȱ inȱ theȱthenȱ clauseȱ butȱhadȱstatementsȱinȱtheȱelseȱclause?ȱHowȱelse,ȱcouldȱanȱequivalentȱstatementȱbeȱ written?ȱ
5. Whatȱoutputȱisȱproducedȱfromȱtheȱloopȱbelow?ȱ int i;
...
for( i = 0; i < 10; i += 1 ) printf( "%d\n", i );
6. Whenȱmightȱaȱwhileȱstatementȱbeȱmoreȱappropriateȱthanȱaȱforȱstatement?ȱ
7. Theȱcodeȱfragmentȱbelowȱisȱsupposedȱtoȱcopyȱtheȱstandardȱinputȱtoȱtheȱstandardȱ outputȱandȱcomputeȱaȱchecksumȱofȱtheȱcharacters.ȱWhatȱisȱwrongȱwithȱit?ȱ
while( (ch = getchar()) != EOF )
checksum += ch;
putchar( ch );
printf( "Checksum = %d\n", checksum );
8. Whenȱisȱtheȱdoȱstatementȱmoreȱappropriateȱthanȱaȱwhileȱorȱaȱforȱstatement?ȱ
9. Whatȱoutputȱisȱproducedȱfromȱthisȱcodeȱfragment?ȱNote:ȱTheȱ%ȱoperatorȱdividesȱitsȱ leftȱoperandȱbyȱitsȱrightȱoperandȱandȱgivesȱyouȱtheȱremainder.ȱ for( i = 1; i <= 4; i += 1 ){ switch( i % 2 ){ case 0: printf( "even\n" ); case 1: printf( "odd\n" ); } } 10. Writeȱstatementsȱthatȱreadȱanȱintegerȱvalueȱfromȱtheȱstandardȱinputȱandȱthenȱprintȱ thatȱmanyȱblankȱlines.ȱ
11. Writeȱ statementsȱ toȱ validateȱ andȱ reportȱ onȱ someȱ valuesȱ thatȱ haveȱ alreadyȱ beenȱ read.ȱIfȱxȱisȱlessȱthanȱy,ȱprintȱtheȱwordȱWRONG.ȱAlso,ȱifȱaȱisȱgreaterȱthanȱorȱequalȱtoȱb,ȱ printȱWRONG.ȱ Otherwise,ȱ printȱ theȱ wordȱRIGHT.ȱ Note:ȱ Inȱ caseȱ youȱ needȱ it,ȱ||ȱ isȱ theȱ ȈorȈȱoperator.ȱ
12. Yearsȱthatȱareȱdivisibleȱbyȱfourȱareȱleapȱyears,ȱexcept,ȱthatȱyearsȱthatȱareȱdivisibleȱ byȱ 100ȱ areȱ not.ȱ However,ȱ yearsȱ thatȱ areȱ divisibleȱ byȱ 400ȱ areȱ leapȱ years.ȱ Writeȱ statementsȱ toȱ determineȱ whetherȱ theȱ valueȱ inȱ yearȱ isȱ aȱ leapȱ year,ȱ andȱ setȱ theȱ variableȱleap_yeasȱtoȱoneȱifȱitȱisȱaȱleapȱyear,ȱandȱzeroȱifȱitȱisȱnot.ȱ
13. Newspaperȱreportersȱareȱtrainedȱtoȱaskȱwho,ȱwhat,ȱwhen,ȱwhere,ȱandȱwhy?ȱWriteȱ statementsȱthatȱwillȱprintȱwhoȱifȱtheȱvariableȱwhich_wordȱisȱone,ȱwhat if the ȱ
ȱ
variableȱisȱtwo,ȱandȱsoȱforth.ȱIfȱtheȱvalueȱisȱnotȱinȱtheȱrangeȱoneȱthroughȱfive,ȱprintȱ
don’t knowȱinstead.ȱ
14. Pretendȱ thatȱ aȱ ȈprogramȈȱ controlsȱ you,ȱ andȱ thisȱ programȱ containsȱ twoȱ function:ȱ
4.14 Programming Exercises 89 valueȱdependingȱonȱwhetherȱyouȱareȱhungry.ȱWriteȱtheȱstatementȱthatȱallowȱyouȱtoȱ eatȱasȱmanyȱhamburgersȱasȱyouȱwantȱuntilȱyouȇreȱnoȱlongerȱhungry.ȱ 15. Modifyȱyourȱanswerȱtoȱquestionȱ14ȱtoȱsatisfyȱyourȱgrandmother—Youȇveȱgotȱtoȱeatȱ something!—soȱthatȱyouȱalwaysȱeatȱatȱleastȱoneȱhamburger.ȱ 16. Writeȱstatementsȱtoȱprintȱaȱcapsuleȱsummaryȱofȱtheȱcurrentȱweatherȱaccordingȱtoȱ theȱvaluesȱofȱtheȱvariablesȱprecipitatingȱandȱtemperature.ȱ ȱ
Ifȱprecipitatingȱisȱ…ȱ andȱtemperatureȱisȱ…ȱ thenȱprintȱ…ȱ
<ȱ32ȱ snowing trueȱ >=ȱ32ȱ raining <ȱ60ȱ cold falseȱ >=ȱ60ȱ warm ȱ ȱ ȱ ȱ
4.14 Programming Exercises
ȱ1. Theȱ squareȱ rootȱ ofȱ aȱ positiveȱ numberȱ nȱ canȱ beȱ computedȱ asȱ aȱ seriesȱ ofȱ approximations,ȱ eachȱ moreȱ accurateȱ thanȱ theȱ last.ȱ Theȱ firstȱ approximationȱ isȱ one;ȱ successiveȱapproximationsȱareȱgivenȱbyȱtheȱfollowingȱformula.ȱ ȱ 1 2 i i i n a a a ȱ ȱ Writeȱaȱprogramȱtoȱreadȱaȱvalueȱandȱcomputeȱandȱprintȱitsȱsquareȱroot.ȱlfȱyouȱprintȱ allȱ ofȱ theȱ approximationsȱ youȱ canȱ seeȱ howȱ quicklyȱ thisȱ methodȱ convergesȱ onȱ theȱ correctȱvalue.ȱInȱprinciple,ȱtheȱcomputationȱcouldȱgoȱonȱforever,ȱyieldingȱmoreȱandȱ moreȱaccurateȱvalues.ȱInȱpractice,ȱthough,ȱtheȱrestrictedȱprecisionȱofȱfloatingȬpointȱ variablesȱ preventsȱ theȱ programȱ fromȱ continuing.ȱ Haveȱ yourȱ programȱ stopȱ computingȱwhenȱanȱapproximationȱisȱequalȱtoȱtheȱpreviousȱone.ȱ
2. Anȱ integerȱ isȱ calledȱ primeȱ ifȱ itȱ isȱ notȱ evenlyȱ divisibleȱ byȱ anyȱ integerȱ otherȱ thanȱ itselfȱandȱone.ȱWriteȱaȱprogramȱtoȱprintȱthoseȱnumbersȱinȱtheȱrangeȱ1–100ȱthatȱareȱ prime.ȱ
3. Allȱthreeȱsidesȱofȱequilateralȱtrianglesȱareȱtheȱsameȱlength,ȱbutȱonlyȱtwoȱofȱtheȱsidesȱ ofȱanȱisoscelesȱtriangleȱareȱequal.ȱlfȱallȱofȱtheȱsidesȱofȱaȱtriangleȱareȱdifferentȱlengthsȱ
itȱisȱcalledȱscalene.ȱWriteȱaȱprogramȱtoȱpromptȱforȱandȱreadȱthreeȱnumbersȱthatȱareȱ theȱlengthsȱofȱtheȱthreeȱsideȱofȱaȱtriangle.ȱTheȱprogramȱshouldȱthanȱdetermineȱwhatȱ typeȱ ofȱ triangleȱ theȱ numbersȱ represent.ȱ Hint:ȱ Whatȱ elseȱ shouldȱ theȱ programȱ beȱ lookingȱfor?ȱ
4. Writeȱtheȱfunctionȱcopy_nȱwhoseȱprototypeȱisȱshownȱbelow.ȱ void copy_n( char dst[], char src[], int n );
Theȱ functionȱ isȱ toȱ copyȱ aȱ stringȱ fromȱ theȱ arrayȱsrcȱ toȱ theȱ arrayȱdstȱ butȱ withȱ theȱ followingȱ requirement:ȱ exactlyȱnȱ charactersȱ mustȱ beȱ storedȱ intoȱdst;ȱ noȱ more,ȱ noȱ less.ȱIfȱtheȱlengthȱofȱtheȱstringȱinȱsrcȱisȱlessȱthanȱn,ȱthenȱyouȱmustȱaddȱenoughȱNULȱ charactersȱ afterȱ theȱ copiedȱ charactersȱ toȱ getȱ aȱ totalȱ ofȱnȱ charactersȱ stored.ȱ Ifȱ theȱ lengthȱ ofȱ theȱ stringȱ inȱsrcȱ isȱ greaterȱ thanȱ orȱ equalȱ toȱn,ȱ thenȱ stopȱ afterȱ youȱ haveȱ storedȱtheȱnȇthȱcharacter;ȱinȱthisȱcaseȱdstȱwillȱnotȱbeȱNULȬterminated.ȱNoteȱthatȱaȱcallȱ toȱcopy_nȱ shouldȱ storeȱ somethingȱ intoȱdst[0]ȱ throughȱdst[n-1],ȱ andȱ onlyȱ thoseȱ locations,ȱregardlessȱofȱtheȱlengthȱofȱsrc.ȱ
Ifȱ youȱ planningȱ onȱ usingȱ theȱ libraryȱ routineȱ strncpyȱ toȱ implementȱ yourȱ program,ȱyouȱareȱcongratulatedȱforȱreadingȱahead,ȱbutȱtheȱgoalȱhereȱisȱforȱyouȱtoȱ figureȱoutȱtheȱlogicȱyourself,ȱsoȱyouȱmayȱnotȱuseȱanyȱofȱtheȱlibraryȱstringȱroutines.ȱ 5. Writeȱaȱprogramȱthatȱreadsȱtheȱstandardȱinputȱlineȱbyȱlineȱandȱdocsȱtheȱfollowing:ȱ
forȱeachȱsetȱofȱtwoȱorȱmoreȱidentical,ȱadjacentȱlinesȱinȱtheȱfile,ȱoneȱlineȱfromȱtheȱsetȱ shouldȱ beȱ printedȱ out;ȱ nothingȱ elseȱ shouldȱ beȱ printedȱ out.ȱ Youȱ mayȱ assumeȱ thatȱ theȱlinesȱinȱtheȱfileȱwillȱnotȱexceedȱ128ȱcharactersȱinȱlengthȱ(127ȱcharactersȱplusȱoneȱ forȱtheȱnewlineȱthatȱterminatesȱeachȱline).ȱ
Considerȱtheȱinputȱfileȱshownȱbelow.ȱ ȱ
This is the first line. Another line. And another. And another. And another. And another. Still more.
Almost done now – Almost done now – Another line. Still more. Finished! ȱ
Assumingȱ thatȱ thereȱ areȱ noȱ trailingȱ blanksȱ orȱ tabsȱ onȱ anyȱ ofȱ theȱ linesȱ (whichȱ wouldnȇtȱ beȱ visibleȱ butȱ wouldȱ makeȱ theȱ lineȱ differentȱ fromȱ itsȱ neighbors),ȱ thisȱ
4.14 Programming Exercises 91 programȱwouldȱproduceȱtheȱfollowingȱoutputȱfromȱthisȱinputȱfile.ȱ
ȱ ȱ
ȱ And another.
Almost done now –ȱ
ȱ
Oneȱ lineȱ fromȱ eachȱ setȱ ofȱ adjacentȱ identicalȱ linesȱ isȱ printed.ȱ Noticeȱ thatȱ ȈAnotherȱ line.ȈȱandȱȈStillȱmoreȈȱareȱnotȱprintedȱbecause,ȱalthoughȱthereȱareȱtwoȱofȱeachȱinȱtheȱ file,ȱtheyȱareȱnotȱadjacent.ȱȱ
Hints:ȱ Useȱgetsȱ toȱ readȱ theȱ inputȱ lines,ȱ andȱstrcpyȱ toȱ copyȱ them.ȱ Thereȱ isȱ aȱ routineȱ calledȱstrcmpȱ thatȱ takesȱ twoȱ stringsȱ asȱ argumentsȱ andȱ comparesȱ them.ȱ Itȱ returnsȱzeroȱifȱtheyȱareȱequalȱandȱaȱnonzeroȱvalueȱifȱtheyȱareȱnot.ȱ
6. Writeȱaȱfunctionȱthatȱextractsȱaȱsubstringȱfromȱaȱstring.ȱTheȱfunctionȱshouldȱhaveȱ theȱfollowingȱprototype:ȱ
ȱ
int substr( char dst[], char src[], int start, int len )
ȱ
Itȱ shouldȱ copyȱ theȱ stringȱ thatȱ beginsȱstartȱ charactersȱ pastȱ theȱ beginningȱ ofȱ theȱ stringȱinȱsrcȱintoȱtheȱarrayȱdst.ȱAtȱmost lenȱnonȬNULȱcharactersȱshouldȱbeȱcopiedȱ fromȱsrc.ȱ Afterȱ copying,ȱdstȱ mustȱ beȱNULȬterminated.ȱ Theȱ functionȱ shouldȱ returnȱ theȱlengthȱofȱtheȱstringȱstoredȱinȱdst.ȱ
Ifȱstartȱspecifiesȱaȱpositionȱbeyondȱtheȱendȱofȱtheȱstringȱinȱsrc,ȱorȱeitherȱstartȱ orȱlenȱareȱnegative,ȱthenȱdscȱshouldȱbeȱgivenȱtheȱemptyȱstring.ȱ
7. Writeȱaȱfunctionȱthatȱremovesȱexcessȱwhiteȱspaceȱfromȱaȱstringȱofȱcharacters.ȱTheȱ functionȱshouldȱhaveȱthisȱprototype:ȱ
ȱ
void deblank( char string[] ); ȱ
Everyȱrunȱofȱoneȱorȱmoreȱwhiteȱspaceȱcharactersȱshouldȱbeȱreplacedȱbyȱoneȱspaceȱ character.ȱBeȱsureȱthatȱtheȱstringȱisȱterminatedȱwithȱaȱNULȱbyteȱwhenȱyouȇreȱthroughȱ withȱit!ȱ
ȱ ȱ