版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、Introduction to Computer(C Programming)Software College , Northeastern University2007,9Chapter3 Operators and Expressions 第1页,共36页。3.1 introduction C operators can be classified into a number of categories. They include: Arithmetic operators. Relational operators. Logical operators. Assignment opera
2、tors. Increment and decrement operators. Conditional operators. Bitwise operators. Special operators.第2页,共36页。3.2 Arithmetic operators Operator Meaning Addition or unary plus Subtraction or unary minus Multiplication Division Modulo division Examples:a-b, a+b, a*b, a/b, a%b第3页,共36页。Example 3.1The Pr
3、ogram shows the use of integer arithmetic to convert a given number of days into months and days.main()int months, days; printf(“Enter daysn”); scanf(“%d”, &days); months = days/30; days = days % 30; printf(“Months=%d days=%d”, months, days);OutputEnter days265Months=8 Days=25Enter days364Months=12
4、Days=4Enter days45Months=1 Days=15第4页,共36页。3.3 Relational OperatorsC supports six relational operators in all: Operators Meaning is less than is greater than = is greater than or equal to = is equal to != is not equal toExample4.5=10 4.5=010 is complement of = = = is complement of !=We can simplify
5、an expression involving the not and the less than operators using the complements as shown below: Actual one Simplified one !(x=y !(xy) x=y !(x!=y) x=y !(xy !(x=y) x 55 & salary 1000) 2. if (number 1000) 第8页,共36页。3.5 Assignment OperatorsC has a set of shorthand operators of the form: v op =exp; wher
6、e v is a variable, exp is an expression op is a C binary arithmetic operator op= is known as the shorthand assignment operator v op =exp; is equivalent to v =v op (exp);Examples: x+=y+1; is equivalent to x=x+(y+1); x+=3; is equivalent to x=x+3;第9页,共36页。Shorthand Assignment Operators Statement with s
7、imple Statement with assignment operator shorthand operator a = a + 1 a += 1 a = a 1 a -= 1 a = a * (n+1) a *= n+1 a = a / (n+1) a /= n+1 a = a % b a %= b第10页,共36页。The use of shorthand assignment operators has three advantages:What appears on the left-hand side need not be repeated and therefore it
8、becomes easier to write.The statement is more concise and easier to readThe statement is more efficientExample: value(5*j-2) = value(5*j-2) + delta; With the help of the += operator, this can be written as follows: value(5*j-2) += delta;第11页,共36页。Example 3.2The Program prints a sequence of squares o
9、f numbers. Note the use of the shorthand operator *=.#define N 100#define A 2main()int a; a = A; while(a b) ? a : b; if (a b) x = a; else x = b;第15页,共36页。3.8 Bitwise OperatorC has a distinction of supporting special operators known as bitwise operators for manipulation of data at bit level. These op
10、erators are used for testing the bits, or shifting them right or left. Bitwise operators may not applied to float or double. Operator Meaning & bitwise AND | bitwise OR bitwise exclusive OR shift right第16页,共36页。3.9 Special OperatorsPointer Operators: & and *Member Selection Operators: . and -第17页,共3
11、6页。The Comma Operator: can be used to link the related expression together. A comma-linked list of expressions are evaluated left to right and the value of right-most expression is the value of the combined expression.Example: value = (x = 10, y = 5, x + y); value = 15;Some applications of comma ope
12、rator are: In for loops: for ( n = 1, m = 10, n d)?1:0);printf(“%dn”, (cd)?1:0);Output a=16 b=10 c=6 a=16 b=11 d=26 a/b = 1 a%b = 5 a*=b =176 0 1第20页,共36页。An arithmetic expression of variables, constants, and operators arranged as per the syntax of the language. The complex mathematical expressions
13、are expressed by C expressions as follows: Algebraic expression C expression a * b c (m+n) * (x+y) a * b / c 3 * x * x + 2 * x + 1 x / y + c 3.10 Arithmetic Expressions第21页,共36页。3.11 Evaluation of ExpressionsExpressions are evaluated using an assignment statement of the form variable = expression; V
14、ariable is any C variable name. When the statement is encountered first and the result then replaces the previous value of the variable on the left-hand side. All variables used in the expression must be assigned values before evaluation is attempted.Example x = a * b c; y = b / c * a; z = a b / c +
15、 d;第22页,共36页。Example 3.4The program illustrates the use of variables in expressions and their evaluation.main()float a, b, c, x, y, z; a = 9; b = 12; c = 3; x = a b / 3 + c * 2 - 1; y = a b / (3 + c) * (2 - 1); z = a (b / (3 + c) * 2) 1;printf(“x=%fn”,x); printf(“y=%fn”,y); printf(“z=%fn”,z);Output
16、x=10.000000 y=7.000000 z=4.000000第23页,共36页。3.12 Precedence of Arithmetic OperatorsAn arithmetic expression without parentheses will be evaluated from left to right using the rules of precedence of operators.Examplex = a-b/3+c*2-1When a = 9, b = 12, and c = 3 x = 9-12/3+3*2-1;第24页,共36页。Rules for Eval
17、uation of ExpressionFirst, parenthesized sub expression from left to right are evaluated.If parentheses are nested, the evaluation begins with the innermost sub-expression.The precedence rule is applied in determining the order of application of operators in evaluation sub-expressions.The associativ
18、ity rule is applied when two or more operators of the same precedence level appear in a sub-expression.Arithmetic expressions are evaluated from left to right using the rules of precedence.When parentheses are used, the expressions within parentheses assume highest priority.第25页,共36页。3.13 Some Compu
19、tational ProblemsApproximate values for real numbersa = 1.0 / 3.0;b = a * 3.0;Division by zeroAvoid overflow or underflow errors第26页,共36页。Example 3.5The program shows round-off errors that can occur in computation of floating point numbers.main()float sum, n, term; int count = 1; sum = 0; printf(“En
20、ter value of nn”);scanf(“%f”,&n); term = 1.0/n; while( count = n)sum = sum +term;count+; printf(“Sum = %fn”, sum);Output Enter value of n 99 Sum = 1.000001 Enter value of n 143 Sum = 0.999999第27页,共36页。3.14 Type Conversions in ExpressionsImplicit Type ConversionC permits mixing of constants and varia
21、bles of different types in an expression. C automatically converts any intermediate values to the proper type so that the expression can be evaluated without loosing any significance. This automatic conversion is known as implicit type conversion.第28页,共36页。int i,x,y;float f;double d;long int l; x =
22、l / i + i * f - d longlongfloatfloatfloatfloatdoubledoubleint第29页,共36页。Conversion HierarchyNote that, C uses the rule that, in all expressions except assignments, any implicit type conversions are made from a lower size type to a higher size type as shown below:shortcharint unsigned int long int uns
23、igned long intfloatdoublelong doubleConversionHierarchy第30页,共36页。Explicit ConversionThe general form of a cast is :(type-name) expressionWhere type-name is one of the standard C data types. The expression may be a constant, variable or an expression. Examplesx = (int) 7.5 a = (int) 21.3/(int)4.5b =
24、(doublt)sum/ny = (int)(a+b)z = (int)a+bp = cos(double)x) 第31页,共36页。Example 3.6The program shows how to use a cast to evaluate the equation sum = (1/i)main()float sum;int n; sum = 0;for( n=1; n=10; +n)sum = sum +1/(float)n;printf(“%2d %6.4fn”, n, sum); Output 1 1.0000 2 1.5000 3 1.8333 4 2.0833 5 2.2833 6 2.4500 7 2.5929 8 2.7179 9 2.8290 10 2.9290第32页,共36页。3.15 Operator Precedence and AssociativityEach operator in C has a precedence associated with it. This precedence is used to determine how an expression involving more than one ope
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 重庆人文科技学院《算法设计与分析实验》2022-2023学年期末试卷
- 重庆人文科技学院《机械设计》2021-2022学年第一学期期末试卷
- 重庆财经学院《移动应用程序开发》2022-2023学年期末试卷
- 重庆三峡学院《教师与学生生涯规划》2022-2023学年第一学期期末试卷
- 重庆三峡学院《机械制造技术基础》2023-2024学年第一学期期末试卷
- 重庆三峡学院《工程测量》2021-2022学年第一学期期末试卷
- 重庆三峡学院《道路勘测设计》2023-2024学年第一学期期末试卷
- 重庆人文科技学院《人力资源管理综合模拟实训三模拟集体谈判》2022-2023学年第一学期期末试卷
- 茶具放置设计方案
- 茶业品牌案例研究报告
- 2024-2030年中国风电运维行业发展现状规划分析报告
- 统编版(2024)七年级上册道德与法治第三单元《珍爱我们的生命》测试卷(含答案)
- 实用针灸学-经络养生与康复-暨南大学中国大学mooc课后章节答案期末考试题库2023年
- 入团志愿书(2016版本)(可编辑打印标准A4) (1)
- 106kW水冷式管壳冷凝器 设计说明书
- 宝石类采样规范手册
- 航海模型教学设计和计划
- 第三方安全检查报告模板
- 公司内部市场化实施方案
- 浙江省公路山岭隧道机械化装备应用指导手册
- 医师定期考核简易程序练习及答案
评论
0/150
提交评论