Thanks to
Nakib Hayat Chowdhury
Operators and Expression
Operators and Operands
• Arithmetic Operator
• Relational Operators
• Logical Operators
• Assignment Operators and
• Unary Operators
• Conditional Operators
• Bitwise Operators
• Special Operators
The data items that operators act upon, are called
operands.
Arithmetic Operators
• There are five arithmetic operators in C. They are
Operator Purpose
+ Addition
- Subtraction
* Multiplication
/ Division
% Reminder after integer division
• There is no exponentiation operator in C.
• There is a library function (POW) under math.h to
carry out exponentiation
Arithmetic Operators (cont.)
• The operands acted upon by arithmetic operators
must represent numeric values.
• Thus, the operands can be integer quantities,
floating-point quantities or characters
• Character constant represent integer value
Arithmetic Operators (cont.)
• The remainder operator (%) requires that both
operands be integers and the second operand be
nonzero.
• Similarly, the division operator (/) requires that the
second operand be nonzero.
a+b 13
a-b 7
a*b 30
a/b 3 Here, the decimal portion of the
quotient will be dropped
a%b 1
a+b 14.5
a-b 10.5
a*b 25.0
a/b 6.25
a%b Not Possible!!!
a 65
a+b 131
a+1 66
a + ‘A’ 130
a + ‘1’ 114
int a, b;
a = 11;
b = -3;
Follow
basic
rules of
a+b 8 algebra
a-b 14
a*b -33
a/b -3
a%b 2 Ignore - !!!
Type Convention
• Operands that differ in type may undergo type
conversion before the expression takes on its final
value.
• In general, the final result will be expressed in the
highest precision possible, consistent with the data
types of the operands.
• Rules apply when neither operand is unsigned.
int i = 7; ASCII Value
float f = 5.5; w = 119
char c = ‘w’ 0 = 48
i+f 12.5 float (double)
i+c 126 integer
i + c – ‘0’ 78 integer
(i + c) - (2 * f / 5) 123.8 float (double)
• Type conversion
Type Convention
• If the two operands in assignment expression are of
different data types.
• The value of right hand operand will automatically
be converted to the type of the operand on the left.
• The entire assignment expression will be then same
data type.
float
Left
int 11.995
Right
float
int
3 float
Left
3.0
int
11
;
Type Cast
• To transform the type of a variable temporarily.
• To do so, the expression must be preceded by the
name of the desired data type, enclosed in
parentheses
(data type) expression
int number;
(float) number;
Valid or Invalid?
i = 7;
f = 8.5;
result = (i + f) % 4; Invalid
• Type conversion 2
Valid or Invalid?
float num = 10.5;
num % 2;
float num = 10.5;
((int)num) % 2;
Relational Operators
• Use to compare two values.
Operator Meaning
< is less than
<= is less than or equal to
> is greater than
>= is greater than or equal to
== is equal to
!= is not equal to
Relational Operators…
• These six operators are used to form logical
expressions, which represent conditions that are
either true or false.
• The resulting expressions will be of type integer
• True is represented by the integer value 1
• False is represented by the value 0
Relational Operators
Example Description
A<B True if A is less than B else False
A <= B True if A is less than or equal B else False
A>B True if A is greater than B else False
A >= B True if A is greater than or equal B else False
A == B True if A is equals to B else False
A != B True if A is equals to B else False
i=1
True or False j=2
k=3
Expression Result Value
i<j true 1
(1 + j) >= k true 1
(j + k) > (i + 5) false 0
i=1
True or False j=2
k=3
Expression Result Value
k != 3 false 0
j == 2 true 1
(j + k) >= (i + 5) false 0
Precedence
Each One is Complement of Another
Simplified Expression
Logical Operation
• There are three kinds of logical operators.
Operators Meaning Logical
&& AND
|| OR
! NOT
True or False!
i=7
True or False f = 5.5
c = ‘w’ (119)
Expression Result Value
(i >= 6) && (c == 'w') true 1
(i >= 6) || (c == 119) true 1
(f < 11) && (i > 100) false 0
Precedence
Operator Precedence
! Highest
> >= < <=
== !=
&&
|| Lowest
Assignment Operators
• Use to assign the result of an expression to a variable.
• Most common “=“
• Remember: = and == are not same!
• Other five are:
• +=
• -=
• *=
• /=
• %=
Shorthand Assignment Operator
• If we have an assignment statement as follow-
; ;
• We can written this as-
; ;
Shorthand Assignment Operator
Statement with simple Statement with shorthand
assignment operator assignment operator
Advantage of Shorthand Operators
The use of shorthand assignment operators has three
advantages:
• What appears on the left-hand side need not be
repeated, so easier to write.
• The statement is more concise and easier to read.
• The statement is more efficient.
Unary Operators
• Operators that act upon a single operand to produce
a new value are Known as unary operators.
Increment and Decrement Operator
• ++ and --
• The ++ add 1 to the operand and -- subtracts 1.
• Both are unary operators and takes the form:
++m; or m++;
--m; or m--;
++m; is equivalent to m = m + 1; (or m += 1;)
--m; is equivalent to m = m - 1; (or m -= 1;)
++m; vs m++;
• ++m and m++ mean the same thing when they form
statements independently.
m = 5;
m = 6
++m;
m = 5;
m = 6
m++;
++m; is equivalent to m = m + 1; (or m += 1;)
--m; is equivalent to m = m - 1; (or m -= 1;)
++m; vs m++;
• If ++m and m++ used on the right-hand side of an
assignment statement, they behave differently.
m = 5; m = 6
y = ++m; y = 6
m = 5; m = 6
y = m++; y = 5
++m; is equivalent to m = m + 1; (or m += 1;)
--m; is equivalent to m = m - 1; (or m -= 1;)
++m; vs m++;
• A prefix operator (++m) first adds 1 to the operand
and then the result is assigned to the variable to the
variable on left.
• A postfix operator first operator first assigns the
value to the variable on left and then increments the
operand.
++m; vs m++;
• Program
Conditional Operators
exp1 ? exp2 : exp3
We will discuss it in slide 5!
Bitwise Operator
Operator Meaning
& Bitwise AND
| Bitwise OR
^ Bitwise exclusive OR
<< Shift left
>> Shift right
We will learn
later!
Special Operators
• C supports some special operators of interest
– Comma operator ( , )
– Sizeof operator (sizeof(v))
– Pointer operators ( & and * )
– Member selection operator ( . and ->)
The Comma Operator
• The comma operator can be used to link the related
expressions together.
x = 10;
y = 5; value = (x=10, y=5, x+y;)
value = x+y;
• Evaluated left to right
• The value of right-most expression is the value of the
combined expression.
Find the value of t
t = x, t = y, t = z; z
• Evaluated left to right
• The value of right-most expression is the value of the
combined expression.
The sizeof Operator
• This a compile time unary operator.
• Returns the number of bytes the operand occupies.
• The operand may be-
– a variable m = sizeof(sum);
– a constant n = sizeof(2358);
– A data type qualifier k = sizeof(double);
Type Conversion In Expression
• Implicit Type Conversion
• Explicit Conversion
We will learn
later!
Operator Precedence and Associativity
• Precedence is used to determine how an expression
involving more than one operator is evaluated.
• There are distinct levels of precedence
• An operator may belong to one of these levels
• The operators at higher level of precedence are
evaluated first.
• The operators of the same precedence are evaluated
either from ‘left to right’ or from ‘right to left’.
• This is known as associativity property of an operator.
Operators Precedence
Some Computational Problem
• Computer gives approximate values for real
numbers that can cause serious problem.
• Another problem is division by zero.
• Overflow and Underflow
Home Task
• Write your own program using this math functions
– sin(x)
– ceil(x)
– exp(x)
– fabs(x)
– floor(x)
– fmod(x,y)
– pow(x,y) Or/and google
– sqrt(x) it!