You are on page 1of 24

EE1005 From Computational

Thinking to Programming

Lesson 4
C Operators
(Reference: Harry H. Cheng, Chapter 4)

Dr. Tan Chee Wah, Wesley (CoPaCE/School of EEE)


Email: wesleytan@ntu.edu.sg
Office: S1-B1b-54
Phone: 6790 6009

“Be Prepared, Give Feedback”


4-1
What are C operators?
A C operator is a symbol (e.g. +, -) that instructs C
to perform a specific action on one or more objects
(which can be simple variables) known as operands.

C has a large collection of operators:


• Assignment operators
• Arithmetic operators
• Cast operators
• Increment/decrement operators
• Relational operators (Lesson 6: Decision Making)
• Logical operators (Lesson 6: Decision Making)
• Bitwise operators (not covered in this course)
4-2
The Assignment Operator (=)
The assignment operator (=) assigns the value of the
right-hand side expression to the variable on the left.
int a = 2, b = 3;
a = a + b;//assign sum of a(=2) and b(=3) to a

before 2 3

a b

after 5 3
a=2+3 a b 4-3
No change
Arithmetic Operators
These are: + - * / %
2+3=5 operands: 2, 3
+ is a binary operator (It acts on 2 operands).
When used for changing the sign of a number or a
variable, - is a unary operator. For example, -2
% is the modulus (remainder) operator.
5%2 = 1 //get remainder of 5 divided by 2
It is a useful operator. For example, the value of
num%2 will tell us whether the integer num is even
(remainder is 0) or odd (remainder is 1).
4-4
Program 4.1 (Use of % operator)
/* Checks whether an integer is even or odd */
#include <stdio.h>
int main(void)
{ int number;
== checks for
printf("Enter an integer: "); equality
scanf("%d", &number);
if (number%2 == 0) //if remainder is 0
printf("The integer is even.\n");
else //if remainder is 1
printf("The integer is odd.\n");

return 0;
}
4-5
Type Promotion (mixed-mode calculation)

Suppose we have
int inum; double dnum;

and we wish to calculate inum/dnum. Notice


that this is a mixed-mode calculation involving
two different data types (int and double).
In this case, a copy of inum will be “promoted”
to type double first before the division is carried
out. Variable inum remains as int.
inum/dnum  1/2.0  1.0/2.0  0.5
4-6
Type Promotion (mixed-mode calculation)

In a mixed-mode calculation, the following


promotion order (left to right) is used to convert
a copy of the value of a variable lower in the
order to one of the higher data type.

Promotion order: Lowest on the left


char, int, long, float, double

convert direction
4-7
Conversion by assignment

double dnum; int inum;

(1) Suppose inum = 5 and we have the


assignment:

dnum = inum;

The result is as expected: dnum = 5.0, a double


number, and there is no loss of precision.

4-8
Conversion by assignment
(2) Suppose dnum = 5.8 and we have the
assignment:

inum = dnum;
The result is now: inum = 5, an integer number.
The fractional part is lost, so there is a loss of
precision. We have to be careful about this.
However, this fact can also be usefully employed
in some programming problems, as in to
purposely get the whole number part.
Note that dnum still remains as 5.8
4-9
Typecast
A typecast uses the cast operator to explicitly
control data type conversions.

If inum is of type int, (float)inum ‘casts’


inum to type float. We also have
(double)inum and (int)dnum.

Example: x = 7, y = 5
x/y (both int) results in 1 whereas
(double)x/y (double divide by int) gives 1.4
Similarly, x/(double)y (int divide by double)
gives 1.4 4-10
Arithmetic Assignment Operators

The statement
a = a + b;
can be abbreviated as
a += b;
+= is the addition assignment operator.
We may also combine = with -, *, /, %

4-11
a += b; a = a + b;
a -= b; a = a - b;
a *= b; a = a * b;
a /= b; a = a / b;
a %= b; a = a % b;

Note that

a /= b+c; means a = a/(b+c);

a %= b+c; means a = a%(b+c);


