You are on page 1of 21

Lesson Plan

Title: Operators in C Programming Ref. No: Lecture


12
Target Population: CSC 103 Duration: 60 min
Aims: To introduce students to operators in C programming.

Learning outcomes: At the end of the lecture, the students will have a
knowledge of different types of operators used in C programming,
operator’s precedence and associativity.

Content Method/ Resource or Time


Technique Aid
Introduction: Attendance, Lecture; Q/A White board, 10 min
Rapport building, Summary Multimedia
of the previous lecture by a projector
student, Questions from the
previous class, Pre-
assessment, Lecture outline
Development: Lecture, Q/A, White board, 40 min
1. Definitions of operators Discussion Multimedia
2. Different types of operators used projector
in C programming
3. Arithmetic operators.
4. Explanation of operators’
precedence and associativity
with examples.
Conclusion: Lecture, Q/A White board, 10 min
1. Recap of main points Multimedia
2. Students’ Feedback & Answer projector
3. Assessment of students’
Learning Outcomes
4. References/Suggested Reading
(Lecture note) and Forward
planning for next lecture

2
Operators in C Programming
• Operators are the symbols which operate on
values or variables. For example: + is an
operator to perform addition.
• C programming language has wide range of
operators to perform various operations. For
better understanding of operators, these
operators can be classified as:
 Arithmetic Operators
 Increment and Decrement Operators
 Assignment Operators
 Relational Operators
 Logical Operators
 Conditional Operators
 Bitwise Operators
 Special Operators
Arithmetic Operators
Operator Meaning of operator
+ addition or unary plus
- subtraction or  unary minus
* multiplication
/ division
% remainder after division
(modulo division)

Note: % operator can only be used with integers.


Arithmetic Operators
Operator Meaning of Example Result
Operator
+ Addition 67+5 72
- Subtraction 67.0-4.9 62.1
* Multiplication 25*10 250
/ Division 5/2 2
% Remainder 5%2 1
Remainder Operator
• Remainder operator (%) can only be used with
integers. It is very useful in programming.
For example, an even number %2 is always 0
and an odd number % 2 is always 1. So you
can use this property to determine whether a
number is even or odd.
Precedence of Operators
• If more than one operators are involved in an
expression then, C language has predefined
rule of priority of operators. This rule of
priority of operators is called operator
precedence.
 If you don't explicitly state the order in which an
expression is evaluated, they are evaluated based
on the operator precedence. For example, in the
statement "4+2*8", the 2 will first be multiplied
by 8 and then the result will be added to 4. This is
because the "*" has a higher precedence than the
"+". To avoid ambiguity in reading the program, it
is recommended that statement is written as
"4+(2*8)". The order of evaluation can be
controlled through placement of parenthesis in
the code. A table of operator precedence follows
below:
/*The highest precedence is at the top of the list
and the lowest is at the bottom*/

• Multiplicative: *, /, %
• Additive: +, -
• Relational: <, >, <=, >=
• Equality: ==, !=
• Logical AND: &&
• Logical OR: ||
• Assignment: =, +=, -=, *=, /=, %=
• In C, precedence of arithmetic operators
(*,%,/,+,-) is higher than relational
operators(==,!=,>,<,>=,<=) and precedence of
relational operator is higher than logical
operators(&&, || and !). Suppose an
expression:
(a>b+c&&d)
This expression is equivalent to:
((a>(b+c))&&d)
i.e, (b+c) executes first
then, (a>(b+c)) executes
then, (a>(b+c))&&d) executes
• Operator precedence is why the expression 5 + 3 *
2 is calculated as 5 + (3 * 2), giving 11, and not as (5 +
3) * 2, giving 16.
• We say that the multiplication operator (*) has
higher "precedence" or "priority" than the addition
operator (+), so the multiplication must be
performed first.

Mnemonic: PEMDAS (an acronym for the words


parenthesis, exponents, multiplication, division,
addition, subtraction. Given two or more operations
in a single expression, the order of the letters in
PEMDAS tells you what to calculate first, second,
third and so on, until the calculation is complete).
Associativity of operators
Associativity indicates in which order two
operators of same precedence(priority)
executes.
Operator associativity is why the expression 8 - 3 - 2 is
calculated as (8 - 3) - 2, giving 3, and not as 8 - (3 - 2),
giving 7.
We say that the subtraction operator (-) is "left
associative", so the left subtraction must be performed
first. When we can't decide by operator precedence
alone in which order to calculate an expression, we
must use associativity.
Since the operators + and - have the same
precedence, and are left-associative, the
following expressions are all equivalent:
x+3-y+5
= (x + 3) - y + 5
= ((x + 3) - y) + 5
= ((x + 3) - y + 5)
• For example:
 4+5*6 evaluates to 34, because the * 
operator has precedence over the + operator,
and so the expression is evaluated as 
4 + (5*6), not (4+5)*6.
• Since the operators =, += and -= have the
same precedence, and are right-associative,
the following expressions are all equivalent:
x = y += z -= 4
x = y += (z -= 4)
x = (y += (z -= 4))
(x = y += (z -= 4))
Examples:

This expression is evaluated in this order


1+2*2 1+(2*2)
1+2*2*4 1+((2*2)*4)
(1+2)*2*4 ((1+2)*2)*4
Applying the operator precedence and
associativity rule, the expression
3+4*4>5*(4+3)-1 is evaluated as follows:

3+4*4>5*(4+3)-1 (1) Inside parenthesis first


= 3+4*4>5*7-1 (2) Multiplication
= 3+16>5*7-1 (3) Multiplication
= 3+16>35-1 (4) Addition
= 19>35-1 (5) Subtraction
=19>34 False (6) Greater than
Evaluate:

1. 10 ÷ 2 + 12 ÷ 2 × 3 =
2. 7+3×5=
3. 7+3×4÷2−5×6=
4. (7 +3 ) x 4 / 2 - 5 x 6=
5. 3+4×5=
6. 4 × 32 =
7. [4 − 1 + (5 + 3)] =
8. 15 − 12 × 2/(12/3) + 22=
9. (2 + 6)/4 × 3 =
Answers
1. 23
2. 22
3. -17
4. -10
5. 23
6. 36
7. 11
8. 13
9. 6

You might also like