![1.1.7C程序与程序设计简介 - C程序与程序设计简介1.1_第1页](http://file4.renrendoc.com/view12/M03/10/1D/wKhkGWZK4CaAOrJBAAEQEytc4nY425.jpg)
![1.1.7C程序与程序设计简介 - C程序与程序设计简介1.1_第2页](http://file4.renrendoc.com/view12/M03/10/1D/wKhkGWZK4CaAOrJBAAEQEytc4nY4252.jpg)
![1.1.7C程序与程序设计简介 - C程序与程序设计简介1.1_第3页](http://file4.renrendoc.com/view12/M03/10/1D/wKhkGWZK4CaAOrJBAAEQEytc4nY4253.jpg)
![1.1.7C程序与程序设计简介 - C程序与程序设计简介1.1_第4页](http://file4.renrendoc.com/view12/M03/10/1D/wKhkGWZK4CaAOrJBAAEQEytc4nY4254.jpg)
![1.1.7C程序与程序设计简介 - C程序与程序设计简介1.1_第5页](http://file4.renrendoc.com/view12/M03/10/1D/wKhkGWZK4CaAOrJBAAEQEytc4nY4255.jpg)
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
TheCProgrammingLanguage典型程序设计语言的排行HistoryofCEarlydevelopmentsAT&TBellLabs,1969-1973DennisMacAlistairRitchie(DMR)TransplantUnixfromPDP-7toPDP-11withKennethLaneThompson(Ken)Add”struct”into,1973MostoftheUnixkernelwasrewritteninCK&RC:In1978,”TheCProgrammingLanguage”BrianKernighanandDennisRitchieStandardI/Olibrary(MikeLesk)Longintdatatype,unsignedintdatatype,compoundassignmentoperators=opKen&Ritchie
---1998NationalMedalofTechnologyfromBillClinton
ANSICandISOCC89,1983,ANSI;C99,1990,ISOFunctionprototypes,voidpointersinternationalcharactersetsandlocalespreprocessorenhancementsC99ISO/IEC9899:1999,1999inlinefunctionsseveralnewdatatypes(includinglonglongintandacomplextypetorepresentcomplexnumbers)variable-lengtharrays...C1x,workhasbegunsince2007IntroductionCisageneral-purposeprogramminglanguageCprovidesthefundamentalcontrol-flowconstructionsCisarelatively“low-level”languageCprovidesnooperationstodealdirectlywithcompositeobjectsCoffersonlystraightforward,single-threadcontrolflowCisindependentofanyparticularmachinearchitectureSomeofCoperatorshavethewrongprecedenceSomepartsoftheCsyntaxcouldbebetterChapter1.ATutorialIntroductionLetusbeginwithaquickintroductiontoCWritingaprograminCtoprintthewords:hello,worldInC,theprogramtoprint”hello,world”is:#include<stdio.h>
main(){
printf(“hello,world\n”);}------------------------------------------------------------------------InPASCAL,theprogramwouldbe:Programhello(output);beginwriteln(‘hello,world’)end.includeinformationaboutstandardlibrarymainfunctionwithnoargumentscalllibraryfunctionnamedprintfstatementsofmainareenclosedinbraceTheprogramwouldbewritten:
#include<stdio.h> main() {
printf(“hello,”);
printf(“world”);
printf(“\n”); }
1.2VariablesandArithmeticExpressionsInC,allvariablesmustbedeclearedbeforetheyareused.Formats:typevariablesExamples intfahr,celsius; intlower,upper,step; floattotal;InC,anexpressioniscomposedwith:constantsvariablesfunctionsoperatorsExamples fahr=fahr+step1.2VariablesandArithmeticExpressionsProgramExamplesFahrenheit Celsius0-1720-640460158026100371204814060160711808220093220104260126280137300148
Program:#include<stdio.h> /*printfFahrenheit-Celsiustable
forfahr=0,20,40,…,300*/main(){ intfahr,celsius; intlower,upper,step; lower=0; /*lowerlimitofTtable*/ upper=300;/*upperlimit*/
step=20; /*stepsize*/ fahr=lower; while(fahr<=upper){ celsius=5*(fahr-32)/9; printf(“%d\t%d\n”,fahr,celsius); fahr=fahr+step; }}1.2VariablesandArithmeticExpressionsTheprintfusefulformat:%d printasdecimalinteger;%6d printasdecimalinteger,asatleast6characterswide;%f printasfloatingpoint;%6f printasfloatingpoint,asatleast6characterswide;%.2f printasfloatingpoint,2charactersafterdecimal point;%6.2fprintasfloat,atleast6wideand2charactersafter decimalpoint;%o printasoctal;%x printashexadecimal;%c printasacharacter; %s printascharacterstring;%% print%itself
1.3TheFORstatementRewritedprogramoftemperatureconverterusingFOR:#include<stdio.h>/*printFahrenheit-Celsiustable*/ main() { intfahr; for(fahr=0;fahr<=300;fahr=fahr+20)printf(“%3d%6.1f\n”,fahr,(5.0/9.0)*(fahr-32)); }1.4SymbolicConstantsSymbolicConstantsisaparticularstringofcharacters.Format:
#definenamereplacement-textFunction:
Anyoccurrenceofnamewillbereplacedbythecorrespondingreplacement-text.Examples:
#defineLOWER0/*lowerlimitoftable*/ #defineUPPER300/*upperlimitoftable*/ #defineSTEP20/*steplimitoftable*/ /*printFahrenheit-Celsiustable*/ main() {
int
fahr; for(fahr=LOWER;fahr<=UPPER;fahr=fahr+STEP)
printf(“%3d%6.1f\n”,fahr,(5.0/9.0)*(fahr-32)); }1.5CharacterInputandOutputImportantView:
InC,themodelofInput&Outputaresupportedbythestandardlibrary,andNOTthestatementsofC.ThisisverydifferentofPASCAL.
TowsimplestI/Ofunctions: .getchar()——readoncharacteratatimefrom textstream
c=getchar(); .putchar()——writeonecharacteratatimefrom textstream
putchar(c);1.5CharacterInputandOutputFileCopyingAlgorithmProgram:
#include<stdio.h> /*copyinputtooutput;1stversion*/ main() { intc; c=getchar(); while(c!=EOF){ putchar(c); c=getchar(); } }readacharacterwhile(characterisnotend-of-fileindicator)outputthecharacterjustreadreadacharacter1.5CharacterInputandOutputFilecopyingprogram(2nd
version) #include<stdio.h> /*copyinputtooutput;2ndversion*/ main() { intc; while((c=getchar())!=EOF) putchar(c); }NOTE:c=getchar()!=EOFiseauivalenttoc=(getchar()!=EOF)1.5CharacterInputandOutput
CharacterCountingVersion1 Version2++meansincrementbyone.++ncnc=nc+1#include<stdio.h>main(){longnc;nc=0;while(getchar()!=EOF)++nc;printf(“%ld\n”,nc);}#include<stdio.h>main(){doublenc;for(nc=0;getchar()!=EOF;++nc);printf(“%.0f\n”,nc);}1.5CharacterInputandOutputLineCounting#include<stdio.h> /*countlinesininput*/ main() { intc,nl; nl=0; while((c=getchar())!=EOF) if(c==‘\n’) ++nl; printf(“%d\n”,nl); }1.5CharacterInputandOutputWordCounting:#include<stdio.h> #defineIN1 /*insideaword*/ #defineOUT0 /*outsideaword*/ /*countlines,wordsandcharactersininput*/1.5CharacterInputandOutput
main() {
intc,nl,nw,nc,state; state=OUT;
nl=nw=nc=0;
while((c=getchar())!=EOF){ ++nc;
if(c==
‘\n’)++nl;
if(c==‘’||c==‘\n’||c==‘\t’) state=OUT; elseif(state==OUT){ state=IN;++nw; } }
printf(“%d%d%d\n”,nl,nw,nc); }1.6ArraysArrayExample:Tocountdigits,whitespaces,others.
#include<stdio.h>main(){
int
c,I,nwhite,nother;
intndigit[10];
nwhite=nother=0;
for(i=0;i<10;++i)
ndigit[i]=0;
while((c=getchar())!=EOF)
if(c>=‘0’&&c<=‘9’) ++ndigit[c-’0’]; elseif(c==‘’&&c==‘\n’||c==‘\t’) ++nwhite;
else ++nother;
printf(“digits=“);
for(i=0;i<10;i++)
printf(“%d”,ndigit[i]);
printf(“,whitespace=%d,other=%d\n”,nwhite,nother);}1.7FunctionsFunctiondefinitionform:
return-typefunction-name(parameterdeclarations,ifany) { declarations statements }1.7FunctionsExample:calln-thpower.#include<stdio.h>int
power(int
m,intn);main(){
inti;
for(i=0;i<10;i++)
printf(“%d%d%d\n”,i,power(2,i),
power(-3,i));
return0; }/*power:raisebaseton-thpower:n>=0*/intpower(intbase,intn){
inti,p; p=1;
for(i=1;i<=n;++i) p=p*base; returnp;}1.7Functions/*power:raisebaseton-thpower;n>=0*//* (old-styleversion) */power(base,n)intbase,n;{ inti,p; p=1; for(i=1;i<=n;++i) p=p*base; returnp;}1.8Arguments——callbyValueInC,allfunctionargumentsarepassed“byvalue”/*power:raisebaseton-thpower;n>=0;version2*/intpower(intbase,intn){ intp; for(p=1;n>0;--n) p=p*base; returnp;}1.9CharacterArraysExample:readsasetoftextlinesandprintsthelongestAlgorithm:Program:
while(there’sanotherline)
if(it’slongerthanthepreviouslongest) saveit saveitslengthprintlongestline#include<stdio.h>#defineMAXLINE1000/*maxinuminputlinesize*/int
getline(charline[],int
maxline);voidcopy(charto[],charfrom[]);main(){
int
len,max; charline[MAXLINE],longest[MAXLINE]; max=0; while((len=getline(line,MAXLINE))>0
if(len>max) {max=len;copy(longest,line);}
if(max>0)/*therewasaline*/
printf(“%s”,longes
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2025年度船舶建造与设计合同年度更新
- 2025年度跨境电商代理记账与税务合规支持协议
- 2025年度人工智能技术研发合作协议(全新版)
- 2025年度创意产业园区租赁合同及创业支持协议
- 2025年度租赁合同范本(含违约责任)
- 持续反馈机制的建立与实施计划
- 加强数据安全管理的实施措施计划
- 2025年CO2气体保护药芯焊丝合作协议书
- 定期举办学术交流活动计划
- 生产计划科学制定
- 12j912-2常用设备用房
- 《统计学》完整袁卫-贾俊平课件
- DTⅡ型固定式带式输送机设计选型手册
- GB/T 7701.2-2008煤质颗粒活性炭净化水用煤质颗粒活性炭
- GB/T 657-2011化学试剂四水合钼酸铵(钼酸铵)
- 橡胶坝工程施工质量验收评定表及填表说明编制于
- 抗日战争胜利题材话剧剧本范文
- GB/T 22328-2008动植物油脂1-单甘酯和游离甘油含量的测定
- 录用offer模板参考范本
- FZ/T 25001-1992工业用毛毡
- 儿童气管插管医学课件
评论
0/150
提交评论