




版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認領(lǐng)
文檔簡介
TheCProgrammingLanguage典型程序設(shè)計語言的排行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“l(fā)ow-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. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 度生產(chǎn)加工合同
- 牛仔布供需合同
- 再生廢物原料國外裝運前檢驗合同全文
- 租賃合同范本:辦公場地篇
- 新版買賣合同模板
- 14《天文學(xué)上的曠世之爭》教學(xué)設(shè)計 2023-2024學(xué)年統(tǒng)編版高中語文選擇性必修下冊
- 度醫(yī)院護士勞動合同
- 5《七律·長征》教學(xué)設(shè)計-2024-2025學(xué)年六年級語文上冊統(tǒng)編版
- 企業(yè)戰(zhàn)略聯(lián)盟合同樣本
- 1《春夏秋冬》教學(xué)設(shè)計-2024-2025學(xué)年語文一年級下冊統(tǒng)編版
- 基坑監(jiān)測總結(jié)報告
- 2024年華師大版九年級數(shù)學(xué)下冊全冊教案
- 合肥市廬陽區(qū)雙崗街道社區(qū)工作者招聘考試試題及答案2024
- JBT 106-2024 閥門的標(biāo)志和涂裝(正式版)
- 煤礦技術(shù)員必須會的知識
- (高清版)JTGT 3650-01-2022 公路橋梁施工監(jiān)控技術(shù)規(guī)程
- 北京市2024小升初數(shù)學(xué)模擬試卷一
- 一年級口算題100以內(nèi)比大小
- 《提案與方案優(yōu)化設(shè)計》課件-第一部分 常見戶型問題解析及平面布局優(yōu)化
- 產(chǎn)科抗磷脂綜合征診斷與處理專家共識
- MOOC 跨文化交際通識通論-揚州大學(xué) 中國大學(xué)慕課答案
評論
0/150
提交評論