02b. Operators
02b. Operators
Outline
1. Operators
2. Arithmetic Operators
3. Operator Precedence and Associativity
4. Expressions
5. Different Forms of Assignment Operators
6. Increment and Decrement Operators
7. Swapping Values between Two Variables
8. Some uses of Integer Division and Modulus
Operators
2
1. Operators
• Operator – a symbol or keyword that represents an operation to
be applied to some data, yielding a value.
E.g. varA = -varB + 40 * 20;
• We use operators all the time in real life. You should all be
familiar with Binary operators and Unary operators in basic
arithmetic. You can recognize them intuitively in C:
– Binary operator – an operator that accepts 2 operands
E.g. 40 * 20 x - 7.3 a = 5
– Unary operator – an operator that accepts 1 operand
E.g. -10 +10
3
1. Operators
• Basic arithmetic operators in C:
– e.g., + - * / %
• There are other types of operators you will learn later:
– Relational Operators, e.g., < <= == >= > !=
– Logical Operators, e.g., && || !
– Assignment Operators, e.g., = += *= &=
– Increment and Decrement Operators, e.g., ++ --
– Bitwise Operators, e.g., & | ~ ^
– Comma Operator, Parentheses, Conditional Operator,
Member Operator, Pointer Operators, …
– Most Binary, some Unary and even Ternary
4
2. Arithmetic Operators
Operator Description Example
+ Addition 8 + 5 13
- Subtraction (a binary operator) 8 - 5 3
Don’t type
* Multiplication 8 * 5 40
⨉
or write
5
2. Arithmetic Operators
• When used as an unary operator, '-' represents the
minus operator, which turns a positive value into its
negative counterpart and vice versa, i.e., additive
inverse.
e.g.: foo = 5;
bar = -foo; // Assign -5 to
bar
Exercise: evaluate the following expressions
• 20 % 3
• 2 % 9
• 30 / 20 / 2
• 10 * 2 + 4 * 3
6
3. Operator Precedence & Associativity
• How should we evaluate the following expression? i.e.,
in what order should the operators be applied?
- 2 - 25 / 10 + 33 * 2
- 2 - 25 / 10 + 33 % 10 * 2
8
3. Operator Precedence & Associativity
• Given the limited amount of operators we have learned in C so far, this
table summarizes their operator precedence and operator associativity
Operators Associativity Precedence
+ (unary plus) - (unary minus) Right to Left Highest
* / % Left to right
+ (addition) - (subtraction) Left to right
= (assignment) Right to Left Lowest
• -2 - 25 / 10 + 33 % 10 * 2 = ?
9
3.1. Parentheses
• Use parentheses '(' and ')' to explicitly specify the
evaluation order of sub-expressions
(a + b) * (c + d)
10
4. Expressions
• An expression is a combination of operators, constants,
variables, and function calls
– e.g.: 30
24 + a
d = b * b - 4 * a * c
sqrt(4.0) + a * sqrt(9.0)
• An expression
– Can always be evaluated to a value (of some data type)
– Can be part of another expression
11
5. Assignment Operators
variable = expression
• Low precedence, right-to-left associativity
12
1 int a = 0, b = 2, c;
Equivalent to
2 double pi = 3.1416;
int a, b, c;
3
double pi;
Assignment operator can be used to initialize a = 0;
variables in variable declaration. b = 2;
pi = 3.1416;
1 int a = 0;
2 a = a + 2;
3 printf("%d", a); // What's the
4 output?
13
1 int a = 1, b = 2;
2 b = b * a;
3 a = 0;
4 printf("%d", b); // What's the output?
1 int b, c, d;
2 d = c = b = 0; // Assign 0 to variables b, c, and
3 d
4 // d = c = b = 0 is evaluated as d = (c = (b = 0))
14
5.1. Assignment Operators – Short Form
• i = i + 2; can be written as i += 2;
• The semantics of
variable = variable op (expression);
is equivalent to
variable op= expression;
a = b; // Method A ?
b = a;
tmp = a; // Method B ?
a = b;
b = tmp;
tmp = a; // Method C ?
b = tmp;
a = b;
Answer: Method B
Dry run the code segments and you’ll know
why! 17
Summary
• Arithmetic operators (+, -, *, /, %)
18
Appendix (Optional Topics)
• Difference between ++i (prefix) and i++ (postfix)
Increment operators
19
More on Increment Operator
• The increment operator (++) can be placed in either prefix
or postfix position, with different results.
20
More on Increment Operator
Statement that involves ++ operator Equivalent statements
k = ++i * 2; i = i + 1; // side effect
// prefix increment of first
i k = i * 2; // NEW value of
i*2
k = i++ * 2; k = i * 2; // OLD value of
// postfix increment i*2
of i i = i + 1; // side effect
last
printf("%d\n", ++k); k = k + 1; // side effect
// prefix increment of first
k printf("%d\n", k); // NEW k
printf("%d\n", k++); printf("%d\n", k); // OLD k
// postfix increment k = k + 1; // side effect
of k last
21
More on Increment Operator
• Example
1 int i, k;
2 i = 0;
1
3 k = ++i; 1
4 printf("%d\n", i); 2
5 printf("%d\n", k);
6 printf("%d\n", ++k);
1 int i, k;
2 i = 0;
3 k = i++; 1
0
4 printf("%d\n", i);
0
5 printf("%d\n", k);
6 printf("%d\n", k++);
22
Some uses of Integer Division and Modulus Operators
Suppose n is an integer
• (n % 10) yields the right most digit of n
e.g.: 1234 % 10 4
24