OPERATORS: An operator is a symbol which helps the user to command the computer to do certain mathematical or logical manipulations. Operators are used in C language program to operate on data and variables. C has a rich set of operators which can be classified as
1. Arithmetic operators.2. Relational Operators.
3. Logical Operators.
4. Assignment Operators.
5. Increment and Decrement Operators.
6. Conditional Operators.
7. Bitwise Operators.
Arithmetic
Operators: - All the basic arithmetic operations
can be carried out in C. All the operators have almost the same meaning as in
other languages. Both unary and binary operations are available in C language.
Unary operations operate on a single operand, therefore the number 5 when
operated by unary – will have the value –5.
Arithmetic
Operators
Operator
|
Meaning
|
Example |
+
|
Addition or Unary Plus |
x + y |
– |
Subtraction or Unary Minus |
x - y |
* |
Multiplication |
a * b |
/
|
Division |
a/b |
% |
Modulus Operator |
a%b |
Here
a, b, x, y are known as operands.
Relational operators:
- Performs the comparison between
operands or values depending on their relation. Relation means condition. It
contains two possible actions either true or false. Where true value is
1(Non-zero) and false value is
0(zero). A simple relational expression that contains only one relational
operator is termed as a relational expression.
Relational expressions are used in decision-making (Branching and Looping)
statements.
A simple relational expression contains
only one relational operator and has the form
Syntax: Operand-1 relational operator Operand-2
The relational operators are
Operator |
Meaning |
Example |
> >= < <= = = != |
Greater
than Greater
than or equal to Less
than Less
than or equal to Equal
to (relational) Not
equal to |
a>b a>=b a<b a<=b a==b a!=b |
Logical operators: - There
are three logical operators.
The logical operators && and | |
are called as binary operators. They are used, when we want to test more than
one condition and make decisions.
Ex :
if (avg>=60) &&
(avg<=74)
print(“the grade is first class = %f”,avg);
Operator
|
Meaning
|
&& |
| ! |
Logical
AND Logical
OR Logical
NOT |
The
Truth Table of Logical AND operator is
A
B |
A&&B |
T
T T
F F
T F
F |
T F F F |
If all operands are true then the result
is true otherwise false. It is denoted by ‘&&’
The
Truth Table of Logical OR operator is
A
B |
A||B |
T
T T
F F
T F
F |
T T T F |
When any one operand is true then the
result is true, otherwise false.
The
Truth Table of Logical NOT operator is
A |
!A |
T F |
F T |
Assignment Operator: -
Assignment operators are used to assign the result of an expression to an
identifier. The assignment operator is denoted by “=”.
Increment and Decrement Operators: -‘C’ language contains two special operators such as increment and decrement operators. These operators are called as unary operators. A Unary expression contains a single operand (++i, i++). Increment operator increments the operand by 1(one) (i = i+1). Decrement operator decrements the operand by 1 (i = i - 1).
The Increment Operator can be divided into two types
a)
Pre-Increment Operator
(or) Prefix Increment Operator
b) Post-Increment
Operator (or) Postfix Increment Operator
a. Pre-Increment Operator: - First
adds 1 to the operand, then after that result is assigned to the variable on
left. It is denoted by ‘++i’.
Ex:-
i) i = 5; ii)
i = -6
a = ++i; a = ++i
then result is i=6, a=6 then result is i= -5, a= -5
b. Post Increment Operator: - First the given value is assigned to
the variable on left, then increments the operand by 1. It is denoted by ‘i++’.
Ex:-
i) i = 5 ii)
i = -6
a =i++ a = i++
result is i= 6, a = 5 result is i= -5, a = - 6
The
Decrement Operator can be divided into 2 types.
a)
Pre-Decrement Operator (or) Prefix Decrement Operator
b)
Post-Decrement Operator (or) Postfix Decrement Operator
a. Pre-Decrement Operator :- first
decrements the operand by 1, then after that result is assigned to the variable
on left. It is denoted by ‘--i’.
Ex :- 1) i = 5; 2)
i = -6
a = --i; a = --i
Then result is i=4, a=4. Then the result is i=
-7, a= -7.
b. Post Decrement Operator :- First the given value is assigned to
the variable on left, then
decrements the operand by 1. It
is denoted by ‘i--‘.
Ex :-
i) i = 5 ii)
i = -6
a = i-- a = i--
result is i= 4, a = 5; result is i= -7, a = - 6
Conditional
Operator: - It is also called as Ternary Operator
because it has 3 expressions. It acts as shorthand version of the if-else
statement. It is denoted by ‘? :’. It is used to reduces the if statements.
exp1? exp2 :exp3; (OR) Variable = exp1? exp2 :exp3;
Where
exp1 is condition, exp2 is statement1 and exp3 is statement2. First exp1 is
evaluated, when the condition becomes true statement1 is executed and
statement2 is not executed. Otherwise statement2 is executed and statement1 is
not executed.
Ex:-
i) To find the largest from a & b
a>b ? a: b; (or) c = a>b ? a: b;
Bit-wise operator: -‘C’ supports a set of Bit-wise operators
for manipulating data at bit level.
These operators are used for testing the bits, shifting them right or left.
These operators work only with ‘int’ and ‘char’ data types and can’t be used
with ‘float’ or ‘double’ types. Bit
means Binary Digit which contains 0 & 1.The Bitwise Operators are
Operator Meaning
& Bitwise AND
| Bitwise OR
^ Bitwise Ex-OR
<< Left-Shift
>> Right-Shift
~ One’s Complement
Bitwise Logical AND Operator: -
When
all both the operands are 1 then the
result is 1 otherwise 0. It is denoted by ‘&’. The Truth Table is
A B |
A&B |
1 1 1 0 0 1 0 0 |
1 0 0 0 |
Ex : x
= 5 = 101,
y
= 2 = 010
A B |
A|B |
1 1 1 0 0 1 0 0 |
1 1 1 0 |
Ex : x = 5 = 101,
y = 2 = 010
x | y = 7 = 111
Bitwise Logical Ex-OR Operator: - When the operands are different then the result is 1 otherwise 0 (when the operands are same). It is denoted by ‘^’. The Truth Table is
A B |
A^B |
1 1 1 0 0 1 0 0 |
0 1 1 0 |
Ex
: x = 4 = 100,
y = 2 = 010
x ^ y = 6 = 110
Bitwise Complement Operator: -It converts 0’s into 1’s and 1’s into 0’s. It is denoted by ~(tild). It is also called as one’s complement operator.
Ex:- x = 100111011
~x = 011000100
Bitwise
Shift Operators:
- The shift operators move bits to
the right or left. There are 2 types of bitwise shift operators. a) Bitwise Left-Shift Operator b) Bitwise Right-Shift Operator
These are binary operators that
require two integral operands (character or integer). The first operand is the
value to be shifted and the second operand is the number of bits to be shifted.
Bitwise
Left-Shift Operator: -
The left shift operator shifts the bits to the left side. It is denoted by ‘<<’. The bits towards the left
end drop off and the vacant bit positions on the right end are filled with
zeros.
Ex: - 10110110<<2 = 11011000
When n is the given number, and s
is the number of positions to be shifted towards left, the result is n*2s.
For ex: n = 3à 011 n<<2 is 3*22 = 12 à 1100
Bitwise
Right-Shift Operator: -
The right shift operator shifts the bits to the right side. It is denoted by
‘>>’. The bits towards the right end drop off and the vacant bit
positions on the left end are filled with sign
bit ie., filled with 0 when the sign bit is 0, filled with 1 when the sign
bit is 1. This preserves the sign even after the shift.
Ex:- 10110110>>2 = 00101101
When n is the given number, and s
is the number of positions to be shifted towards right, the result is n/2s.
For ex :
n = 8 à
1000
n<<2 y=8/22 = 2 à
010
Comments
Post a Comment