




版權說明:本文檔由用戶提供并上傳,收益歸屬內容提供方,若內容存在侵權,請進行舉報或認領
文檔簡介
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’,‘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’,‘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’,‘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’–’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. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯系上傳者。文件的所有權益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網頁內容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
- 4. 未經權益所有人同意不得將文件中的內容挪作商業或盈利用途。
- 5. 人人文庫網僅提供信息存儲空間,僅對用戶上傳內容的表現方式做保護處理,對用戶上傳分享的文檔內容本身不做任何修改或編輯,并不能對任何下載內容負責。
- 6. 下載文件中如有侵權或不適當內容,請與我們聯系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 2025建筑工程勘察設計合同 標準版模板
- 自家房屋轉租合同
- 2025有關權益保障反擔保合同范本
- 電子設備購銷合同
- 個人購房合同范文
- 2025年度勞動合同范本模板
- 鏟車出售協議書模板
- 房產收取鑰匙協議書
- 2025年03月河北唐山市曹妃甸區公開選聘高層次專業技術人員15名筆試歷年典型考題(歷年真題考點)解題思路附帶答案詳解
- 2025年03月揚州市廣陵區事業單位工作人員35人筆試歷年典型考題(歷年真題考點)解題思路附帶答案詳解
- 人教五四 六年級 下冊 語文 第五單元《中國有能力解決好吃飯問題 第一課時》課件
- 北京中考語文常考知識點(積累背誦)-2025年北京中考語文二輪復習
- 2025年企業市場推廣服務合同模板
- 2025年新高考政治二輪復習:高分答題模版(含練習題及答案)
- 充電樁行業趨勢及市場前景分析報告
- 2025年吉林鐵道職業技術學院單招職業技能測試題庫1套
- 發生在肺內的氣體交換 課件-2024-2025學年人教版生物七年級下冊
- 奉化市體育特長生初中升高中排球專業考試評分標準
- 回收電纜知識培訓課件
- (二調)武漢市2025屆高中畢業生二月調研考試 語文試卷(含官方答案解析)
- 污水處理廠運營委托合同
評論
0/150
提交評論