Thursday, 20 December 2018

Operators


Operators :
                    C supports a rich set of operators. An operator is a symbol that tells the computer to perform certain mathematical or logical manipulations. C is classified into number of categories. They are :
1.      Arithmetic Operators.
2.      Relational Operators.
3.      Logical Operators.
4.      Conditional Operators.
5.      Bitwise Operators.
6.      Comma Operator.
7.      Assignment Operator.
8.      Increment and Decrement Operators.
1. Arithmetic Operators :
                        Arithmetic operators are used for arithmetic operations like addition, subtraction, division etc.. They are mainly 5 arithmetic operators used in C language.
They are :
1.      *          Multiplication
2.      /           Division
3.      %         Modulus
4.      +          Addition
5.      -           Subtraction           
                                Ex : a=8, b=4 then   a + b = 12
                                                                      a – b = 4
                                                                       a * b = 32
                                                                       a / b = 2
                                                                 a % b = 0
Arithmetic Operators are further classified into 3 types.
They are :
1.      Integer Arithmetic Expression
2.      Real or Float Arithmetic Expression
1.      Mixed mode Arithmetic Expression
1.      Integer Arithmetic Expression:
When both operands are of integer type then such type of expression
                       using arithmetic operators is called Integer Arithmetic Expression.
                                                Ex :  3+2, 3-2, etc.
2.      Real or Float Arithmetic Expression :
When both the operands are of real type then such type of expression using arithmetic operators is called Real or Float Arithmetic Expression.
Ex :  1.2+2.1,   3.2*1.2   etc…
3.      Mixed mode Arithmetic Expression :
        When one operand is of integer type and other is of real type then such                        expression using arithmetic operators is called Mixed mode Arithmetic Expression.
Ex :  1.2+3 ,   3*1.2 , etc.
2. Relational Operator :
                                Relational Operators are used to create a relationship among the operands. They
     are used for comparison purpose. . It is in the form as
                                          Operand1     operator    operand2;
                                The value of a relational expression is either 1 or 0 depending on relation either true or false. These are used in mostly decision making and branching statements, and looping statements.
C supports 6 relational operators. They are :
1.      <        Less Than
2.      >        Greater Than
3.      <=      Less than and equal to
4.      >=      Greater than and equal to
5.      ==      Equal to
6.      !=        Not equal to
                 Ex : If a=5 b=3, then the various relational expressions would be as follows :
                                                Relational Expression     Result
                                                                a>b                        true
                                                                a<b                        false
                                                                a<=b                      false
                                                                a>=b                      false
                                                                a==b                      false
                                                                a!=b                       true                  
3. Logical Operators :
                                  The logical operators are used to combine or construct compound conditional expressions and also to negate expressions. These are used in decision-making statement and some looping statements like if, switch ,while , etc. These statements have either true(1) or false(0). 
 C supports 3 types of logical operators.                
  They are :
1.      Logical AND  (&&)
2.      Logical OR     (||)
3.      Logical NOT   (!)

1.      Logical AND (&&) :
 In Logical AND operator if both operands(expressions)are true then the result will be true i.e. 1 otherwise the result will be false i.e. 0. The general syntax for logical AND operator using the symbol && is
               op1 && op2
The truth table for Logical AND operator is shown below
Op1
Op2
Op1 && Op2
0
0
0
1
0
0
0
1
0
1
1
1








                                                         
2.      Logical OR  (||) :
In Logical OR operator if any one of the operand(expression)is true then the result will be true i.e. 1 otherwise the result will be false i.e. 0. The general syntax for logical OR operator using the symbol || is op1 || op2
The truth table for Logical OR operator is shown below

Op1
Op2
Op1|| Op2
 0
0
0
1
0
1
0
1
1
1
1
1










3.   Logical NOT  (!)  :
The logical NOT operator gives the negation of an operand(expression). The general syntax for Logical NOT operator using the symbol ! is !op

Op
!Op
1
0
0
1
       
       




