资源描述:
《the_c_programming_language习题答案解析.pdf》由会员上传分享,免费在线阅读,更多相关内容在教育资源-天天文库。
1、"TheCProgrammingLanguage",2ndedition,KernighanandRitchieAnswertoExercise1-1Runthe"hello,world"programonyoursystem.Experimentwithleavingoutpartsoftheprogram,toseewhaterrormessagesyouget.Murphy'sLawdictatesthatthereisnosinglecorrectanswertotheveryfirstexerciseinth
2、ebook.Ohwell.Here'sa"helloworld"program:#includeintmain(void){printf("hello,world");return0;}Asyoucansee,I'veaddedareturnstatement,becausemainalwaysreturnsint,andit'sgoodstyletoshowthisexplicitly.AnswertoExercise1-2Experimenttofindoutwhathappenswhenpr
3、intf'sargumentstringcontainsc,wherecissomecharacternotlistedabove.By'above',thequestionisreferringto:(newline)t(tab)b(backspace)"(doublequote)\(backslash)Wehavetotreadcarefullyhere,becauseusinganon-specifiedescapesequenceinvokesundefinedbehaviour.Thefollo
4、wingprogramattemptstodemonstrateallthelegalescapesequences,notincludingtheonesalreadyshown(except,whichIactuallyneedintheprogram),andnotincludinghexadecimalandoctalescapesequences.#includeintmain(void){printf("Audibleorvisualalert.a");printf("Formf
5、eed.f");printf("Thisescape,r,movestheactivepositiontotheinitialpositionofthecurrentline.");printf("Verticaltabvistricky,asitsbehaviourisunspecifiedundercertainconditions.");return0;}AnswertoExercise1-3Modifythetemperatureconversionprogramtoprintaheading
6、abovethetable.#includeintmain(void){floatfahr,celsius;intlower,upper,step;lower=0;upper=300;step=20;printf("FC");fahr=lower;while(fahr<=upper){celsius=(5.0/9.0)*(fahr-32.0);printf("%3.0f%6.1f",fahr,celsius);fahr=fahr+step;}return0;}AnswertoExercis
7、e1-4WriteaprogramtoprintthecorrespondingCelsiustoFahrenheittable.#includeintmain(void){floatfahr,celsius;intlower,upper,step;lower=0;upper=300;step=20;printf("CF");celsius=lower;while(celsius<=upper){fahr=(9.0/5.0)*celsius+32.0;printf("%3.0f%6.1f"
8、,celsius,fahr);celsius=celsius+step;}return0;}AnswertoExercise1-5Modifythetemperatureconversionprogramtoprintthetableinreverseorder,thatis,from300degreesto0.Thisversi