EE1005-L04-C Operators
EE1005-L04-C Operators
EE1005-L04-C Operators
Thinking to Programming
Lesson 4
C Operators
(Reference: Harry H. Cheng, Chapter 4)
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;
convert direction
4-7
Conversion by assignment
dnum = inum;
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.
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
Similarly,
c = c-1; c -= 1; c--;
4-13
c++, c--, ++c, --c
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)
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 /
== != from left
|| from left
?: from right
Consider 15%6*2
Clearer to write it as
(15%6)*2
= 3*2 = 6
4-21
Example (right-associative)
Consider x = y += z -= 4
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