Monday 29 January 2018

C Operators

 C Operators


          Operators are used to perform mathematical & logical operations. Operators supported by C can be categorized into arithmetic, logical, relational, bitwise (used for bit manipulation) & some special operators. Let's see how these operators are used in programming.

Arithmetic Operators

          Arithmetic Operators supported by C are ADD ( + ), SUBTRACT ( - ), MULTIPLY ( * ), DIVIDE ( / ),    MODULO ( % ), INCREMENT ( ++ ) and DECREMENT ( -- ).

' / ' operator prints the quotient of the division operation & ' % ' operator prints the remainder of the division operation. Fo e.g, 5 / 2 = 2 and 5 % 2 = 1.

' ++ ' operator increments a value by 1 and ' -- ' operator decrements a value by 1. For e.g 5++ = 6 and 5-- = 4.

The behaviour of ' + ', ' - ' and ' * ' are trivial.

Let's look at the following program to see arithmetic operators in action :

----------------------------------------------------------------------------------------------------------------------------


----------------------------------------------------------------------------------------------------------------------------
Output :

Sum of 5 and 2 is 7
Difference of 5 and 2 is 3
Product of 5 and 2 is 10
Division of 5 by 2 is 2
Modulo of 5 by 2 is 1
Incrementing a : 6
Decrementing b : 1

----------------------------------------------------------------------------------------------------------------------------

Please note that some of the expressions in which a variable is present in both sides can be written as



Logical Operators

            Logical Operators supported by C are AND ( && ), OR ( || ) and NOT ( ! ).
Consider two operands A and B.
A && B evaluates to True only if both A and B are True.
A || B evaluates to True if any of the two operands is True.
' ! ' operator negates the state of an operand. For e.g, if A = True, then !A = False.
Note that any non-zero value is considered True and zero is considered False.

Following program shows logical operators in action :  

----------------------------------------------------------------------------------------------------------------------------



----------------------------------------------------------------------------------------------------------------------------

Output :

1 AND 0 = 0
1 OR 0 = 1
NOT 0 = 1

----------------------------------------------------------------------------------------------------------------------------

Relational Operators
             

        Relational operators ( used for comparison operations ) supported by C are EQUAL ( == ),
NOT EQUAL ( != ), GREATER THAN ( > ), LESS THAN ( < ), GREATER THAN OR EQUAL TO ( >= ) and LESS THAN OR EQUAL TO ( <= )
.



Following program illustrates the behaviour of relational operators :  



----------------------------------------------------------------------------------------------------------------------------


----------------------------------------------------------------------------------------------------------------------------


Output : 

x : 1
y : 1
z : 0
r : 1


----------------------------------------------------------------------------------------------------------------------------
 
Bitwise Operators

         Bitwise Operators ( used for bit manipulation ) supported by C are BITWISE AND ( & ), BITWISE OR ( | ), BITWISE XOR ( ^ ), BITWISE NOT ( ~ ), LEFT SHIFT ( << ) and RIGHT SHIFT ( >> ). These operators operate at bit level.

Consider two bits bit1 and bit2.
bit1 & bit2 evaluates to True if and only if both bit1 and bit2 are True.
bit1 | bit2 evaluates to True if any of the two bits is True.
bit1 ^ bit2 evaluates to True if and only if one of the bits is True.
~ operator toggles a bit. If bit1 = True, the ~bit1 = False.
' A << 2 ' left shifts the bits of a number (A) by 2 positions.
' A >> 1 ' right shifts the bits of a number (A) by 1 position.
Suppose A = 7 and B = 19. In binary format : A = 00000111 and B = 00010011


Following table shows how bitwise operators do bit manipulation : 

  

Following program demonstrate the behaviour of bitwise operators :  


----------------------------------------------------------------------------------------------------------------------------


----------------------------------------------------------------------------------------------------------------------------


Output : 

7 & 19 = 3
7 | 19 = 23
7 ^ 19 = 20
7 << 2 = 28
7 >> 1 = 3


----------------------------------------------------------------------------------------------------------------------------

  Note that in the above program, there is no demonstration of NOT ( ~ ) operator since the result would be unexpected and will confuse you. You might have noticed that all decimal numbers are represented using 8 bits
( 1 Octet ) but in the system memory it is stored as a sequence of multiple octets (4 in case of 32 bit systems). In our case, other 3 octets would be all 0s. But NOT operation would toggle all those 0s to 1s which would result into a very high value ( outside the range of int ). As a result, some unexpected value would be displayed.

Special Operators
      
          There are some special characters used for specific purpose in C :


     
            Refer to the section on C Tokens and Keywords for sample program demonstrating the use of sizeof operator. Following program illustrates the use of conditional operator :

----------------------------------------------------------------------------------------------------------------------------


----------------------------------------------------------------------------------------------------------------------------

Output :

Greater of 5 and 2 is 5

----------------------------------------------------------------------------------------------------------------------------


Operator Precedence & Associativity
         
            Precedence of operators determines the order of evaluation of expression consisting of multiple operators.
            Higher precedence operators will be evaluated first. Associativity determines whether an expression is evaluated left to right or right to left. Consider the expressions below :




          Following table shows the precedence and associativity of the C operators ( Operator precedence decreases as we move downwards ) :

 

*Note : Unary operators have single operand

Postfix and Prefix Operators
           
               The increment (++) and decrement (--) operators can appear either on left side (prefix) or right side (postfix) of the operand. Following program demonstrates the behaviour of postfix and prefix operators :

----------------------------------------------------------------------------------------------------------------------------



----------------------------------------------------------------------------------------------------------------------------

Output :

x : 6 , a : 6
y : 2 , b : 3
r : 6 , c : 6
s : 3 , d : 2

----------------------------------------------------------------------------------------------------------------------------

No comments:

Post a Comment