Discuss the different types of operators used in C with an example for each
C supports a rich set of operators.
They are classified into following categories:
1.
Arithmetic operators
2.
Relational operators
3.
Logical operators
4.
Assignment operators
5.
Increment and decrement operators
6.
Conditional operators
7.
Bitwise operators
8.
Special operators(Comma ‘,’ and sizeof()
operator)
Ø
Arithmetic operators: C provides all the basic arithmetic operators
which are following:
Operator
|
Description
|
Example
|
+
|
Addition or
unary plus
|
5+4 Result
9
|
-
|
Subtraction
or unary minus
|
5-4 Result
1
|
*
|
Multiplication
|
5*4 Result 20
|
/
|
Division
|
5/4 Result
1
|
%
|
Modulo
division
|
5/4 Result
1
|
Ø
Relational operators:
C provides all the basic relation
operators to compare two or more quantities and depending on their relation
taking decision. These operators and their meanings are as below.
Operator
|
Description
|
Example
|
>
|
greater
than
|
5 > 4
|
>=
|
greater
than or equal to
|
mark >=
score
|
<
|
less than
|
height <
75
|
<=
|
less than
or equal to
|
height
<= input
|
==
|
equal to
|
score ==
mark
|
!=
|
not equal
to
|
5
!= 4
|
Ø
Logical operators:
In addition to the relational
operators, C has the following three logical operators. The logical operators
&& and || are used when we want to test more than one condition and
make decision.
Operator
|
Description
|
Example
|
&&
|
Logical AND
|
If(1< i
&& I <10) ...
|
||
|
Logical OR
|
If (i <5
|| I =5) ...
|
!
|
Logical NOT
|
If ( i !=5)
...
|
Ø
Assignment operators:
Assignment operators are used to
assign the result of an expression to a variable.
Operator
|
Description
|
Example
|
=
|
Assignment
Operator
|
a=
5
|
Ø
Increment and decrement operators(Or Binary
Operator):
C provides shortcuts to add or
subtract constant 1 to a variable.
Operator
|
Description
|
Example
|
++
|
Increment
|
a=a++
|
--
|
Decrement
|
a=a--
|
Ø
Conditional operators:
The conditional operator (ternary
operator) pair “?:” is available in C to construct conditional expressions of
the form
Expr1?expr2:expr3
For example
a=100;
b=200;
c=(a>b)?a:b;
Result: c=200
This is same as if..else statement as
follows:
if (a>b)
c=a;
else
c=b;
Ø
Bitwise operators:
The bitwise operators operate on
integers thought of as binary numbers or strings of bits. These operators let
us work with the individual bits of a variable; one common use is to treat an
integer as a set of single-bit flags.
Operator
|
Description
|
Example
|
&
|
Bitwise AND
|
|
|
|
Bitwise OR
|
|
^
|
Bitwise
exclusive OR
|
|
<<
|
Shift left
|
|
>>
|
Shift right
|
Ø
Special operators:
C supports some special operators
like ‘,’ and sizeof() operators.
The ‘,’ (comma) operator can be used
to link the related expression together.
The sizeof() is a compile time
operator and, when used with an operand it returns the numbers of bytes the
operand occupies. The operand may be variable, a constant or a data type
qualifier.
Operator
|
Description
|
Example
|
,
|
Comma
Operator
|
Value=(x=10,
y=5, x+y) Results: value=15
|
sizeof()
|
Sizeof()
Operator
|
k=
sizeof(235L)
|