4. Conditional Operator :
                  Conditional operators are also called Ternary operator. These operators are used instead of “ if ” statement. The syntax for conditional operator is :
                                        exp1 ? exp2 : exp3 ;
                  Here first the expression exp1 will be computed which is a conditional expression. If exp1 is true then exp2 will be executed. But if exp1 is false then exp3 will be executed.
                                                Ex :         c=(a>b)?a-b:a+b;
                                                                if a=5 and b=4 then according to the conditional operator
                                                                c=(5>4)?5-4:5+4;

c=1 ( as 5 is greater than 4 so exp2 is executed i.e. 5-4=1 )

 5. Bitwise Operator :
Bitwise operators are specially used for low level programming. These operators are used for the manipulation of binary data i.e. bits. These operators should not be of float or double type. C supports 6 types of Bitwise operators.
                        They are :
1.      Bitwise AND ( & )
2.      Bitwise OR  (| )
3.      Bitwise Exclusive OR  ( ^ )
4.      Bitwise Shift Left  ( << )
5.      Bitwise Shift Right  ( >> )
6.      Bitwise Complement(bitwise NOT)  (  ~  )         
1. Bitwise AND (&):
                 AND operator is a binary operator which needs two operands to operate.                                                   AND operator operates by adding the individual bits of two operands one by one. If both operand has 1 at same position then the result is 1 otherwise the result is 0.
                       Ex :   Let a =  00101010
                                         b = 10011001
                                then  c = a & b = 00001000
2. Bitwise OR (|):
                         OR operator is a binary operator which operates on two operands. If any one of the bit value is 1 then it results to 1 otherwise 0.
                       Ex :   Let a = 00101010
                                         b = 10011001
                                                       then  c = a | b = 10111011
3. Bitwise Exclusive OR (^):
                         Exclusive OR is a binary operator which operates on two operands. This operator returns 1 if one operand has the bit value 1 and the other has 0 at same position or vice-cersa otherwise it returns 0.
                                            Ex :   Let a = 00101010
                                         b = 10011001
                                                       then  c = a ^ b = 10110011
4. Bitwise Shift Left (<<):
                         Bitwise shift left operator transfer the bits of data to left side. The left operand is the data and the right operand is the no. of times the shift operation is to be done. Here the most significant bit will be discarded and the least significant bit will come out to be 0.
                                                The syntax is :     op << n
                Ex :             x = 0100 1001 1100 1011
                           X << 3 = 0100 1110 0101 1000
5. Bitwise Shift Right (>>) :
                                  Bitwise shift right operator transfers the bits of data to right side. The left operand is the data and the right operand is the no. of times the shift operation is to be done. Here the least significant bit will be discarded and the most significant bit will come out to be 0.
                                                The syntax is   op >> n
Ex :            x = 0100 1001 1100 1011
                                          X >> 3 = 0000 1001 0011 1001
6. Bitwise Complement (bitwise NOT)(~) :

                                 One’s complement operator is a unary operator which is similar to NOT. It converts 0 to 1 and 1 to 0.
                               Ex :                Let a = 5
                it’s binary equivalent is   a= 00000101
                                                                ~a= 11111010

6. Comma Operator :
                         When number of statements occur in a C program having a relationship between expressions, then we can write all expressions or statements in a single expression using the comma operator.
                         Mostly the comma operator is used in looping statements like while, do-while and for.
EX :                a=12;
                         b=10;
                   c=a+b;
          So, the above statement can be written as  c=(a=12,b=10,a+b);
Type Operator :
                   Type operator is used for conversion purpose or casting purpose. So it is also called convert operator. This operator converts float type data into integer form and vice-versa. It is also used for casting a value and process to convert from one form to another is called Casting.
                   The syntax is:
                                    (type) v or e;
Where v is variable and e is an expression.
Ex: int a,b;
       float c;
        a=5;
        b=2;
        c=a/b;
