C Operators
§ An
operator is a symbol that is used to perform operations such as arithmetic,
logical, bitwise, etc.
§ There
are three types of operators in C language.
Figure:
Types of Operators
Concept
·
Operator :
It
is an entity, which perform operation that is called operator.
Example:
20 + 30
Here, + sign is an operator which perform
addition.
·
Operand :
It
is an entity on which operation is to be performed is know as operand or data.
Example:
20 + 30
Here, 20 and 30 number
is operator or called data on which addition operation is perform.
1) I) Binary Operator :
·
It is operator which is requires minimum
two operand or data to perform operation is known as binary operator.
·
Binary operators are arithmetic, relational,
logical, assignment & bitwise.
1) Arithmetic
operator:
Operators |
Operation |
Example |
Addition (+) |
Used for addition |
10 + 5 |
Subtraction (-) |
Use for subtraction |
10 – 5 |
Multiplication (*) |
Use for multiplication |
10 * 5 |
Division (/) |
Use for division |
10 / 5 |
Modulus (%) |
Use to find reminder after division |
10 % 5 |
(Note – name of special
characters i.e. + plus sign, - minus sign,
* asterisk, / slash. & % percentage)
Arithmetic
operation performs on different data types as follows.
Operator |
Integer |
float |
character |
+ |
7 + 3 = 10 |
4.8 + 1.2 = 6.0 |
‘a’+‘A’= 97+65=162 |
- |
7 - 3 = 4 |
4.8 – 1.2 = 3.6 |
‘a’-‘A’= 97-65=32 |
|
4-7 = -4 |
1.2 – 4.8= -3.6 |
|
* |
7 * 3 = 21 |
4.8 * 1.2 = 5.76 |
|
/ |
7 / 3 = 2 |
4.8 / 1.2 = 4.0 |
|
|
3 / 7 = 0 |
1.2 / 4.8 = 0.25 |
|
% |
7 % 3 = 1 |
|
|
|
3 % 7 =3 |
|
|
1) Relational
Operator:
·
Relational operator compare two values
and depending on its relation certain decisions taken.
·
If relation is True it returns ‘one’ and
relation is False it returns ‘zero’
·
Relational operator used in decision
making statement such as ‘if’ and ‘while’
Operator |
Meaning |
< |
Less
than |
< = |
Less
than or equal to |
> |
Greater
than |
> = |
Greater
than or equal to |
= = |
equal
to |
! = |
Not
equal to |
Example:
Example |
Meaning |
Conclusion |
2
< 4 |
2
is less than 4 |
True |
2
> 4 |
2
is greater than 4 |
False |
2
<= 4 |
2
is less than or equal to 4 |
True |
4
= = 4 |
4
is equal to 4 |
True |
4
! = 4 |
4
is not equal to 4 |
False |
2) Logical
operators:
·
When we want to check more than one
condition and make decision that time we are use logical operator.
·
The following table shows logical symbol
and their meaning.
Logical Symbol |
Meaning |
&
& |
Logical AND |
|| |
Logical OR |
! |
Logical NOT |
Example:
·
a < b && x >= 10
In
above example, it is true when the value of a is less than value of b (a <
b) is true AND the value of x is equal to 10 (x = = 10) is true.
·
a < b || x >= 10
In
above example, it is true when the value of a is less than value of b (a <
b) is true OR the value of x is equal to 10 (x = = 10) is true.
3) Assignment
operator:
·
Variable = Value or Variable or Expression
·
Assignment operator is used to assign
the result of an expression to a variable.
For
Example,
a
= 4 ; (the 4 is copied to variable ‘a’)
a
= b ; (the value in variable ‘b’ is copied to
variable ‘a’)
a
= a + 1 ; (if variable ‘a’
value is 4 then a = 4 + 1 means new value of a is 5)
·
C has shorthand assignment
operators of the form,
v
op =
exp |
Where,
v = variable
op = arithmetic operator
exp = expression
·
In c language there are 5 shorthand
assignment operators i.e. + = , - =, * =, / = and % =
Example,
a + = y + 1 (Expand form of this expression is a = a + (y + 1))
Here, v (variable) = a
op (arithmetic operator) = +
exp (expression) = y + 1
Statement with shorthand operator |
Statement with simple assignment
operator |
a + = 1 |
a
= a + 1 |
a -
= 2 |
a
= a - 2 |
a * = b+1 |
a = a * (b + 1) |
c / = n + 1 |
c = c / (n+1) |
I)
0 Comments