




版權(quán)說(shuō)明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡(jiǎn)介
Chapter2:Types,Operators,andExpressions2.1VariableNameletters(include‘_’)anddigits,beginwithletter.LowercaseanduppercaselettersaredistinctKeywordslikeif,else,intfloat,ect,arereserved.2.2DataTypesandSizes2.2DataTypesandStates basicdatatypesinC: char字符類型 int 整型 可加long,short: shortintsh; orshortsh; longintcounter; orlongcounter; float浮點(diǎn)型 double雙精度浮點(diǎn)型 對(duì)char,int可加unsigned; unsignedinti;2.3Constants int:123 long:12345678l,12345678L float(double):123.4 1.2e-2 char:‘a(chǎn)’,‘4’,‘\013’,‘\n’ stringconstant:“hello,world”
‘h’+’e’+’l’+’l’+’o’+’,’+’w’+’o’+’r’+’l’+’d’+’\0’strlen(s)intstrlen(chars[]){inti;i=0;while(s[i]!=‘\0’)++i;returni;}NOTICE!2.3Constants int:123 long:12345678l,12345678L float(double):123.4 1.2e-2 char:‘a(chǎn)’,‘4’,‘\013’,‘\n’
stringconstant:“hello,world”
enumerationconstant(枚舉常量):
enumboolean{NO,YES}; /*NO=0,YES=1*/
enumweeks{MON=1,TUE,WED,THU,FRI,SAT,SUN}2.3Constants int:123 long:12345678l,12345678L float(double):123.4 1.2e-2 char:‘a(chǎn)’,‘4’,‘\013’,‘\n’
stringconstant:“hello,world”
enumerationconstant(枚舉常量):
symbolicconstant #defineMAXLINE1000 #defineNO0 /*NO“;”,here*/ #defineYES1
2.4Declarations typevar1,var2,…,varN;avariablemayalsobeinitializedinitsdeclaration,e.g.; charesc=‘\\’; inti=0; intlimit=MAXLINE+1;thequalifierconstcanbeappiedtothedeclarationofanyvariabletospecifythatitsvaluewillnotbechanged.
constdoublee=2.71828; constcharmsg[]=“warning:”;intlower,upper,step;charc,line[1000];intlower;intupper;intstep;charc;charline[1000];2.5ArithmeticOperators
artithmeticoperators:+,-,*,/,% %:producestheremainder(求余),cannotbe appliedtofloat,double /:int/int=>int; 17/5=>3; 17.0/5=>3.4
if((year%4==0&&year%100!=0)||year%400==0)
printf(“%disaleapyear\n”,year); elseprintf(“%disnotaleapyear\n”,year); precedence: *,/,% high +,- low2.6relationalandLogicalOperatorsTherelationaloperators:>,>=,<,<=,==,!=Precedence:.*,/,%.+,-.>,>=,<,<=.==,!=.||logicaloperators:&&(與),||(或),!(非) i<lim-1&&(c=getchar())!=‘\n’&&c!=EOF (c=getchar())!=‘\n’ !valid<==>valid==0resultoflogicalexpression:
true----1;false----0;當(dāng)作邏輯判斷時(shí):非0----true;0---flase;2.7TypeConversionschar<shortint<int<unsigned<long<unsignedlong<float<doubleExample1:
int
atoi(chars[]) { inti,n; n=0;
for(i=0;s[i]>=‘0’&&s[i]<=‘9’;++i) n=10*n+(s[i]-‘0’); return0; }Example2:
int
lower(intc) { if(c>=‘A’&&c<=‘Z’) returnc+‘a(chǎn)’–’A’; elsereturnc; }2.7TypeConversionschar<shortint<int<unsigned<long<unsignedlong<float<doubletypeconversionstakeplacein: expresionwithmulti-kindsofdata assignments argumentpassing(functioncall) explicittypeconversion(forced); (type_name)expression unsignedlongintnext=1; intrand(void) { next=next*1103515245+12345;
return(unsignedint)(next/65536)%32768; } voidsrand(unsignedintseed) { next=seed; }2.8IncrementandDecrementOperators(incrementoperator)++:adds1toitsoperand(自增)(decrementoperator)--:substract1fromitsoperand(自減)
inti=3;
i++;/*i=i+1*/ i++and++iaredifferent(alsoi--,--i); Ifnis5,then x=n++; /*n=6,x=5*/ but:x=++n;/*n=6,x=6*/ voidsqueeze(chars[],intc) { inti,j; for(i=j=0;s[i]!=‘\0’;i++)
if(s[i]!=c)s[j++]=s[i];
s[j]=‘\0’; }if(s[i]!=c){s[j]=s[i];j++;} 2.8IncrementandDecrementOperatorsExample2:if(c==‘\n’){s[i]=c;++i;}
if(c==‘\n’)s[i++]=c;Example3:/*stract:concatettoendofs;smustbebigenough*/voidstrcat(chars[],chart[]){ inti,j; i=j=0; while(s[i]!=‘\0’)i++; while((s[i++]=t[j++])!=‘\0’);}2.9BitwiseOperatorsOperators:&:bitwiseAND|:bitwiseinclusiveOR^:bitwiseexeclusiveOR<<:leftshift>>:rightshift~:one’scomplement(unary)distinguish&/:from&&/||: x=1;y=2; x&y==>0; x&&y==>1;shiftoperatirs<<and>>:<<:fillwith0;>>:fillwith0(logicalshift)orwithsignbit(artithmeticshift)aba&ba|ba^b~a000001010111001101111002.9BitwiseOperatorsExample: /*getbits:getnbitsfrompositionp*/ unsignedgetbits(unsignedx,intp,intn) { return(x>>(p+1-n))&~(~0<<n); }2.10AssignmentOperatorsandExpressionsassignmentoperators: =,+=,-=,*=,/=,%=,<<=,>>=,&=,^=,|=
expr1op=expr2<==>expr1=(expr1)op(expr2)Example: /*bitcount:count1bitsinx*/ intbitcount(unsignedx) { intb; for(b=0;x!=0;x>>1) if(x&01)b++; returnb; }assignmentinexpression: while((c=getchar())!=EOF)…2.11ConditionalExpressionsexpr1?expr2:expr3
example: z=(a>b)?a:b; example2: for(i=0;i<n;i++)
printf(“%6d%c”,a[i],(i%10==9||i==n-1)?‘\n’:‘‘); /*printanarray,10perlin*/if(a>b)z=a;elsez=b;2.12PrecedenceandOrderofEvaluationOperatorsAssociativity
()[]->.!-++--+-*&sizeof righttoleft*/%+-<<>>><>=<===!=&^|&&||?:
溫馨提示
- 1. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒(méi)有圖紙預(yù)覽就沒(méi)有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。
最新文檔
- 2025年秋季滬科版七年級(jí)上學(xué)期數(shù)學(xué)課程實(shí)施計(jì)劃
- 時(shí)尚零售快時(shí)尚模式在2025年的品牌戰(zhàn)略與市場(chǎng)趨勢(shì)研究報(bào)告
- 營(yíng)養(yǎng)食品供應(yīng)鏈質(zhì)量控制分析-全面剖析
- 部編版小學(xué)三年級(jí)課后輔導(dǎo)教學(xué)計(jì)劃
- 2025幼兒園中班環(huán)保教育計(jì)劃
- 環(huán)保建材2025年建筑防水材料市場(chǎng)潛力評(píng)估報(bào)告
- 數(shù)字化社區(qū)零售:2025年業(yè)態(tài)創(chuàng)新與社區(qū)健康促進(jìn)報(bào)告
- 需經(jīng)批準(zhǔn)國(guó)有股權(quán)轉(zhuǎn)讓合同效力研究
- 新能源汽車充電基礎(chǔ)設(shè)施投資策略報(bào)告:2025年行業(yè)趨勢(shì)與投資策略
- 2025-2030年中國(guó)甜品行市場(chǎng)營(yíng)銷態(tài)勢(shì)分析與投資方向咨詢報(bào)告
- 培育五大可信數(shù)據(jù)空間
- (二調(diào))武漢市2025屆高中畢業(yè)生二月調(diào)研考試 語(yǔ)文試卷(含官方答案解析)
- 2025-2030年中國(guó)電力行業(yè)發(fā)展前景預(yù)測(cè)與投資戰(zhàn)略規(guī)劃分析報(bào)告
- 20《井岡翠竹》(+公開(kāi)課一等獎(jiǎng)創(chuàng)新教案)
- 2024年AI大模型產(chǎn)業(yè)發(fā)展與應(yīng)用研究報(bào)告
- Agent視域下的人工智能賦能作戰(zhàn)系統(tǒng)
- 2025年幼兒園家園共育工作計(jì)劃
- 2025年貴州銅仁市玉屏永昇國(guó)有資產(chǎn)投資管理有限公司招聘筆試參考題庫(kù)附帶答案詳解
- DBJ33T 1271-2022 建筑施工高處作業(yè)吊籃安全技術(shù)規(guī)程
- 基本藥物制度政策培訓(xùn)課件
- 大學(xué)生心理健康教育(山東聯(lián)盟)知到智慧樹(shù)章節(jié)測(cè)試課后答案2024年秋德州學(xué)院
評(píng)論
0/150
提交評(píng)論