programming in ANSI C-Chapter 3 Operators and Expressions_第1頁(yè)
programming in ANSI C-Chapter 3 Operators and Expressions_第2頁(yè)
programming in ANSI C-Chapter 3 Operators and Expressions_第3頁(yè)
programming in ANSI C-Chapter 3 Operators and Expressions_第4頁(yè)
programming in ANSI C-Chapter 3 Operators and Expressions_第5頁(yè)
已閱讀5頁(yè),還剩27頁(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)介

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)益歸上傳用戶所有。
  • 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)論