TheCProgrammingLanguageNorthChinaElectricPowerUniversity21/26/2020CourseArrangementClasshours:Total:56hours5rd~19thweekTeaching:30hours5rd~19thweekProgramming:26hours7th~19thweek31/26/2020ExaminationofTheCourseTheexamcontains3parts:Writtentest:70%HomeworkandProgrammingexperiments:20%Programmingtest:10%Chapter1OverviewofCPROGRAMMINGINANSIC51/26/2020CharacteristicsStructuredHigh-levelMachineIndependentHistoryUNIXThefunctionisthebasicunitofClanguage.ACprogramperformsitstaskbycallingvariousfunctions,andithasagoodcontrolstructure.Clanguageusethewayclosedtothehumannaturelanguagetoexpresstheorderstatements.Thesestatementsareeasilytobememorizedandbeunderstood.TheCompilertranslatesaCprogramtothemachinecodescomposedby0and1,andthenletcomputerexecutethesemachinecodes.However,programmersdon’tneedtoconsiderit.Chapter1OverviewofCCprogramcanbeexecutedindifferentcomputers,andyouneednotconsiderthedifferencebetweenvariouscomputers.ClanguagewasdevelopedalongwithUNIX.Infact,thegoalofthebirthofCistoprogramUNIX.BothUNIXandthemostoftheprogramswhichrunonUNIXarewritteninC.61/26/2020Isee,Iremember!Isee,Iremember!printf(Isee,);printf(Iremember!);printf(Isee,\n);printf(Iremember!);5)newlinecharacter:“\n”ASimpleCProgram1–PrintingaMessagemain(){/*----------printingbegins----------*/printf(Isee,Iremember!);/*-----------printingends-----------*/}Everyprogrammusthaveonlyonemainfunction!Theformsofthemainstatement:main()intmain()voidmain()main(void)voidmain(void)intmain(void)comment/*ThisisaCprogram/*output*/,09-01-08*/ThecommentscannotbenestedinC!!!printf(Isee,\nIremember!);2)Thecommentscannotbenested!1)Everyprogrammusthaveonlyonemainfunction3)Everystatementshouldendwith“;”4)Cmakesadistinctionbetweenuppercaseandlowercaseletters71/26/2020ASimpleCProgram2–AddingTwoNumbersmain(){intnumber;floatamount;number=100;amount=number+75.35;printf(number=%d\n,number);printf(amount=%6.2f,amount);}Allvariablesmustbedeclaredbeforetheyareused.numberamount100175.35number=100_number=100amount=175.3581/26/2020ASimpleCProgram3–InterestCalculation#definePRINCIPAL5000.00main(){floatamount,inrate=0.11;amount=PRINCIPAL;amount=amount+inrate*amount;printf(Amount=$%.2f,amount);}Amount=$5550.00#defineinstructionisapreprocessorcompilerdirectiveandnotastatement,soitshouldnotendwitha“;”PRINCIPAL=PRINCIPAL+inrate*PRINCIPAL;Error…6:Lvaluereqiredinfunctionmain5000.00=5000.00+inrate*5000.00;91/26/2020ASimpleCProgram4–UseofSubfunctionsintmul(intx,inty){intp;p=x*y;return(p);}main(){inta=5,b=10,c;c=mul(a,b);printf(%d*%d=%d,a,b,c);}5*10=50mul(5,10)50101/26/2020ASimpleCProgram5–UseofMathFunctions#includemath.h#definePI3.1416main(){intangle=30;floatc;c=sin((PI/180)*angle);printf(sin(%d)=%.2f,angle,c);}sin(30)=0.50111/26/2020BasicStructureofCProgramsDocumentationSectionLinkSectionDefinitionSectionGlobalDeclarationSectionmain()FunctionSection{DeclarationpartExecutablepart}User-definedfunctionsfunction1……functionn121/26/2020ProgrammingStyleCallowsthefree-formprogramming.1.Symbolicconstantsarewritteninuppercaseletters,andothers,suchasvariablesorfunctionnames,inlowercases.2.Eachstatementisplacedinalinebyitself.Don’twriteonestatementintwoormorelines.Don’twritetwoormorestatementsinoneline.3.Properlywriteblankline,suchasbetweendeclarationsandexecutablestatements.4.Usecommentsproperly.5.Alignapairofbraces.6.Alignthestatementsinthesamelayer.Andindentthosestatementsinlowerlayer.131/26/2020ExecutingaCProgram1.Createthesourceprogram.(thefileextensionis“.c”)2.Compilethesourceprogram.Itgeneratestheobjectcodefile.(thefileextensionis“.obj”)3.Linktheprogramwithotherprogramfilesandfunctionsthatarerequiredbytheprogram.Itgeneratesexecutablefile(thefileextensionis“.exe”).4.Executetheexecutablefile.141/26/2020TurboC2.0151/26/2020TurboC2.0161/26/2020HomeworkReviewQuestionsP191.1~1.4&1.8~1.10(Writedowninyourexercisebook)1.5~1.7(Thinkaboutthem,andthencheckyourthoughtthroughprogrammingexperiments)1.11~1.20(Thinkwhetheryoucanunderstandallofthem)ProgrammingExercises