32
Chapter 3 Operators and Expressions PROGRAMMING IN ANSI C

3 Operators and Expressions

Embed Size (px)

DESCRIPTION

idk

Citation preview

Page 1: 3 Operators and Expressions

Chapter 3Operators and Expressions

PROGRAMMING IN ANSI C

Page 2: 3 Operators and Expressions

04/18/23

2

Operators

Arithmetic + - * / % ++ --

Relational < <= > >= == !=

Logical ! && ||Bitwise << >> ~ | ^ &

Assignment

= (Shorthand Assignment: += -= etc.)

Comma , Explicit conversion

(type)

Conditional ?: Arrow ->

Pointer * & Array element [ ]

Number of bytes

sizeof others ( ) - .

Page 3: 3 Operators and Expressions

04/18/23

3

Expressions

An expression is a sequence of operands and

operators that produces a single value.

This value may be any data type except void.

Page 4: 3 Operators and Expressions

04/18/23

4

How to learn operators and expressions? When we study operators and expressions, we

must pay attention to such 5 points:

1. The function of operators.

2. The relation of operators and operands:

① How many operands does the operator need?

② Which types does the operator require?

3. The precedence of the operator.

4. The associativity of the operator.

5. The type of the calculated result.

Page 5: 3 Operators and Expressions

04/18/23

5

Arithmetic Operators & Expressions

+ : Addition or unary plus e.g. 3+2 , +3.5

- : Subtraction or unary minus e.g. 3-2 , -3.5

* : Multiplication e.g. 3*2 , 3.5*2

/ : Division e.g. 3/2 , 3.5/2 If the 2 operands both are integers, the result is an integer a

nd its fractional part is truncatedtruncated. e.g. 8/5=1

% : Modulo division e.g. 5%2 = 1 Both of the 2 operands must be integers integers .

The sign of the result is the same as the dividend.

5 % 2 =

-5 % 2 =

5 % -2 =

-5 % -2 =

5 % 1 =

5 % 1.0 =

5 / - 2 =

5 / - 2.0 =

- 2

- 2.5

1

- 1

1

- 1

0

Page 6: 3 Operators and Expressions

04/18/23

6

Arithmetic Operators & Expressions

+ , - , * , / , %

Precedence: + - (Unary) higher than * / % higher than + -

Associativity : + , - (Unary): Right to left others : Left to right

Page 7: 3 Operators and Expressions

04/18/23

7

Arithmetic Operators & Expressions

Notice :

If the operands are all integers, the result must be a

n integer.

If one of the operands is a real number, the result

must be a real number.

The modulo division operator cannot used on real

numbers but only integers, and the sign of result is

the same as the sign of dividend.

Page 8: 3 Operators and Expressions

04/18/23

8

Arithmetic Operators & Expressions

An arithmetic expression is a combination of

variables, constants, and operators arranged as

per the syntax of the language.

e.g. a * b / c + 1.5 – (3.28 + 'f') * (5 % 3)

Page 9: 3 Operators and Expressions

04/18/23

9

Arithmetic Operators & Expressions

You must pay attention to :

1. The expression “a multiplied by b” must be written to “a*b” but not “ab”;

2. C doesn’t 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;

3. Notice the precedence of those arithmetic operators, and you should reasonably use parentheses.

Page 10: 3 Operators and Expressions

04/18/23

10

Arithmetic Operators & Expressions

4. When expressions include real values, then it

is important to take necessary precautions to

guard against certain computational errors.

e. g. a = 1/3.0;

b = a * 3.0;

5. Don’t make any expression divide zero.

6. Avoid data overflow.

Page 11: 3 Operators and Expressions

04/18/23

11

Relational Operators & Expressions

< <= > >= == !=

Value of Relational Expression: 1(True) & 0(False)

Precedence : < <= > >= higher than == != Arithmetic operators higher than relational operators.

Associativity : Left to right

Relational operators are used in the test condition of

decision or loop statements to decide the course of ac

tion of running program.

Page 12: 3 Operators and Expressions

04/18/23

12

Relational Operators & Expressions

int a=3, b=2, c=1, d, f;

1. a > b

2. c == a > b

3. a < b + c

4. d = a > b

5. f = a > b > c

3>2 , the value is 1

1==1 , the value is 1

b+c=3 , the value is 0

d=1

a>b is 1 , 1>c is 0 , so: f = 0

Page 13: 3 Operators and Expressions

04/18/23

13

Relational Operators & Expressions

Notice

Avoid carrying out “==” and “!=” between real nu

mbers.

e.g. 1.0 / 3.0 * 3.0 == 1.0 /*not always 1 */

fabs ( 1.0 / 3.0 * 3.0 - 1.0 ) < 1e-6

Pay attention to distinguish “=” and “==”.

e.g. int a = 3, b = 2;

a = b == a; /* a = 0 */

Page 14: 3 Operators and Expressions

04/18/23

14

Logical Operators & Expressions

! && ||

Precedence : ! higher than relational operators higher than && higher than ||

Associativity : ! : Right to left

&& , ||: Left to right

Value of logical Expression: 1(True) & 0(False)

