0% found this document useful (0 votes)
12 views28 pages

4.chapter4 Expressions WF

The document summarizes operators and expressions in C programming. It covers arithmetic, assignment, increment and decrement operators. It discusses operator precedence and associativity. Compound assignment operators are also introduced which allow updating a variable value in one step, like i+=2. The increment and decrement operators ++ and -- can be used as prefix or postfix operators, with prefix changing the value immediately and postfix using the original value first before changing it.

Uploaded by

behzad.samnokia
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views28 pages

4.chapter4 Expressions WF

The document summarizes operators and expressions in C programming. It covers arithmetic, assignment, increment and decrement operators. It discusses operator precedence and associativity. Compound assignment operators are also introduced which allow updating a variable value in one step, like i+=2. The increment and decrement operators ++ and -- can be used as prefix or postfix operators, with prefix changing the value immediately and postfix using the original value first before changing it.

Uploaded by

behzad.samnokia
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 28

Programing in C

Chapter 4 Expressions
运算符和表达式

Fang Wang
Outlines

• 4.1 Arithmetic Operators 算术运算符

• 4.2 Assignment Operators 赋值运算符

• 4.3 Increment and Decrement Operators 自增自减运算

• 4.4 Expression Evaluation 表达式

• 4.5 Expression Statement 表达式语句

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

Precedence ( 操作符优先级 ) 先乘除后加

i + j * k mean “add i and j, then multiply


sult by k” or “multiply j and k, then add i”?
olution to this problem is to add parentheses,
g either (i + j) * k or i + (j * k).
parentheses are omitted, C uses operator
dence rules to determine the meaning of the
sion.

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));

– Watch out for unexpected results in chained assignments as a


result of type conversion:
int i;
double f;

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

• The ++ operator adds 1 to its operand.


• The -- operator subtracts 1 from its operand.

– They can be used as prefix operators (++i and –-i) or


postfix operators (i++ and i--).

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

• How much later?


– The C standard doesn’t specify a precise time, but it’s safe to
assume that i will be incremented before the next
statement is executed.“ 后”到什么时候?通常执行下一句
前应该已经 自增自减过了。
23
4.3 Increment and Decrement
Operators
• Evaluating the expression ++i (a “pre-increment”)
yields i + 1 and—as a side effect—increments i:
int i = 1;
printf("i is %d\n", ++i); /* prints "i is 2" */
printf("i is %d\n", i); /* prints "i is 2" */
• Evaluating the expression i++ (a “post-increment”)
produces the result i, but causes i to be
incremented afterwards:
int i = 2;
printf("i is %d\n", i++); /* prints "i is 2" */
printf("i is %d\n", i); /* prints "i is 3" */
C Programming Tutorial - 24 - Increment
Operator
24
4.3 Increment and Decrement
Operators
• The -- operator has similar properties:
int i = 1;

printf("i is %d\n", --i); /* prints "i is 0" */

printf("i is %d\n", i); /* prints "i is 0" */

int i = 1;

printf("i is %d\n", i--); /* prints "i is 1" */

printf("i is %d\n", i); /* prints "i is 0" */

25
4.4 Expression Evaluation 表达式求值
• Table of operators discussed so far:

Precedence Name Symbol(s) Associativity


1 increment (postfix) ++ left
decrement (postfix) --
2 increment (prefix) ++ right
decrement (prefix) --
unary plus +
unary minus -
3 multiplicative * / % left
4 additive + - left
5 assignment = *= /= %= += -= right

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

You might also like