4.chapter4 Expressions WF
4.chapter4 Expressions WF
Chapter 4 Expressions
运算符和表达式
Fang Wang
Outlines
2
Operators
• Expressions are built from variables,
constants, and operators. Each expression has
its own type and value.
• C has a rich collection of operators, including
– arithmetic operators
– relational operators
– logical operators
– assignment operators
– increment and decrement operators
and many others
3
4.1 Arithmetic Operators
• Unary Arithmetic Operators 一元算术运算符
– There are also two unary arithmetic operators:
+ unary plus
- unary minus
– The unary operators require one operand:
i = +1;
j = -i;
4
4.1 Arithmetic Operators
• Binary Arithmetic Operators 二元算术运算符
• C provides five binary arithmetic operators:
+ addition
- subtraction
* multiplication
/ division
% remainder ( 整数相除取余数 )
– Remainder operator
– The value of i % j is the remainder when i is divided
by j.
– i and j must be integer 整数
10 % 3 has the value 1, and 10 % 5 has the value 0.
5
4.1 Arithmetic Operators
• Binary Arithmetic Operators 混合运算
– Binary arithmetic operators (with the exception of
% remainder) allow either integer or floating-point
operands, with mixing allowed.
– When int and double operands are mixed, the
result has type double.
9 + 2.5 has the value 11.5, and 6.7 / 2 has the
value 3.35.
整型数据和实型数据混合运算,结果为实型
整型和整型运算,结果为整型
实型和实型运算,结果为实型
6
4.1 Arithmetic Operators
• Binary Arithmetic Operators
– The / and % operators require special care:
• When both operands are integers, / “truncates” the
result.
• The value of 1 / 2 is 0, not 0.5.
• The value of 1 .0/ 2 is 0.5.
• The % operator requires integer operands;
• In C99, the value of i % j has the same sign as i.
• --5%3 -2
• --5%-3 -2
7
4.1 Arithmetic Operators
8
4.1 Arithmetic Operators
• Operator Precedence ( 操作符优先级 )
– The arithmetic operators have the following
relative precedence:
Highest: + - (unary) 正负号
* / %
Lowest: + - (binary)
– Examples:
i + j * k is equivalent to i + (j * k)
-i * -j is equivalent to (-i) * (-j)
+i + j / k is equivalent to (+i) + (j / k)
9
4.1 Arithmetic Operators
• Operator Associativity ( 操作符结合律,有左结合和右结合两种)
– Associativity comes into play when an expression contains two or
more operators with equal precedence.
– An operator is said to be left associative ( 左 结 合 ) if it groups
from left to right.
• The binary arithmetic operators (*, /, %, +, and -) are all left
associative, so
i - j – k is equivalent to (i - j) - k
i * j / k is equivalent to (i * j) / k
– An operator is right associative (右结合) if it groups from right
to left.
• The unary arithmetic operators (+ and -) are both right
associative, so
- + i is equivalent to -(+i)
当表达式中有多个运算符时,先看优先级,如优先级相同,则看结合律,左结
合就从左向右算,右结合就从右向左算。只有少数运算符是右结合的。
Learning to Program in C (Part 03)(000033000-000507046) 10
4.2 Assignment Operators 赋值运算符
• Simple assignment: used for storing a value into a
variable
1. Compound assignment: used for updating a value
already stored in a variable
11
4.2 Assignment Operators 赋值运算符
• Simple Assignment: v=e 变量 = 常数、变量、表达式
….
– The effect of the assignment v = e is to calculate the
expression e and copy its value into v.
– e can be a constant, a variable, or a more
complicated expression:
i = 5; /* i is now 5 */
j = i; /* j is now 5 */
k = 10 * i + j; /* k is now 55 */
12
4.2 Assignment Operators
– If v and e don’t have the same type, then the
value of e is converted to the type of v as the
assignment takes place:
int i;
double f;
i = 72.99; /* i is now 72 */
f = 136; /* f is now 136.0 */
• Side Effects
– An operators that modifies one of its operands is
said to have a side effect .
– The simple assignment operator has a side effect: it
modifies its left operand.
13
4.2 Assignment Operators
• Side Effects
– Since assignment is an operator, several assignments can be
chained together:
i = j = k = 0;
– The = operator is right associative, so this assignment is
equivalent to
i = (j = (k = 0));
f = i = 33.3;
– i is assigned the value 33, then f is assigned 33.0 (not 33.3).
14
4.2 Assignment Operators
• Simple Assignment
– In many programming languages, assignment is a
statement;
– in C, however, assignment is an operator, just like +.
– v=e is an expression.
– The value of an assignment v = e is the value of v
after the assignment.
• 赋值表达式的值就是左边变量的值。
15
4.2 Assignment Operators
• Side Effects
– An assignment of the form v = e is allowed wherever
a value of type v would be permitted:
i = 1;
k = 1 + (j = i);
printf("%d %d %d\n", i, j, k);
/* prints 1 1 2 */
• “ 赋值表达式的值就是左边变量的值。
16
4.2 Assignment Operators
• Lvalues 左值
– The assignment operator requires an lvalue as its
left operand.
– An lvalue represents an object stored in computer
memory, not a constant or the result of a
computation.
– Variables are lvalues;
– Variables =…..
– 等号的左边只能是变量
17
4.2 Assignment Operators
• Lvalues
– Since the assignment operator requires an lvalue as
its left operand, it’s illegal to put any other kind of
expression on the left side of an assignment
expression:
12 = i; /*** WRONG ***/
i + j = 0; /*** WRONG ***/
-i = j; /*** WRONG ***/
– The compiler will produce an error message such as
“invalid lvalue in assignment.”
• 等号的左边只能是变量
18
4.2 Assignment Operators
• Compound Assignment ( 复合赋值 )
– Assignments that use the old value of a variable to
compute its new value are common.
– Example:
i = i + 2;
– Using the += compound assignment operator, we
simply write:
i += 2; /* same as i = i + 2; */
19
4.2 Assignment Operators
• Compound Assignment
– There are nine other compound assignment operators,
including the following:
-= *= /= %=
– All compound assignment operators work in much the same
way:
v += e adds v to e, storing the result in v
v -= e subtracts e from v, storing the result in v
v *= e multiplies v by e, storing the result in v
v /= e divides v by e, storing the result in v
v %= e computes the remainder when v is divided by e,
storing the result in v Learning to Program in C (Part
03)(000508555-000554000)
20
4.2 Assignment Operators
• Compound Assignment
– One problem is operator precedence:
– i *= j + k isn’t the same as i = i * j + k.
– i =i*( j + k )
– Similar remarks apply to the other compound
assignment operators.
21
4.3 Increment and Decrement
Operators
• C provides special ++ (increment) and -- (decrement)
operators. 自增自减运算符—自增自减 1
22
4.3 Increment and Decrement
Operators
• ++i (a “pre-increment”) means “increment i
immediately ,then use the new value of i ”
• i++ (a “post-increment”) means “use the old value of i
for now, but increment i later.”
• 加号在前 : 先加,再使用 i
• 加号在后 : 后加,先使用 i
int i = 1;
25
4.4 Expression Evaluation 表达式求值
• Table of operators discussed so far:
26
4.5 Expression Statements
• C has the unusual rule that any expression can be used as
a statement. (adding a semicolon at the end of an
expression) 表达式加上分号就是表达式语句
• Example:
++i;
27
The End
28