Operands: 0 denotes false, others denote true.

e.g. 3.0 && 0

a b !a a&&b a||b

0 0 1 0 0

0 1 1 0 1

1 0 0 0 1

1 1 0 1 1

Page 15: 3 Operators and Expressions

04/18/23

15

Logical Operators & Expressions

int a = 4, b = 5;

1. !a

2. a&&b

3. a||b

4. !a||b

5. 4&&0||2

6. 5>3&&0||8<4-!0

7. 'c'&&'d'

!4 => 0

4 && 5 => 1

4 || 5 => 1

0 || 5 => 1

0 || 2 => 1

((5>3)&&0)||(8<(4-(!0))) = 0||0

=> 0

, 99 || 100 => 1

Page 16: 3 Operators and Expressions

04/18/23

16

Logical Operators & Expressions

Question : Write the expression of leap year.If the “year” is a leap year, it must satisfy one of the 2 conditions:

1) It can be divided exactly by 4, and it can’t be divided exactly by 100.

2) It can be divided exactly by 400.

year%4==0 year%100!=0 year%400==0( && ) ||

Page 17: 3 Operators and Expressions

04/18/23

17

Logical Operators & Expressions

Short-circuit

When a logical expression is being evaluated,

it is not each part linked by && or || all is

evaluated. But only when it has to be

evaluated, it is done. a&&b /* Only when a is true, b is evaluated. If a is false, b is not evaluated.*/

a||b /* Only when a is false, b is evaluated. If a is true, b is not evaluated.*/

a=1; b=2; c=3; d=4; m=1; n=2;( m = a >b ) && ( n = c > d ); /* m=0,n=2*/

Page 18: 3 Operators and Expressions

04/18/23

18

Assignment Operators & Expressions

=

Format : variable = expression

Precedence : = lower than ||

Associativity : Right to left

a = b = 5;

int a = b = 5; Wrong!

int a = 5, b = 5; Right!

Page 19: 3 Operators and Expressions

04/18/23

19

Assignment Operators & Expressions

int a, b, c;

1. a = b = c = 5;

2. a = 5 + (c=6);

3. 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*/

Page 20: 3 Operators and Expressions

04/18/23

20

Assignment Operators & Expressions

“shorthand ” assignment operators: +=, -=, *=, /=, %=

variable op = expression;is equivalent to:

variable = variable op expression e.g.

x * = y + 1; is equivalent to:

x = x * (y + 1);

Page 21: 3 Operators and Expressions

04/18/23

21

Assignment Operators & Expressions

int a = 2;

a += a -= a*a ; a += a –= 4

a += (a = a – 4)

a += (a = -2)

a += a a = a + a

a = (-2) + (-2) a = -4

Page 22: 3 Operators and Expressions

04/18/23

22

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 to left

(10 / m) ++ Wrong!

10 / m ++ Right!

Page 23: 3 Operators and Expressions

04/18/23

23

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;

Page 24: 3 Operators and Expressions

04/18/23

24

Increment and Decrement Operators

1. j=3; k=++j;

2. j=3; k=j++;

3. j=3; printf("%d",++j);

4. j=3; printf("%d",j++);

5. a=3;b=5;c=(++a)*b;

6. a=3;b=5;c=(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

Page 25: 3 Operators and Expressions

04/18/23

25

Increment and Decrement Operators

Notice

The operand of ++ or -- must be a variable and

not any expression or any constant.

Page 26: 3 Operators and Expressions

04/18/23

26

Comma Operator & Expressions

expression 1, expression 2, ……, expression n

Value of Relational Expression:

the value of expression n.

Precedence : the lowest

Associativity : Left to right

Page 27: 3 Operators and Expressions

04/18/23

27

Comma Operator & Expressions

1. a = 3*4, 5*2 ;

2. b = (a = 3*4, 5*2) ;

3. 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

Page 28: 3 Operators and Expressions

04/18/23

28

Implicit Type Conversion

The rules of conversion:

P67

In all expressions except

assignments, any implicit

type conversions are made

from a lower size type to a

higher size type as shown

here:

short & char

int

unsigned int

long

unsigned long

float

double

long double

Page 29: 3 Operators and Expressions

04/18/23

29

Implicit Type Conversion

During assignment:

1. If expression is real type and the variable is integer

type, the value of expression will be truncated its

fractional part.

2. If expression is double type and the variable is float

type, the value of expression will be round its digits.

3. If expression is long type and the variable is int type,

the value of expression will be drop its higher byte.

Page 30: 3 Operators and Expressions

04/18/23

30

Implicit Type Conversion

int i, x; float f; double d; long L;

x = L / i + i * f -

d; long float

float

float

double

double

doubleint

Notice: in the whole 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.

Page 31: 3 Operators and Expressions

04/18/23

31

Explicit Type Conversion

Form : (type-name) expression

Precedence : 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, but all the types of variables are not converted. Of course, the values of these variables are not changed.

Page 32: 3 Operators and Expressions

04/18/23

32

Homework

Review Questions P76

3.1~3.8 & 3.10 write down in your exercise book

Programming Exercises