Sei sulla pagina 1di 32

Chapter 3

Operators and
Expressions
PROGRAMMING IN ANSI C

10/03/15

Operators
Arithmetic

Relational

<

<=

>

Logical

&&

||

Bitwise

<<

>>

Assignment

++

>=

==

!=

--

&

(Shorthand Assignment: += -= etc.)

Comma

Explicit conversion (type)

Conditional

?:

Arrow

->

Pointer

Array element

[]

others

() - .

&

Number of bytes sizeof

10/03/15

Expressions

An expression is a sequence of operands and


operators that produces a single value.

This value may be any data type except void.

10/03/15

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.


4

10/03/15

Arithmetic Operators & Expressions

+ Addition or unary plus

3+2 +3.5

-5 % 2e.g.
= - 1 3-2 -3.5
- Subtraction or unary minus
5 % -2 = 1
* Multiplication
e.g. 3*2 3.5*2
-5 % -2 = - 1
/ Division
e.g. 3/2 3.5/2
5%1= 0

5 % 2 e.g.
= 1

If the 2 operands both are integers,


5 % 1.0the
= result is an

integer and its fractional part5is/truncated.


truncated
- 2 = - 2 e.g. 8/5=1

% Modulo division

e.g. 5%2
5 /= -1 2.0 = - 2.5

Both of the 2 operands must be integers .

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


5

10/03/15

Arithmetic Operators & Expressions

+-*/%
Precedence:

+ - (Unary) higher than * / % higher than + Associativity

+ - (Unary): Right to left


others Left to right

10/03/15

Arithmetic Operators & Expressions

Notice
If

the operands are all integers, the result must

be an 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.
7

10/03/15

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)

10/03/15

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

10/03/15

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.

Dont make any expression divide zero.

6.

Avoid data overflow.

10

10/03/15

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
action of running program.
11

10/03/15

Relational Operators & Expressions


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

a > b 3>2 the value is 1

2.

c == a > b1==1 the value is 1

3.

a < b + c b+c=3 the value is 0

4.

d = a > b d=1

5.

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

12

10/03/15

Relational Operators & Expressions

Notice
Avoid

e.g.

carrying out == and != between real numbers.

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 */
13

10/03/15

Logical Operators & Expressions

&&

||

Precedence ! higher than

relational operators

higher than && higher than ||

Associativity
a

! Right to left
&& , ||: Left to right
!a
a&&b

a||b

Value
1(True)
0 of logical
0 Expression:
1
0 & 0(False)
0
0
1
1
0
1
Operands: 0 denotes false, others denote true.
1
0
0
0
1
e.g. 3.0 && 0
1
1
0
1
1
14

10/03/15

Logical Operators & Expressions


int a = 4, b = 5;
!4 => 0

1.

!a

2.

a&&b

4 && 5 => 1

3.

a||b

4 || 5 => 1

4.

!a||b

0 || 5 => 1

5.

4&&0||2

6.

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

7.

'c'&&'d'
,

0 || 2 => 1

99 || 100 => 1
15

10/03/15

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 cant be
divided exactly by 100.
2) It can be divided exactly by 400.
( year%4==0 && year%100!=0) || year%400==0

16

10/03/15

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*/
17

10/03/15

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!

18

10/03/15

Assignment Operators & Expressions


int a, b, c;
1.

2.

a = b = c = 5;
c = 5, b = c, a = b
a = 5 + (c=6);
c = 6, a = 5+c

3.

/*a=5, b=5, c=5*/

/*a=11, c=6*/

a = (b=4) + (c=6);
b = 4, c = 6, a = b+c

/*a=10, b=4, c=6*/

19

10/03/15

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

10/03/15

Assignment Operators & Expressions


int a = 2;
a += a -= a*a
;

a += a = 4

a += (a = a 4)

a += (a = -2)

a += a

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

a=a+a

21

10/03/15

Increment and Decrement Operators

++ -m ++; or
is equivalent to:
m --;
is equivalent to:

++ m;

(10 / m) ++

Wrong!

10 / m ++

Right!

m = m + 1;
or

-- m;
m = m - 1;

Precedence same as unary +, unary -, !

Associativity

Right to left

22

10/03/15

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;

23

10/03/15

Increment and Decrement Operators


1.

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

result k=4, j=4

2.

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

result k=3, j=4

3.

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

output:4

4.

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

output:3

5.
6.

a=a+1; c=a*b; result


a=3;b=5;c=(++a)*b;
a=4 c=20
a=3;b=5;c=(a++)*b;
c=a*b; a=a+1; result
a=4 c=15
24

10/03/15

Increment and Decrement Operators

Notice
The

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

any expression or any constant.

25

10/03/15

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

26

10/03/15

Comma Operator & Expressions


1.

a = 3*4, 5*2 ; a = 12

2.

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

3.

a=1; b=2; c=3;


printf("%d,%d,%d", a, b, c);output: 1, 2, 3
printf("%d,%d,%d", (a, b, c), b, c);
output: 3, 2, 3

27

10/03/15

Implicit Type Conversion

The rules of conversion: P67

long double
double

In all expressions except

float

assignments, any implicit type

unsigned long

conversions are made from a

long

lower size type to a higher


size type as shown here:

unsigned int
int
short & char
28

10/03/15

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

10/03/15

Implicit Type Conversion


int i, x; float f;
x

L / i

double d; long L;
+

d;
long
float
double
Notice: in the whole process of type conversion,
only the type
of the interim value used in
float
evaluation is converted, but all the types of
variables are not converted. float
double
Except the variable x assigned
a value, all the
values of other variables are not changed.
int
double
30

10/03/15

Explicit Type Conversion

Form (type-name) expression

Notice: like the implicit


Precedence same as unary +, unary -, !, ++, -type conversion, only the
main()
x=3.60, y=6.00
type of the interim value
{ Associativity Right to left
float x, y ;
used in evaluation is
e.g.
(int) (x+y) (int)converted,
x+y
but all the
x = 3.6 ;
types of variables are
y = (int) x * 2 ;
not converted.
Of course, the values of
printf("x=%.2f, y=%.2f", x, y);
these variables are not
}
changed.
31

10/03/15

Homework

Review Questions P76


3.1~3.8 & 3.10 write down in your exercise book

Programming Exercises

32

Potrebbero piacerti anche