




版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認領(lǐng)
文檔簡介
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浮點型 double雙精度浮點型 對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)作邏輯判斷時:非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. 本站所有資源如無特殊說明,都需要本地電腦安裝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. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 2025-2030年中國餐飲連鎖行業(yè)運營市場深度調(diào)查及投資策略研究報告
- 2025-2030年中國阿莫西林行業(yè)競爭現(xiàn)狀及投資戰(zhàn)略研究報告
- 2025-2030年中國鍍層鋼板市場運營態(tài)勢與發(fā)展風(fēng)險分析報告
- 2025-2030年中國酒石酸美托洛爾緩釋片行業(yè)發(fā)展趨勢及投資戰(zhàn)略研究報告
- 2025-2030年中國運動服飾行業(yè)運行現(xiàn)狀及發(fā)展前景趨勢分析報告
- 2025-2030年中國西廚設(shè)備行業(yè)市場發(fā)展現(xiàn)狀及前景趨勢分析報告
- 2025-2030年中國營養(yǎng)保健食品市場發(fā)展狀況及投資戰(zhàn)略研究報告
- 病人轉(zhuǎn)運合同范本
- 2025河北省安全員B證(項目經(jīng)理)考試題庫
- 2025年廣東省安全員知識題庫及答案
- 領(lǐng)導(dǎo)干部的國學(xué)修養(yǎng)講義
- 05-第三章-環(huán)境污染物的生物轉(zhuǎn)運和生物轉(zhuǎn)化-生物轉(zhuǎn)化幻燈片
- 公司精益改善項目推進管理制度及激勵方案
- 工科高等數(shù)學(xué)(下)知到章節(jié)答案智慧樹2023年上海海洋大學(xué)
- oppor11t刷全網(wǎng)通改全教程
- 兒童羽毛球教程
- 福建某機場二次雷達站基建工程施工組織設(shè)計
- 內(nèi)部控制-倉儲與存貨循環(huán)調(diào)查問卷
- 流程成熟度模型(PEMM)
- 高二英語期末考試試卷質(zhì)量分析報告
- 催化動力學(xué)分析法及其應(yīng)用
評論
0/150
提交評論