




版權(quán)說(shuō)明:本文檔由用戶(hù)提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡(jiǎn)介
1、Chapter 3Operators and Expressions,PROGRAMMING IN ANSI C,Operators,Expressions,An expression is a sequence of operands and operators that produces a single value. This value may be any data type except void.,How to learn operators and expressions?,When we study operators and expressions, we must pay
2、 attention to such 5 points: The function of operators. The relation of operators and operands: How many operands does the operator need? Which types does the operator require? The precedence of the operator. The associativity of the operator. The type of the calculated result.,Arithmetic Operators
3、C doesnt supply exponential operator, so you can write some multiplication, or you can use mathematic function pow(x,y) in the C function library to express xy; Notice the precedence of those arithmetic operators, and you should reasonably use parentheses.,Arithmetic Operators b = a * 3.0; Dont make
4、 any expression divide zero. Avoid data overflow.,Relational Operators a b c = a b a b f = a b c,32,the value is 1 1=1, the value is 1 b+c=3, the value is 0 d=1 ab is 1,1c is 0,so: f = 0,Relational Operators a = b = a;/* a = 0 */,Logical Operators !a aint a = b = 5;Wrong!int a = 5, b = 5;Right!,Assi
5、gnment Operators a = b = c = 5; a = 5 + (c=6); a = (b=4) + (c=6);, c = 5, b = c, a = b /*a=5, b=5, c=5*/ c = 6, a = 5+c /*a=11, c=6*/ b = 4, c = 6, a = b+c /*a=10, b=4, c=6*/,Assignment Operators is equivalent to:variable = variable op expression e.g.x * = y + 1; is equivalent to:x = x * (y + 1);,As
6、signment Operators a += a -= a*a ;, a += a = 4 a += (a = a 4) a += (a = -2) a += a a = a + a a = (-2) + (-2) a = -4,Increment and Decrement Operators,+- m +; or + m; is equivalent to: m = m + 1; m -; or - m; is equivalent to: m = m - 1; Precedence : same as unary +, unary -, ! Associativity: Right t
7、o left,(10 / m) + Wrong! 10 / m + Right!,Increment and Decrement Operators,int m=5, n; n = 10 / m+; is equivalent to: n = 10/m; m+; n = 10 / +m; is equivalent to: +m;n = 10/m;,Increment and Decrement Operators,j=3; k=+j; j=3; k=j+; j=3; printf(%d,+j); j=3; printf(%d,j+); a=3;b=5;c=(+a)*b; a=3;b=5;c=
8、(a+)*b;,j=j+1; k=j; result:k=4, j=4 k=j; j=j+1; result :k=3, j=4 j=j+1; printf(); output:4 printf(); j=j+1; output:3 a=a+1; c=a*b; result :a=4,c=20 c=a*b; a=a+1; result :a=4,c=15,Increment and Decrement Operators,Notice The operand of + or - must be a variable and not any expression or any constant.
9、,Comma Operator b = (a = 3*4, 5*2) ; a=1; b=2; c=3; printf(%d,%d,%d, a, b, c); printf(%d,%d,%d, (a, b, c), b, c);,a = 12 a = 12, b = 10 output: 1, 2, 3 output: 3, 2, 3,Implicit Type Conversion,The rules of conversion: P67In all expressions except assignments, any implicit type conversions are made f
10、rom a lower size type to a higher size type as shown here:,Implicit Type Conversion,During assignment: If expression is real type and the variable is integer type, the value of expression will be truncated its fractional part. If expression is double type and the variable is float type, the value of
11、 expression will be round its digits. If expression is long type and the variable is int type, the value of expression will be drop its higher byte.,Implicit Type Conversion,int i, x;float f;double d;long L; x = L / i + i * f - d;,long,float,float,float,double,double,double,int,Notice: in the whole
12、process of type conversion, only the type of the interim value used in evaluation is converted, but all the types of variables are not converted. Except the variable “x” assigned a value, all the values of other variables are not changed.,Explicit Type Conversion,Form :(type-name) expression Precede
13、nce : same as unary +, unary -, !, +, - Associativity: Right to left e.g. (int) (x+y) (int) x + y,main() float x, y ; x = 3.6 ; y = (int) x * 2 ; printf(x=%.2f, y=%.2f, x, y); ,x=3.60, y=6.00,Notice: like the implicit type conversion, only the type of the interim value used in evaluation is converted, bu
溫馨提示
- 1. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶(hù)所有。
- 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ì)用戶(hù)上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶(hù)上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶(hù)因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。
最新文檔
- 詐騙對(duì)公賬戶(hù)管理辦法
- 廣電行業(yè)統(tǒng)計(jì)管理辦法
- 福建建設(shè)動(dòng)態(tài)管理辦法
- 肥胖課件下載
- 高二數(shù)學(xué)導(dǎo)數(shù)數(shù)學(xué)試卷
- 分班考數(shù)學(xué)試卷
- 二中廣雅初三數(shù)學(xué)試卷
- 二數(shù)下數(shù)學(xué)試卷
- 廣安市2024年二診數(shù)學(xué)試卷
- 2025年04月浙江省衢州市衢江區(qū)衛(wèi)生健康系統(tǒng)招引高層次緊缺人才27人筆試歷年專(zhuān)業(yè)考點(diǎn)(難、易錯(cuò)點(diǎn))附帶答案詳解
- 2024年高等教育文學(xué)類(lèi)自考-00730中外音樂(lè)史歷年高頻考點(diǎn)試卷專(zhuān)家薈萃含答案
- 2024屆四川涼山州數(shù)學(xué)高二第二學(xué)期期末考試試題含解析
- 2023全新包干制物業(yè)服務(wù)合同
- 外賣(mài)運(yùn)營(yíng)培訓(xùn)手冊(cè)
- 鋁壓延加工材項(xiàng)目評(píng)估報(bào)告
- 多學(xué)科治療協(xié)作模式
- (環(huán)境管理)環(huán)境保護(hù)與水土保持監(jiān)理實(shí)施細(xì)則
- 國(guó)際稅收稅收管轄權(quán)
- 云南省昆明市官渡區(qū)2022-2023學(xué)年七年級(jí)下學(xué)期期末語(yǔ)文試題(含答案)
- yamaha貼片機(jī)操作規(guī)程
- 標(biāo)準(zhǔn)化知識(shí)競(jìng)賽考試題庫(kù)(含答案)
評(píng)論
0/150
提交評(píng)論