Unit 3 - Operators and Expression - 1704342525014
Unit 3 - Operators and Expression - 1704342525014
● Arithmetic Operators
● Assignment Operators
● Unary Operators
● Comparison (Relational) Operators
● Shift Operators
● Bit-wise Operators
● Logical Operators
● Miscellaneous Operators
Arithmetic Operators
X1 + X2 =162
X1+ X2+ ‘5’ = 215
Assignment Operator:
Assignment operator assigns the value of the right operator or expression to the
variable on the left side. There are many variations of assignment operator as
described below;
Unary Operators:
● As a prefix
In prefix, the operator precedes the variable i.e. ++var . In this form the
value of the variable is first incremented and then used in the expression as
illustrated below;
var1=20; var2=++var1;
At the end, both variables var1 and var2 store value 21.
● As a postfix
Likewise, in postfix, the operator follows the variable i.e. var++. In this form,
the value of variable is used in the expression and then incremented as
illustrated below;
var1=20; var2=var1++;
Output:
54
43
Output:
56
67
Comparison Operator
Comparison operators evaluate to true or false. They are also called relational
operators.
Shift Operators
Data is stored internally in binary format. A bit can have a value of one or zero.
Eight bits form a byte. Shift operators work on individual bits in a byte. Using the
If the int data type occupies four byte in the memory, the rightmost eight bits of
the number are represented in binary as “00001010”
Bit-wise operators
Bitwise operator works on bits and performs bit-by-bit operation. The truth
tables for &, |, and ^ are as follows −
0 0 0 0 0
0 1 0 1 1
1 0 0 1 1
Assume if A = 60; and B = 13; now in binary format they will be as follows −
A = 0011 1100
B = 0000 1101
-----------------------------
Binary Or Operator:
~A = 1100 0011
Logical Operators
Miscellaneous Operators
1 sizeof
Example:
In the above example, if the marks obtained is more than 50, the
result will be passed else fail.
3 ,
5 Cast
7 *
Precedence of Operators
If more than one operator is involved in an expression, C language has a predefined rule
of priority for the operators. This rule of priority of operators is called operator
precedence.
Here, operators with the highest precedence appear at the top of the table, those with
the lowest appear at the bottom. Within an expression, higher precedence operators will
be evaluated first.
Output:
Value of (a + b) * c / d is : 90
Value of ((a + b) * c) / d is : 90
Value of (a + b) * (c / d) is : 90
Value of a + (b * c) / d is : 50