Operators are the symbols which tell the computer to
execute certain mathematical or logical operations. A mathematical or logical
expression is generally formed with the help of an operator.
Operator performs an operation on data. They are classified
into following −
·
Arithmetic operators.
·
Relational operators.
·
Logical operators.
·
Assignment operators.
·
Increment and decrement
operators.
·
Bitwise operators.
·
Conditional operators.
·
Special operators.
Arithmetic operator
These operators are used for numerical calculations (or)
to perform arithmetic operations like addition, subtraction etc.
|
Operator |
Description |
Example |
a=20,b=10 |
Output |
|
+ |
Addition |
a+b |
20+10 |
30 |
|
- |
subtraction |
a-b |
20-10 |
10 |
|
* |
multiplication |
a*b |
20*10 |
200 |
|
/ |
Division |
a/b |
20/10 |
2(quotient) |
|
% |
Modular Division |
a%b |
20%10 |
0 (remainder) |
Relational operators
These are used for comparing two
expressions.
|
Operator |
Description |
Example |
a=20,b=10 |
Output |
|
< |
less than |
a<b |
10<20 |
1 |
|
<= |
less than (or) equal
to |
a<=b |
10<=20 |
1 |
|
> |
greater than |
a>b |
10>20 |
0 |
|
>= |
greater than (or)
equal to |
a>=b |
10>=20 |
0 |
|
== |
equal to |
a==b |
10==20 |
0 |
|
!= |
not equal to |
a!=b |
10!=20 |
1 |
Logical Operators
These are used to combine 2 (or) more
expressions logically.
They are logical AND (&&)
logical OR ( || ) and logical NOT (!)
Logical NOT(!)
|
Operator |
Description |
Example |
a=20,b=10 |
Output |
|
&& |
logical AND |
(a>b)&&(a<c) |
(10>20)&&(10<30) |
0 |
|
|| |
logical OR |
(a>b)||(a<=c) |
(10>20)||(10<30) |
1 |
|
! |
logical NOT |
!(a>b) |
!(10>20) |
1 |
Assignment operators
It assigns a value to a variable. The types of assignment
operators are −
·
Simple assignment.
·
Simple assignment.
|
Operator |
Description |
Example |
|
= |
simple assignment |
a=10 |
|
+=,-=,*=,/=,%= |
compound assignment |
a+=10"a=a+10a=10"a=a-10 |
Increment and decrement operator
Let us understand what is an increment
operator.
Increment operator (++)
This operator increments the value of
a variable by 1
The two types include −
·
pre
increment
·
post
increment
example 1)
main() {
int a= 10, z;
z= ++a;
printf("z=%d", z);
printf("a=%d", a);
}
z= 11a=11 Example 2) main() { int a= 10, z; z= a++;printf("z=%d", z); printf("a=%d", a);}z= 10a=11
Bitwise Operator
Bitwise operators operate on bits.
|
Operator |
Description |
|
& |
Bitwise AND |
|
| |
Bitwise OR |
|
^ |
Bitwise XOR |
|
<< |
Left Shift |
|
>> |
Right shift |
|
~ |
One's Complement |
Left Shift
If the value is left shifted one time,
then its value gets doubled.
For example, a = 10, then a<<1 =
20
Right shift
If the value of a variable is right
shifted one time, then its value becomes half the original value.
For example, a = 10, then a>>1 =
5
0 comments:
Post a Comment