




版權(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;當作邏輯判斷時:非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)容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責。
- 6. 下載文件中如有侵權(quán)或不適當內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 電子商務平臺用戶行為心理學應用考核試卷
- 貨運物流標準化與統(tǒng)一編碼體系構(gòu)建考核試卷
- 再生材料在建筑節(jié)能中的應用考核試卷
- 合作伙伴關(guān)系風險管理考核試卷
- 印刷品裁剪技術(shù)考核試卷
- 急需流動資金借款合同(32篇)
- 企業(yè)管理咨詢業(yè)務合作協(xié)議書
- 《于丹趣品人生》讀書筆記
- 四川省眉山市2024-2025學年下學期期末考試八年級數(shù)學試卷及答案
- 夢幻迪士尼派對活動方案
- 2025年江西省社區(qū)工作者招聘考試試卷
- 2025-2030中國鋼制車輪行業(yè)競爭格局與盈利前景預測報告
- 【人教版】北京西城2024-2025學年 四年級下學期期末數(shù)學試題【三】有解析
- miRNA與心血管疾病
- 醫(yī)院五年建設發(fā)展規(guī)劃(2025年)
- 湖湘文化課件
- 基礎寫作的試題及答案
- 2025至2030年中國自動售水機行業(yè)市場需求分析及發(fā)展趨勢分析報告
- 2024-2025學年統(tǒng)編版小學語文六年級下冊教學工作總結(jié)(共三套)
- 儲備糧庫消防培訓課件
- T/CI 475-2024廚余垃圾廢水處理工程技術(shù)規(guī)范
評論
0/150
提交評論