3. C_OperatorsExpressions_1101
3. C_OperatorsExpressions_1101
a + b 13
a - b 7
a * b 30
a % b 1 Remainder of division
Arithmetic Operators
example floating point
a + b 14.5
a - b 10.5
a * b 25.0
a / b 6.25
a % b Not Possible!!!
Arithmetic Operators
example character
a 65
a + b 131
a + 1 66
a + ‘A’ 130
a + ‘1’ 114
Type Conversion
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.
int i = 7; ASCII Value
float f = 5.5; w = 119
Example
char c = ‘w’; 0 = 48
int number;
(float) number;
Check Type Cast
Check example
Again
i = 7;
f = 8.5; float num = 10.5;
num % 2;
result = (i + f) % 4;
float num = 10.5;
((int)num) % 2;
Invalid
Relational Operator
The operators which are used to compare two numbers and take decision
depending on their relation are called relational operators.
Example
Relational Operator
Given the following C declarations:
int a =1, b = 2, c = 3, d = 1;
a == d is true
c > b is true
c >= b is true
a >= c is false
a != d is false
a <= d is true
Relational Operator
Suppose that i, j and k are integer variables.
Where, i=1;
j=2;
k=3;
Several logical expressions involving these variables are shown below.
Relational Operator
.
Simplified Expression
Logical Operator
Operators which are used to combine two or more relational expressions are
known as logical operators.
There are three logical operators.
Logical Operator
.
Truth table of Logical Operator
Assignment Operator
Operators which are used to assign the result of an expression to a variable
are known as assignment operators.
Consider an example:
x = 100; meaning: 100 is assigned to x
x += (y+1);
The operator += means ‘add y+1 to x’ or
‘increment x by y+1’.
For y=2; the above statement results x= 103,
x += 3; that is (x = x + 3)
Increment Decrement Operator
C allows two very useful operators increment (++) and decrement (--)
operators.
The operator ++ adds 1 to the operand, while -- subtracts 1.
Rules for ++ and – operators:
i) Increment (++) and decrement (--) operators are unary operators
ii) When postfix ++ (or --) is used with a variable in an expression, the
expression is evaluated first using the original value of the variable and
then the variable is incremented (or decremented) by one.
iii) When prefix ++ (or --) is used in an expression, the variable is
incremented (or decremented) first and then the expression is evaluated
using the new value of the variable.
Increment Decrement Operator
Example:
Web:
1. www.wikbooks.org
and other slide, books and web search.