1.2.6數(shù)據(jù)類型與表達(dá)式 - 數(shù)據(jù)類型與表達(dá)式1.2_第1頁(yè)
1.2.6數(shù)據(jù)類型與表達(dá)式 - 數(shù)據(jù)類型與表達(dá)式1.2_第2頁(yè)
1.2.6數(shù)據(jù)類型與表達(dá)式 - 數(shù)據(jù)類型與表達(dá)式1.2_第3頁(yè)
1.2.6數(shù)據(jù)類型與表達(dá)式 - 數(shù)據(jù)類型與表達(dá)式1.2_第4頁(yè)
1.2.6數(shù)據(jù)類型與表達(dá)式 - 數(shù)據(jù)類型與表達(dá)式1.2_第5頁(yè)
已閱讀5頁(yè),還剩13頁(yè)未讀 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(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ì)自己和他人造成任何形式的傷害或損失。

評(píng)論

0/150

提交評(píng)論