If we divide a by b, then result stored in the C variable be 2.0. But by using the type operator, we can get the float value as:
                                                c=(float)a/b;

7. Assignment Operator :
                      Assignment  operator s are used for assigning an expression or value to a variable. Assignment operators are further divided into two types. They are
·         Simple Assignment Operator
·         Shorthand Assignment Operator
                                                                                (or)
                                                               Arithmetic Assignment Operator
Simple Assignment Operator :
                         Simple assignment operator is = (equal to). The general syntax of simple assignment operator is :
                                                V=constant value (or) variable (or) expression;
                          Ex:   a=3;
                                  b=c;     
Shorthand Assignment (or) Arithmetic Assignment Operator:
                        These operators have  = (equal to) sign with all arithmetic operators. The general syntax is :
                         v arithmetic operator = constant value or expression;
                 where v is the variable and expression can be an arithmetic expression i.e.+,-,/,*,%.
                 Ex:                         Simple Assignment         Shorthand Assignment
                                                          i=i+1;                                           i+=1;
                                                          a=a*b+c;                                   a*=b+c;
                                                          b=b%c;                                       b%=c;

8. Increment/Decrement Operator:
                         These operators are also called unary operators. Another name for increment and decrement operators is counter operator. Increment Operator (++ ) are used for incrementing the value by 1 and Decrement Operator( -- )  is used for decrementing the value by 1. These operators are further divided into types. They are
·         Prefix Increment/Decrement
·         Postfix Increment/Decrement


Prefix Operator :
                In the prefix increment operator first of all value will be incremented and then incremented value will be assigned to a variable. Similarly in the prefix decrement operator first of all the value will be decremented and then decremented value be assigned to the variable. They are represented as :
                                                ++v;   prefix increment
                                                --v;     prefix decrement  where  v is the variable.
Postfix Operator :
                In the postfix increment operator first of all value will be assigned to a variable and then value will be incremented. Similarly in the postfix decrement operator first of all the value will be
assigned to a variable and then value will be decremented.
 They are represented as :
                                                v++;   postfix increment
                                                v--;     postfix decrement  where v is the variable.
Ex:   x=7                                     x=7
        Y=++x                                y=x++
After processing :            After processing :    
        Value of  y is 8              value of y=7
        Value of  y is 8                value of x=8
        x=7                                     x=7
        Y=--x                                  y=x--
After processing :            After processing :    
        Value of  y is 6              value of y=7
        Value of  y is 6                value of x=6

Order of Operators Precedence :
                                                Order of precedence means the rank in which all the operators operate in a C expression. The order of precedence and associatively between these operators is shown below :
Operator
Description
Associatively
Rank
()
Parenthesis
Left to right
1
[]
Square bracket
Left to right
1
+
Unary plus
Right to left
2
-
Unary minus
Right to left
2
++
increment
Right to left
2
--
Decrement
Right to left
2
!
Logical negation
Right to left
2
~
One’s compliment
Right to left
2
*
indirection
Right to left
2
&
Address
Right to left
2
Size of
Size of an object
Right to left
2
(type)
Type cast
Left to right
2
*
multiplication
Left to right
3
/
Division
Left to right
3
%
modulus
Left to right
3
+
Addition
Left to right
4
-
subtraction
Left to right
4
<< 
Left shift
Left to right
5
>> 
Right shift
Left to right
5
< 
Less than
Left to right
6
<=
Less than or equal to
Left to right
6
> 
Greater than
Left to right
6
>=
Greater than or equal to
Left to right
6
==
equality
Left to right
7
!=
inequality
Left to right
7
&
Bitwise AND
Left to right
8
^
Bitwise XOR
Left to right
9
|
Bitwise OR
Left to right
10
&&
Logical AND
Left to right
11
||
Logical OR
Left to right
12
?!
Conditional operator
Right to left
13
=
Assignment operator
Right to left
14
,
Comma operator
Left to right
15


No comments:

Post a Comment