Operators in C :
Operators :
An Operator is a symbol that is used to perform an operation between the operands. C is very rich in built-in-operators. In fact, they place a major role in building the program. The main 5 classes of an operator are listed below.
Types of Operators :
1.Arithmetic Operator
2.Relational Operator
3.Logical Operator
4.Bitwise Operator
5.Assianment Operator
Example: consider an expression
x=10+2*5;
In this Expression, we don't know, which operator will be evaluated first. To Evaluate the expression we need to know the precedence of an operator. The precedence of the operator will specify which operator will be evaluated first and next. The Associativity specifies the direction of an operator to be Evaluated.
Click Here to know more about the Precedence and Associativity of an operator in C.
Arithmetic Operator: Arithmetic operators are used to calculating Mathematical operations. The arithmetic operators are +, -, *, /, %.
operator | Operation | Result |
+ | Addition | 1 + 2 = 3 |
- | Subtraction | 2 - 1 = 1 |
* | Multiplication | 2 * 1 = 2 |
/ | Division | 4 / 2 = 2 [quotient] |
% | Modulus | 5%2=1 [remainder] |
Relational Operator: It tests the kind of relation between two entities. The relational operators are listed below.
operator | Operation | Result |
> | Greater than | Returns true, if 20>10, otherwise false. |
>= | Greater than or equal to | Returns true, if 10>=9, otherwise false |
< | Less than | Returns true, if 9<10, otherwise false |
<= | Less than or equal to | Returns true, if 9<=9, otherwise false |
== | equal | Returns true, if 10==10, otherwise false |
!= | Not equal | Returns true, if 10!=9, otherwise false. |
Logical Operator: Logical Operator is used to connecting the two or more expressions and determine the logic between them. The Logical Operators are listed below.
operator | Operation | Result |
&& | AND | Returns true, If both the conditions on either side of the operator are true. Ex: 20>10 && 10==10 |
|| | OR | Returns true, If either one of the conditions on either side of the operator is true. Ex: 20>10 || 11==10 |
! | NOT | Returns true, if the condition is false. Ex: 10>20 |
Bitwise Operator: To perform BitWise operations between the operands we use a bitwise operator. The mathematical operations such as addition, subtraction, multiplication are done at the bit level. The bitwise operators are listed below.
In C programming bits are represented in the form of 0's and 1's. To perform the bitwise operations we used to convert the decimal number [Ex: 10] into the binary form [0's and 1's ].
Remember this code from right to left to convert into binary form.
. . . . . . 2^4 2^3 2^2 2^1 2^0
. . . . . . . 16 8 4 2 1
Ex: The binary form of 10 is 1010.
.. . 16 8 4 2 1
1 0 1 0
Place the ones at the highest value to get the value 10.
operator | Operation | Result |
& | AND | The result is 1 if both the bits must be 1. |
| | OR | The Result is 1 if one of the bit should be 1. |
^ | XOR | The Result is 1 if there exists only one bit as 1. |
~ | NOT | The result is 1 if the bit is 0. |
>> | RIGHT SHIFT | If >>1, the bits positions are shifted 1 position to the right side. |
<< | LEFT SHIFT | If <<1, the bits positions are shifted 1 position to the left side. |
Examples:
1. 10 & 12 = 8
1 0 1 0
1 1 0 0
1 0 0 0 [8+0+0+0]
2. 10 | 12 =14
1 0 1 0
1 1 0 0
1 1 1 0 [8+4+2+0]
3. 10 ^ 12 = 6
1 0 1 0
1 1 0 0
0 1 1 0 [0+4+2+0]
4. ~1= 0 [~ = tild operator]
5. 10 = 1010 [Binary form]
place the 0's before the binary form to get 8 digits binary format.
10>>1 = 5
Given: 00001010
Result:00000101 [0+4+0+1]
10>>3 = 1
Given : 00001010
Result : 00000001
6. 4 = 0100
4<<1 = 8
Given : 00000100
Result : 00001000
4<<2 = 16
Given : 00000100
Result : 00010000
Assignment Operator: The Assignment operator is used to assign a value to a variable. The Assignment operators are =, +=, -=, /=, %=.
operator | Operation | Result |
+= | Addition | x+=1 or x=x+1 |
-= | Subtraction | X-=1 or x=x-1 |
*= | Multiplication | X*=1 or x=x*1 |
/= | Division | X/=1 or x=x/1 |
%= | Modulus | X%=1 or x=x%1 |
Increment and Decrement operator: These operators are categorized into 4 types.
1. post-increment : x++
2. pre-increment : ++x
3. post decrement : x--
4. pre decrement : --x
Ternary Operator: The ternary operator operates between the three operands/conditions. The ternary operator is represented by ?:.
syntax : x = var1 ? var2 : var3 ;
we have taken 3 operands as var1, var2, var3. If the condition of a var1 is evaluated as true then var2 will be executed otherwise var3 will be executed.
Example: x = 201>10 ? 20 : 10 ;
In the above example, the condition is true so that 20 will be assigned to x otherwise 10 will be assigned to x.
No comments:
Post a Comment