版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
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. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 中职计算机应用基础教案
- 荷花淀(网友来稿) 教案教学设计
- 2024中长期借款合同
- 2023-2024学年江苏省常州一中高一数学第二学期期末教学质量检测模拟试题含解析
- 工商注销合同范本
- 赣州舞蹈教室租赁合同
- 保险销售人员劳动合同
- 全新广告制作设计服务合同
- 办公文具长期采购合同范本
- 2024事业单位的劳动合同书
- 锅炉原理智慧树知到期末考试答案2024年
- 供电企业舆情的预防及处置
- Altium-Designer-19原理图与PCB设计完整全套教案课件教学电子课件
- 2024年山东省临沂市中考模拟化学试题(附答案)
- 两轮自平衡电动车悬架的设计
- 2023-2024学年广东省广州市广雅、省实、二中、六中、执信五校高二(上)期末物理试卷(含解析)
- 2023年小学五年级语文下册名校期末基础测评卷【附答案】
- “一带一路”战略背景下广东外贸发展路径研究
- 离职协议书范本(餐饮业)
- 2024年国家税务总局部分直属事业单位招聘30人高频考题难、易错点模拟试题(共500题)附带答案详解
- 2024四川成都市科学技术局事业单位招聘16名工作人员历年高频考题难、易错点模拟试题(共500题)附带答案详解
评论
0/150
提交评论