C Programming.ppt
《C Programming.ppt》由会员分享,可在线阅读,更多相关《C Programming.ppt(26页珍藏版)》请在麦多课文档分享上搜索。
1、C Programming,Lecture 5,Precedence and Associativity of Operators,Rules of associativity and precedence of operators determine precisely how expressions are operated. In the expression 1 + 2 * 3, the operator * has higher precedence than +, causing the multiplication to be performed first. The resul
2、t is 7 instead of 9.,Associativity of Operators,When two operators placed in proximity in an expression have the same precedence, their associativity is used to determine how the expression is evaluated. In the expression 6 / 2 * 3, both / and * have the same precedence. Since they both have left to
3、 right associa-tivity, the expression has the value 9 rather than 1.,Partial Table of Operator Precedence and Associativity,Operator Associativity,() + (postfix) - (postfix) left to right+(unary) -(unary) +(prefix) -(prefix) right to left* / % left to right+ - left to right= += -= right to leftOpera
4、tors on the top line have the highest precedence.Precedence decreases line-by-line going down the table.All operators on the same line have equal precedence.,Parentheses and the Order of Operations,Expressions inside parentheses are evaluated first. This provides for the use of parentheses to clarif
5、y or change the order in which operations are performed.1 + 2 * 3 has a value of 7.(1 + 2)* 3 has a value of 9.,Binary Plus versus Unary Plus,Both binary plus and unary plus are represented by a + (plus sign). The same is true of binary and unary - (the minus sign). Unary + and - have a higher prece
6、dence that binary + and - and the unary operators associate right-to-left instead of left-to-right.,Example of Unary Operators,In the expression - a * b - c the first minus is unary and the second is binary. We can use parentheses to write an equivalent expression that is less likely to be misinterp
7、reted.(- a) * b) - c,Example of Unary Operators Using Numbers,-1 * 2 - 3 has a value of -5 it is equivalent to(-1) * 2) - 3 or (-2) - 3which is -5 it is not equivalent to(-1) * (2 - 3) or (-1) * (-1)which is +1,Increment and Decrement Operators,+ (the increment operator) and - (the decrement operato
8、r) are unary operators with the same precedence and right-to-left associativity as the other unary operators. The + and - operators can occur in either a prefix or postfix position with different results.,Prefix versus Postfix When Using Increment and Decrement Operators,Each of the expressions +i a
9、nd i+ causes the stored value of i to be incremented by 1, however: The expression +i causes the stored value of i to be incremented first, with the expression then taking as its value the new stored value of i. The expression i+ has as its value the current value of i; then the stored value is incr
10、emented.,Example of the Increment and Decrement Operators,int a, b, c = 0; a = +c; b = c+; printf(“%d %d %dn”, a, b, +c);/* 1 1 3 is printed */c is incremented making its value 1.The result assigned to a making its value 1.The value of c is assigned to b making its value 1.Then c is incremented maki
11、ng its value 2.Finally, c is incremented before it is printed, making its value 3.,Practice with Operators and Expressions,Declarations and Initializations,int a = 1, b = 2, c = 3, d = 4;,Expression Equivalent expression Valuea * b / c (a * b) / c 0a * b % c + 1 (a * b) % c) + 1 3+a * b - c - (+a) *
- 1.请仔细阅读文档,确保文档完整性,对于不预览、不比对内容而直接下载带来的问题本站不予受理。
- 2.下载的文档,不会出现我们的网址水印。
- 3、该文档所得收入(下载+内容+预览)归上传者、原创作者;如果您是本文档原作者,请点此认领!既往收益都归您。
下载文档到电脑,查找使用更方便
2000 积分 0人已下载
下载 | 加入VIP,交流精品资源 |
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- CPROGRAMMINGPPT
