




版權說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權,請進行舉報或認領
文檔簡介
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. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
- 4. 未經(jīng)權益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責。
- 6. 下載文件中如有侵權或不適當內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 財務公司內(nèi)部保密及競業(yè)禁止合同
- 采煤隊安全生產(chǎn)責任與承包管理合同
- 休閑飲品店品牌區(qū)域獨家代理合同
- 財務軟件遠程監(jiān)控與故障響應服務協(xié)議
- 校長在教師暑期培訓中的講話
- 酒店周邊旅游路線服務企業(yè)制定與實施新質(zhì)生產(chǎn)力項目商業(yè)計劃書
- 在線教育在線平臺行業(yè)跨境出海項目商業(yè)計劃書
- 醫(yī)療器械回收與再利用企業(yè)制定與實施新質(zhì)生產(chǎn)力項目商業(yè)計劃書
- 交通工程設計在線平臺行業(yè)跨境出海項目商業(yè)計劃書
- 上海市飼料買賣合同4篇
- 100以內(nèi)三數(shù)加減法混合運算綜合練習習題大全附答案
- 壓力管道年度檢查報告(空白)
- DB52∕T 1067-2015 地理標志產(chǎn)品 興仁薏(苡)仁米
- 設備采購運輸安裝調(diào)試售后服務方案投標方案
- 高速列車傾斜控制系統(tǒng)分析與綜合設計
- 中藥藥劑學智慧樹知到期末考試答案章節(jié)答案2024年湖南中醫(yī)藥大學
- 電纜橋架技術規(guī)范
- 肝硬化門靜脈高壓食管胃靜脈曲張出血的防治指南( 2022)
- 初中英語《反義疑問句》優(yōu)質(zhì)課件
- 農(nóng)田水利學專業(yè)課程設計
- 子宮脫垂病例護理討論
評論
0/150
提交評論