Operators and its Types in C

    Operators act as connectors and they indicate what type of operator is been carried out. The values that can be operated by these operators are called Operands.

eg: 5 + 2 --> 5 and 2 are operands, + --> operator.

There are 3 types of operator.

1. Unary Operator.

2. Binary Operator.

3. Ternary Operator.


Unary Operator:

operator that acts upon only one operand is known as unary operator. C-unary operators are ,

1.1 Unary minus.

1.2 Logical NOT operator

1.3 Bitwise Compliment.


1.1 Unary minus: 

        Any positive operand associated with an unary minus operator gets its value changed to negative.

Let a=3 and b=4

c=a+(-b)

c=3+(-4)

c=3-4

c=-1

Here 'c=-1', because b was initially a positive integer variable, when operated by unary minus gets its value changed to negative.


1.2 Logical NOT operator: 

        The logical NOT is used to obtain the logical complement of the operand. The result of a logical NOT is TRUE, when the  operand is FALSE. and vice-versa.


1.3 Bitwise Compliment: 

        It is used to obtain once compliment of a binary sequence. This means each zero gets  changed to 1 and each 1 gets changed to 0 during bitwise compliment operation.



2. Binary Operator: 

These operators acts upon two operands. Hence the name binary. The binary operators are further classified into four categories.





2.1 Arithmetic Operator: 

    these are used to perform the basic arithmetic operations. such as, Addition, Subtraction, Multiplication and Division.

C language does not provide an exclusive exponential constant. This can be performed using pow(), which is a library function.


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

| Operation      | Operator |

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

| Addition           |    +        |

| Subtraction     |    -    |

| Multiplication |    *        |

| Division            |    /        |

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


2.2 Relational Operator:

These are used to compare two operands. They define the relationship existing between two constants or variables. The results will be either TRUE or False.

The value of TRUE is non-zero (i.e 1). And that of False in zero (i.e 0).

There are six relational operators used in C.


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

| operator |          Meaning                  |

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

|    <           | Less than                       |

|    >           | Greater than                    |

|   <=   | Less than or equal to       |

|   >=   | Greater than or equal to |

|   ==   | Equal to                |

|   !=           | Not Equal to               |

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


2.3 Logical Operator:

There are 3 logical operators and they are AND, OR, NOT.


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

| Operator | Name of the Operator |   Meaning     |

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

|   &&    | double ampersand    | Logical AND |

|   ||            | double pipeline            | Logical OR   |

|    !            | exclamation                  | Logical NOT |

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


2.3.1 AND: 

    the logical AND operation is used to perform ANDing operation on two logical operands. It is equivalent to multiplication.


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

| operand 1 | operand 2 | op1 && op2 |

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

| false          |  false | false        |

| false          |  true         | false        |

| true           |  false         | false        |

| true           |  true         | true        |

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


2.3.2 OR:     

    the logical OR operation is used to perform ORing operation on two logical operands. It is equivalent to addition.


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

| operand 1 | operand 2 | op1 || op2 |

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

| false         |  false     | false      |

| false         |  true     | true      |

| true          |  false     | true      |

| true          |  true     | true      |

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


2.3.3 NOT: 

    the logical NOT is used to obtain the logical complement of the operand. The resultof a logical NOT is TRUE, when the operand is FALSE. And vice-versa.

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

| operand | NOT operand |

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

| false   |    true             |

| true   |    false            |

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


2.4 Bitwise operator: (0's and 1's) 

There are six types of 6 Bitwise operator.


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

| Sl.No | Operator | Name of symbol      |       Meaning                |

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

|   1     |    &           |  Ampersand             |  Bitwise AND           |

|   2     |    |             |  Pipeline                   |  Bitwise OR           |

|   3     |    ^            |  caret                        |  Exclusive OR (XOR)  |

|   4     |    ~            |  tilde                          |  1's complement           |

|   5     |    <<         |  double less than       |  left shifting                  |

|   6     |    >>         |  double greater than |  right shifting            |

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


2.4.1 The result of bitwise ANDing 1, when both bits are 1, otherwise it is 0.


a= 0000 0100

b= 0000 0011

     _______________

      a&b= 0000 0000

2.4.2 The result of bitwise ORing is 1, if one of the bit is 1, otherwise it is 0.


     a= 0000 0100

b= 0000 0011

      _____________

       a|b= 0000 0111


2.4.3 The result of XORng is 1, if the bits are different, otherwise it is 0.


        a= 0000 0100

b= 0000 0011

         _______________

          a^b= 0000 0111


2.4.4 The bitwise complement operator is an unary operator that reserves the state of each bit within an integer or a character. It means 0 get 1 and vice-versa.

Let a=4 its binary equivalent is 100 

then b=~a

     b=~100

     b=100


2.4.5 The left shift operator shifts the bits toward left.

Let binary number 10000100 is equivalent of 132 in decimal

1 0 0 0 0 1 0 0   -----> After Shifting 0 0 0 0 1 0 0 1

2.4.6 The right shift operator shifts the bits towards right.


Let binary number 10000100 is equivalent of 132 in decimal

1 0 0 0 0 1 0 0   -----> After Shifting 0 1 0 0 0 0 1 0


3. Ternary operator:

The ternary operator is a conditional operator in C. There is only one such operator in C. It takes 3 operands. The general form is;

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

| <expression> ? <value 1> : <value2> ; |

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

expression: Relational expression

value 1   : value to be assigned on True

Value 2   : Value to be assigned on False.

Ex: c = a < b ? a : b;

Here C will be assigned the value of a is less than b is True. Otherwise it will be assigned the value of b.