资源描述:
《浙江大学C语言课件.ppt》由会员上传分享,免费在线阅读,更多相关内容在教育资源-天天文库。
1、TheCProgrammingLanguageIntroductionCisageneral-purposeprogramminglanguageCprovidesthefundamentalcontrol-flowconstructionsCisarelatively“low-level”languageCprovidesnooperationstodealdirectlywithcompositeobjectsCoffersonlystraightforward,single-threadcontrolflowCisind
2、ependentofanyparticularmachinearchitectureSomeofCoperatorshavethewrongprecedenceSomepartsoftheCsyntaxcouldbebetterChapter1.ATutorialIntroductionLetusbeginwithaquickintroductiontoCWritingaprograminCtoprintthewords:hello,worldInC,theprogramtoprint”hello,world”is:#incl
3、udemain(){printf(“hello,world”);}It’seasy!includeinformationaboutstandardlibrarymainfunctionwithnoargumentscalllibraryfunctionnamedprintfstatementsofmainareenclosedinbraceTheprogramwouldbewritten:#includemain(){printf(“hello,“);printf(“world”);pr
4、intf(“”);}【example】Calculate1+2+3+……+100◆Howtocalculatea+b(ifa=1,b=2)main(){inta,b,sum;variablesdeclearedbeforeuseda=1;b=2;sum=0;sum=a+b;printf(“%d”,sum);}◆Howtocalculatea+b+c(ifa=1,b=2,c=3)main(){inta,b,c,sum;a=1;b=2;c=3;sum=0;sum=a+b;sum=sum+c;printf(“%d”,su
5、m);}Understand?Letmesee.【example】Calculate1+2+3+……+100◆Howtocalculate1+2+3+……+100main(){inti,sum;i=1;sum=0;while(i<=100){sum=sum+i;i=i+1;}printf(“%d”,sum);}It’seasytoo!Ifa,binputtedfromkeyboardwithdifferentvalues?◆Howtocalculatea+b(ifa,binputted)main(){inta,b,sum;
6、scanf(“%d%d”,&a,&b);sum=a+b;printf(“%d”,sum);}Ican!inputfromkeyboardEXAMPLE:编1-1/2+1/3-1/4+......,下面是编的程序,但是结果是0.000,可能是小数整数没有转换好,可是不知道哪里错了?????#includemain(){ float t,i,sum,n;i=1.0;sum=0.0;t=1;n=1.0; while(n<=100000){ t=t*(-1); i=t/n;
7、 sum=sum+i; n=n+1; }; printf("%.4f",i); }EXAMPLE:大小字符转换:(pleaseanswer)charc;小变大:if()大变小:if()1.2VariablesandArithmeticExpressionsInC,allvariablesmustbedeclaredbeforetheyareused.Formats:typevariablesExamplesintfahr,celsius;intlower,upper,step;floattot
8、al;InC,anexpressioniscomposedwith:constantsvariablesfunctionsoperatorsExamples:fahr=fahr+step1.2VariablesandArithmeticExpressionsProgramEx