4-12
Increment & Decrement Operators
(++, --)

Adding or subtracting 1 to/from a variable is a


very common operation in programming.

To do this, we have 3 forms:


c = c+1; c += 1; c++;

Similarly,
c = c-1; c -= 1; c--;

4-13
c++, c--, ++c, --c

c++ Post-increment (increment last)

c-- Post-decrement (decrement last)

++c Pre-increment (increment first)

--c Pre-decrement (decrement first)


Differences?

4-14
Example (Pre-decrement):
Suppose c = 1;
and result = --c;
c is decremented first (c becomes 0) before it is
assigned to result (also 0 now). We get
c = 0 and result = 0

result = --c;
is equivalent to two statements:
c = c – 1; //Pre-operation (decr)
result = c; //Main operation
4-15
Example (Post-decrement):
Suppose c = 1;
and result = c--;
c (still 1) is assigned to result (also 1) before it is
decremented (c becomes 0 now), so
c = 0 and result = 1

result = c--;
is equivalent to two statements:
result = c; //Main operation
c = c – 1;//Post-operation (decr)
4-16
c++, c--, ++c, --c
Contents of Expression Contents of Value of
c before c after Expression
1 c++ 2 1 (incr last)
1 ++c 2 2 (incr first)
1 c-- 0 1 (decr last)
1 --c 0 0 (decr first)

Note the last column gives the values of the


expressions (c++, etc) which are used in the
example below:
printf("%d", <expression>);
where <expression> can be c++, ++c, c--, --c
Note: printf("%d", c); is 4-17

main operation
Examples
What will be shown on screen after running :
Print first, then results on c value
(post) increase screen
int c=1, y; 1
printf("%d\n", c++); 1 2
printf("%d\n", ++c); 3 3
y = 5 + c++;
printf("%d\n", y); 8 4
(Pre) Increase
first, then print Add (c=3) to 5 and assign to
y (becomes 8), then (post)
increase c (from 3 to 4)
4-18
Precedence & Associativity of
Operators
Precedence is the order in which operations are
performed.
a + b/c Division before addition because / at
higher precedence level wrt +
(a+b)/c Addition before division because addition
in()at higher precedence level wrt /

Associativity refers to left-to-right or right-to-left


evaluations of expressions when two or more
operators at the same precedence level appear in the
same expression.

See next slide for Order of Precedence for Operators


4-19
Precedence and Associativity Table
(Selected Operators Only)
Operator (Precedence: High to Low) Associativity
() from left

++ -- ! &(address of) *(dereference) cast from right


unary -
* (multiplication) / (division) % (modulus) from left

+ (addition) - (subtraction) from left

< <= > >= from left

== != from left

&& from left

|| from left
?: from right

Assignment: = += -= *= /= %= from right


4-20
Example (left-associative)

Consider 15%6*2

% and * are at the same precedence level.


Evaluation goes from left to right (left-
associative).

15%6 = 3, then 3*2 = 6

Clearer to write it as
(15%6)*2
= 3*2 = 6
4-21
Example (right-associative)
Consider x = y += z -= 4

=, += and -= are at the same precedence level.


Evaluation goes from right to left as shown
below:

x = y += z -= 4
x = y += (z -= 4) [Do z = z-4 first]
x = (y += (z -= 4))[Then y = y+z]
(x = (y += (z -= 4))) [Lastly x = y]

Assuming y = 0, z = 0; x = ? 4-22
Example
int x =5, y = 5, z= 5

x %= y + z
What is the value of x after
running the program?
Ans:
Because, + precedence level is higher than %=,
so, x %=(y + z)
%=(5 + 5)
%=10
=5%10 = 5
4-23
Summary
1. Assignment (=)

2. Arithmetic (+ - * / %)

3. Mixed-mode

4. Conversion by Assignment

5. Typecast

6. Arithmetic assignment (+= -= *= /= %=)

7. Increment, decrement (++, --)


• Difference between Post and Pre operations

8. Precedence & Associativity of Operators 4-24

You might also like