| What is
Operator? |
An Operator is a one type of symbol
that is used to perform certain operation on its operands.
Operators are always associated with Operands. |
| Types of
Operator Bases on Operands |
Operators can be classified into three categories based on its operands.
(1) Unary Operator: An Operator having only one Operand is known
as Unary Operator. Example: Unary +, Unary -
(2) Binary Operator: An Operator having two Operands is known as
Binary Operator. Example: +, -, *, /, % etc...
(3) Ternary Operator: An Operator having three Operands is known
as Ternary Operator. For Example: ?: (Conditional Operator) |
| Types of
Operator based on Operations Performed |
Operators can be classified into
seven categories based on operations performed by Operators.
(1) Arithmatic Operator
(2) Relational Operator
(3) Logical Operator
(4) Assignment Operator
(5) Increment/Decrement Operator
(6) Bitwise Operator
(7) Conditional Operator |
| Arithmatic
Operator |
Arithmatic Operators are used to
perform mathematical operations on its operands.
Following are
the List of Arithmatic Operators |
| Operator |
Purpose |
Example |
| + |
Perform addition of its two operands. |
int x=5,y=3,z;
z=x+y;
z=8 |
| - |
Perform substraction of its two operands. |
int x=5,y=3,z;
z=x-y;
z=2 |
| * |
Perform multiplication of its two operands. |
int x=5,y=3,z;
z=x*y;
z=15 |
| / |
Divide left operand by right operand. |
int x=5,y=3,z;
z=x/y;
z=1 |
| % |
Divide left operand by right operand and return remainder of division. |
int x=5,y=3,z;
z=x%y;
z=2 |
|
| Relational
Operator |
Relational Operators
are used to determine relation between two operands. Relation means weather one
operand is greater then other operand or not etc...
Generally Relational Operators are used to check condition. The result of the condition
is either TRUE or FALSE.
Following are the List of Relational Operators |
| Operator |
Purpose |
Example |
| < |
Check weather left operand is less then right operand or not. |
int x=5,y=4;
x<y FALSE |
| > |
Check weather left operand is greater then right operand or not. |
int x=5,y=4;
x>y TRUE |
| <= |
Check weather left operand is less then or equal to right operand or not. |
int x=5,y=4;
x<=y FALSE |
| >= |
Check weather left operand is greater then or equal to right operand or not. |
int x=5,y=4;
x>=y TRUE |
| == |
Check weather left operand and right operand are equal or not. |
int x=5,y=4;
x==y FALSE |
| != |
Check weather left operand and right operand are equal or not. |
int x=5,y=4;
x!=y TRUE |
|
| Logical
Operator |
Logical
Operators are used to combine more then one condition.
If you want to check more then one condition at a same time and based on the result
of that conditions want to perform some action then you can use Logical Operator. |
| Operator |
Purpose |
Example |
| AND(&&) |
When more then one conditions are combined using Logical AND operator then it will
check all the conditions and returns TRUE only when all the conditions evalute to
TRUE.
If any of the condition evalute to FALSE then it returns FALSE. |
int x=5, y=4,z=7;
((x>y) && (x>z))
FALSE |
| OR (||) |
When more then one conditions are combined using Logical OR operator then it will
check all the conditions and returns FALSE only when all the conditions evalute
to FALSE.
If any of the condition evalute to TRUE then it returns TRUE. |
int x=5, y=4,z=7;
((x>y) || (x>z))
TRUE |
| NOT(!) |
It is Unary Operator.
It is used to invert the result of the condition.
If condition evalutes to TRUE then it inverts it to FALSE and vice versa. |
!(x > y)
FALSE |
|
|
| Assignment
Operator |
Assignment
Operator is used to assign value of its right operand to its left operand.
if in an arithmatic expression the operand to the both side of assignment operator
is same then we can write them in the short form.
For Example:
a = a + 5 can be written as a+=5.
Here += is known as short hand assignment operator. |
| Operator |
Purpose |
Example |
| = |
It will assign value of its right operand to its left operand.
|
int a;
a = 5; |
| += |
Here, += Operator first adds value to the operand and then result of that addition is assigned to the operand. |
int a=5;
a+=5;
so Now a=10; |
| -= |
Here, -= Operator first subtracts value from the operand and then result of that subtraction is assigned to the operand. |
int a=5;
a-=5;
so Now a=0; |
| *= |
Here, *= Operator first multiply the operand with some value and then result of that multiplication is assigned to the operand. |
int a=5;
a*=5;
so Now a=25; |
| /= |
Here, /= Operator first divide the operand by some value and then result of that division is assigned to the operand. |
int a=5;
a/=5;
so Now a=1; |
| %= |
Here, %= Operator first divide the operand with some value and then reminder of that division is assigned to the operand. |
int a=5;
a%=5;
so Now a=0; |
|
| Increment/Decrement
Operator |
Incremnt
Operator is used to increment the value of its operand by one.
Decrement Operator is used to decrement the value of its operand by one.
Increment and Decrement operators are unary operators. |
| Operator |
Purpose |
Example |
| ++(Prefix) |
It will first increment the value of its operand by one and then assign that value
to the variable. |
int x=10, y;
y=++x;
x=11
y=11 |
| -- (Prefix) |
It will first decrement the value of its operand by one and then assign that value
to the variable. |
int x=10, y;
y=--x;
x=9
y=9 |
| ++(Postfix) |
It will first assign the value of its operand to variable and then increment the
value of its operand by one. |
int x=10, y;
y=x++;
x=11
y=10 |
| --(Postfix) |
It will first assign the value of its operand to variable and then decrement the
value of its operand by one. |
int x=10, y;
y=x--;
x=9
y=10 |
|
| Bitwise
Operator |
Bitwise
Operators are used to manipulate integer numbers at bit level.
Using Bitwise Operator we can work with individual bits of integer number.
Bitwise Operator can not work with floating point numbers. |
| Operator |
Purpose |
Example |
| & |
Perform Bitwise AND operation on individual bits of its two operand. |
int a=8,b=5;
a & b
0 0 0 0 1 0 0 0 (a)
0 0 0 0 0 1 0 1 (b)
____________
0 0 0 0 0 0 0 0
Result is 0. |
| | |
Perform Bitwise OR operation on individual bits of its two operand. |
int a=8,b=5;
a | b
0 0 0 0 1 0 0 0 (a)
0 0 0 0 0 1 0 1 (b)
____________
0 0 0 0 1 1 0 1
Result is 13. |
| ^ |
Perform Bitwise EX-OR operation on individual bits of its two operand. |
int a=8,b=5;
a ^ b
0 0 0 0 1 0 0 0 (a)
0 0 0 0 0 1 0 1 (b)
____________
0 0 0 0 1 1 0 1
Result is 13. |
| ~ |
Perform One's complement operation on its single operand.
It will inverts each bit of its operand.
It is Unary Operator. |
int a=8;
~a
0 0 0 0 1 0 0 0 (a)
____________
1 1 1 1 0 1 1 1
Result is 247. |
| << |
Perfrom Bitwise Shift Left operation.
It will shift each bits of its operand towards left side upto specified number of
position.
After Shifting bits it will insert that much 0 at the right side. |
int a=8;
a << 2
0 0 0 0 1 0 0 0 (a)
____________
0 0 1 0 0 0 0 0
Result is 32. |
| >> |
Perfrom Bitwise Shift RIght operation.
It will shift each bits of its operand towards right side upto specified number
of position.
After Shifting bits it will insert that much 0 at the left side. |
int a=8;
a >> 2
0 0 0 0 1 0 0 0 (a)
____________
0 0 0 0 0 0 1 0
Result is 2. |
|
| Conditional
Operator |
Conditional
Operator is an alternative of If ... else statement.
Conditional Operator having three opernads thats why it is also known as Ternary
Operator.
General Syntax of Ternary Operator is:
(Condition) ? Statement1: Statement2; |
| Operator |
Purpose |
Example |
| ?: |
Conditional Operator is used to check some condition.
If condition is TRUE then the statement followed by ? is executed.
If condition is FALSE then the statement followed by : is executed. |
int x=5,y=3;
(x>y)?printf("x is maximum"):printf("y is maximum");
|
|