Lecture 9-3 Operators
2
Objectives
At the end of this lecture, you will be
• able to use C operators
• able to evaluate C expressions
Read & watch
• 2.5—2.12 in English textbook
• Read C operators at
http://www.tutorialspoint.com/cprogrammin g/c_operators.htm
• Watch the video clips C programming tutorial 12—16
• Watch the video clip on arithmetic operators https://www.youtube.com/watch?v=vvpDbh qPrww&list=PL2_aWCzGMAwLSqGsERZ GXGkA5AfMhcknE&index=8
3
4
Operators and expressions
• Operators are used to form complicated
expressions (uttryck)
• An expression may have the following forms x op y
op x x op
primary expression
where x and y are called operand; op the operator
5
Operators and expressions
•
• • •
Primary expressions are the building blocks of more complex expressions.
– Constants, names, expression surrounded by parentheses primary expression examples
tall 381 2.5 “Hej” ( i + j ) Expression examples
i + j p = q ++ k k — r -= s There are various types of operators
– it is important to know the meaning and operation
– It is also necessary to know in which order the expression is evaluatedprecedence and associativity of operators
6
Arithmetic expression
• Formed by arithmetic operators • Arithmetic operators
1) +
2) –
3) *
4) /
5) +
6) –
7) ++
8) —
addition subtraction multiplication division unary plus unary minus increment decrement
7
Arithmetic expressions
• Examples
100 * antal i – 10 x – y x / y
– n -3.1415 + 6
• Evaluation
– before evaluation, the operands are converted automatically to the same data type
– Pay attention to the precedence and associativity of the operators
Type conversions
• Automatic conversion
– from ”narrower” operand to ”wider” operand
– f + i int operand is converted to float
• Helpful rules on conversions
8
• Forced conversion (cast) (type-name) expression
9
Integer division and modulus
• Integerdivision
in C, when 2 integers are divided, the result is integer ( the fraction part is discarded)
– Example
9 / 2 result is 4 9 /4 2
• Integer modulusfind the remainder – Examples
9 % 2 result is 1 9 % 7 2
• Compareintegerdivisionwithfloatingdivision – 9.0 / 2 4.5 Why?
10
About precedence and associativity
• precedence
– The order to evaluate the expression
– *, / and % have higher precedence than + and –
– parentheses are used to explicitly tell the precedence
• associativity
– when operators are of the same precedence, associativit tells the order to do the evaluation (from left or from right)
• Examples
1 + 2 * 3 / 4 – 5 -3 why? (1 + 2) * 3 / 4.0 -5 ?
11
Increment and decrement operators
• Increment ++
– add 1 to a variable
– antal = antal + 1 antal ++ or ++ antal
• decrement —
– subtract 1 from a variable
– rest = rest – 1 rest — or
— rest
• increment or decrment can be put before or after a variable name
++ antal (prefix) antal ++ (postfix) — antal (prefix) antal — (postfix)
12
Increment and decrement operators
• Prefix increment/decrement
– increment or decrement operator is before the operand
– The increment or decrement is done before its value is used
• Postfix increment/decrement
– increment or decrement operator is after the operand
– The increment or decrement is done after its value has been used
• Example
int i = 4, j = 7, a;
a = ++i * j –;
i is incremented by 1 first: i has a value of 5
j will be decremented after the evaluation of the expression a is assigned with 5 * 7 = 35; then j has a value of 6
Examples—arithmetic operators
• Arithmetic Operators in C (http://www.tutorialspoint.com/cprogrammin g/c_arithmetic_operators.htm)
• Study the example arithmetic-operators.c
13
Questions on expression and
operators
• How to write the following expressions in C? a2 + b3 + c2, (5 − 3x)(5 + 3x),
• Assume x=10, y=2, z=2;
what are the values in x, y and z ?
z=y=x++ + ++y∗2;
• Will 13/3/2 be the same as 13/(3/2) in C ?
14
15
Relational operators
• Relational and equality operators are used in selection and repetition structures to form complex program.
• The relational operators: > >= < <=
• they have the same precedence
• examples
X < y a > 3.5 i + j <= 4
16
Equality operators
• The equality operators: ==
!=
• they have the same precedence. But they have lower precedence than relational operators
• examples
x == y i != 0 a * b + c == x - y
17
Example on relational/equality operators • Study the example relational-operators.c
18
Logical operators
• Logical operators
&& -- logical AND ( OCH)
|| -- logical OR ( ELLER)
! -- logical negation ( ICKE)
• Examples
aktiv && varm aktiv || klar ! Aktiv temp < 100 || ! varm
Logical operators
• The numerical value of a relational or logical expression is 1 if the relation is true, and 0 if it is false.
• Nonzero is thought to be true, while zero is false
19
Examples – logical operators
• Study the example logical-operators.c at http://www.tutorialspoint.com/cprogrammin g/c_logical_operators.htm
20
21
Example on equality operators • Study the examples
– equality.c
22
logical operators—short-circuit
• When evaluating a C logical expression, the short- circuit is applied
• Short-circuit
– When the result of the expression can be obtained,
the evaluation of the expression stops
– Examples
1) If exp1 is false, then exp1 && exp2 will be false. So short-circuit means when exp1 is evaluated to be false, exp2 will not be evaluated.
2) If exp1 is true, then exp1 || exp2 will be true. So short-circuit means when exp1 is evaluated to be true, exp2 will not be evaluated
23
Bit operators
• Bit operators are low level operators and you can skip these, if you have not learnt number systems
• Bit level operators need to know the binary number system and internal computer representation of an integer
• A bit could 0 or 1
• Bit operators are applied to integer types
• Operators
~ negation (bit-wise) << shift-left
>> shift-right
& | ^
(OCH) AND (bit-wise) (Eller) OR (bit-wise)
exclusive OR (bit-wise)
Bit operators
• How to do the computation?
• Suppose a and b are bit operands:
24
About bit operators
• A good online source is
http://www.cprogramming.com/tutorial/bitw ise_operators.html (Bitwise Operators in C and C++: A Tutorial)
• Study the example program bit- operators.c
25
Assignment operators
• Used to assign a value to a variable (on the left)
• Simple assignment operator ( = ) – example
f = a*x + b;
• Otherassignmentoperators
+= -= *= /= %=<<=>>=&=|=^= – op1op=op2;op1=op1opop2
• Readalso
http://www.tutorialspoint.com/cprogramming/c_ass ignment_operators.htm (Assignment Operators in C)
• Studytheexampleassignment-operators.c 26
27
sizeof operators
• sizeof operator is used to find the memory size (in bytes) for a data object or type
• examples
– sizeof ( int ) gives the number of bytes used to represent an int value
– sizeof c gives the number of bytes used to represent the value of variable c.
Table of operators
• Important to know precedence and associativity of operators (page 53 in English textbook)
28