First of all let us know what an operator actually means? Well in layman term, an operator is a special symbol used on C programming which instructs compiler to specified mathematical or logical operation. Various operators are pre-defined in C language which proves to be useful for programmers in programming different software.
There are mainly 5 kinds of operators, namely:
• Arithmetic Operators
• Relational Operators
• Logical Operators
• Bitwise Operators
• Assignment Operators
Arithmetic Operator
This operator is used to most of the mathematical calculations in a c programming.
See the table given bellow to understand what arithmetic operators are:
(let A=30 and B=15)
Operator
|
Description
|
Example
|
+
|
Adds two operands
|
A+B will give 45
|
-
|
Subtracts operand
|
A-B will give15
|
*
|
Multiplies both operands
|
A*B will give 450
|
/
|
Divide operands
|
A/B will give 2
|
%
|
Modulus Operator
|
A%B will give 0
|
++
|
Increment
|
A++ will give 31
|
--
|
Decrement
|
B-- will give 14
|
Relational Operator
Relational operators are those symbols which are used to compare two or more operands. Examples of relational operator are equal to, less than, more than, more than or equal to, less than or equal to, not equal to etc.
We’ve already discussed about Relational Operator in our previous blog.
Click Here to See
Logical Operator
Logic operators work like logic gates that are OR gate, AND gate, NOT gate etc. They only work in case of binary code i.e. 1 or 0.
Here are logical operators used in C language programming.
(Let A=1 and B=0)
Operator
|
Description
|
Example
|
&&
|
AND operator
|
(A && B) gives False
|
||
|
OR Operator
|
(A || B) gives True
|
!
|
NOT Operator
|
A! gives False;
B! gives True |
Bitwise Operator
Bit-wise operator first covert a number into its binary code and then operate upon it to give the result.
Following are the results of bitwise operation:
u
|
v
|
u&v
|
u|v
|
u^v
|
0
|
0
|
0
|
0
|
0
|
0
|
1
|
0
|
1
|
1
|
1
|
1
|
1
|
1
|
0
|
1
|
0
|
0
|
1
|
1
|
These are bitwise operator:
(Let A=60 and B=13)
A= 00111100
B= 00001101
Operator
|
Description
|
Example
|
&
|
Binary AND gate
|
(A & B) will give 12, which is
00001100
|
|
|
Binary OR gate
|
(A | B) will give 61, which is 00111101
|
^
|
Binary XOR gate
|
(A ^ B) will give 49, which is 00110001
|
~
|
Binary Compliment
|
(~A ) will give -60, which is 11000011
|
<<
|
Binary Left Shift Operator
|
A << 2 will give 240, which is
11110000
|
>>
|
Binary Right Shift Operator
|
A >> 2 will give 15, which is
00001111
|
Assignment Operator
These operator perform following tasks in C language programming:
Operator
|
Description
|
Example
|
=
|
|
A+B=C, C will be A+B
|
+=
|
|
C+=A, C will be C+A
|
-=
|
|
C-=A, C will be C-A
|
*=
|
|
C*=A, C will be C*A
|
/=
|
|
C/=A, C will be C/A
|
%=
|
|
C%=A, C will be C%A
|
<<=
|
|
C<<=2, C will be C<<2
|
>>=
|
|
C>>=2, C will be C>>2
|
&=
|
|
C&=2, C will be C&2
|
^=
|
|
C^=2, C will be C^2
|
|=
|
|
C|=2, C will be C|2
|
No comments:
Post a Comment