Friday, April 19, 2019

C language : Operators

                            

                           OPERATORS IN C LANGUAGE

Operator-
An operator is defined as symbol that specifies mathematical, logical or relational operation to be used in expressions. These operation can be categorised into following terms:-
 
1. Arithmetic operator:    Arithmetic operator can  be applied to any integer or floating point operand.
for eg.    if a=5,b=4

Then,C=a+b=9
         C=a-b=1
         C=a*b=20
         C=a/b=1
         C=a%b=1

2. Logical operator:-    Logical operator are used to calculate expression containing the relational operators.
There are three types of logical operators--
a. Logical AND "&&"
b. Logical OR"||"
c. Logical NOT"!"

for eg. if a=5,b=4

then for AND 
if a>b&&a!=b
Then first condition is true and second condition is also true so, the whole condition is true.
then for OR
if a>b||a==b
Then first condition is true but here second condition is false so, the whole condition is also true.
then for NOT
if !(a>b)=False

3. Bitwise operator:-     Bitwise operator are those operator which perform operation in bit level. These operator include-
1. bitwise AND
2. bitwise OR
3. bitwise XOR
4. shift operator
5. bitwise NOT
1. bitwise AND:-    bitwise AND operator (&) is a small version of the boolean AND(&&) as it performs operation on bits instead of bytes, char ,integer etc.
for eg. 10101 & 01010=00000
2. bitwise OR:-   bitwise OR operator ( | ) is a small version of the boolean OR ( || ) as it performs operation on bits instead of bytes ,char,integer etc.
for eg 10101 | 01010=11111
3. bitwise XOR:-       bitwise XOR operator (^) performs operation on individual bits of the operands.
for eg. 0101 ^ 1010 = 1111
           0101 ^ 1111 = 1010
4. shift operator:-     C supports two bitwise shift operators.
They are shift left "<<" and shift right ">>".
       These operations are simple and responsible for shifting bits either to the left or to the right.

Left shift ( << ) - When we apply a left shift ,every bit in X is shifted to the left by one place. So, the MSB of X is lost , the LSB of X is set to 0.
for eg. if  X = 0001 1101
         then X<<1 = 0011 1010

if X=0001 1101
  then, X<<4 = 1010 0000

Right shift ( >> )- When we apply a right shift,every bit in X is shifted to the right by one place. So, the LSB of X is lost and the MSB of X is sets to 0.
for eg.- if X=0001 1101
then, X>>1 = 0000 1110
  
   if  X = 0001 1101
then  X>>4 = 0000 0001

5. bitwise NOT:-       The bitwise NOT (~), or complement is a unary operation that performs logical negotiations on each bit of the operand. By performing negotiation of each bit, it actually produces the 1's complement of the given binary value. BITWISE NOT operator sets the bit to 1 if it was initially 0 and sets it to 0 if it was initially 1.
for eg. ~10101011 = 01010100

4. Equality operator:-  C language supports two kind of equality operators to compare their operands for strict equility or inequalty. They are equal to  ( == ) operator and not equal to ( !- ) operator. 
Equal to ( ==) operator returns one(1), i.e., true if both the operands are , otherwise it returns zero.
Not equal to (!=) operator returns one(1), i.e.,true if both the operands do not have the same value.


5. Unary operator:-     Unary operator acts on single operands. C language support three unary operator:
       unary minus; increment and decrement operator.
unary minus:  Unary minus ( - ) operator is strikingly different from the binary arithmetic operator that operates on two operands and subtracts the second operand form the first operand.
when an operands is preceded by a minus sign , the Unary minus operator negates its value.
for eg  int a,b = 10;
a=-(b);
then, a=-10

increment operator: Increment operator is a unary operator that increases the value of its operand by 1(one). The increment operator  have two variants- prefix and postfix.
In prefix expression ( ++X ), the operator is applied before an operand is fetched for computation and in postfix expression ( X++ ) the operator is applied after an operand is fetched for computation. 
for eg.
int  x = 10,y,z;
     y = x++;
  z= ++y;

so, y=11 and z = 11

decrement operator :
 The decrement operator is a unary operartor that decreases the value by 1 . The decrement operator have two variants-  prefix and postfix.
In prefix expression ( --X ), the operator is applied before an operand is fetched for computation and in postfix expression ( X-- ), the operator is applied after an operand is fetched for computation.
for eg.  a= 7;
then,  a--  = 6
         --a  =  6
6. Conditional operator:    The conditional operator or the ternary ( ?: ) is just like an if  else statement that can be within expression. Such an operator is useful in situation in which there are two or more alternatives for an expression.
for eg.   large =  (a>b) ? a : b

The conditional operator is used to find largest of two given numbers. First expression  i.e.,  (a>b) is evaluated. if 'a' is greater than 'b' then large = a , else large = b.

7. Assignment operator:-     In C, the assignment operator is responsible for assigning values to the variables while the equal sign ( = ) is the fundamental assigning operator.
 for eg. int x;
           x = 10;
then, ( x+ = 2 ) = 12
          ( x/=5 ) = 2
           ( x*=3 ) = 30
          ( x-=4 ) = 6
8. Comma operator:-      The comma operator in C takes two operand. It works by evaluating the first and discarding its value, and then evaluates the second and returns the value as a result of the expression. Comma operator separates the operands when chained together are evaluated in left to right  sequence with the right most value yielding the result of the expression.
for eg. int a = 3, b = 4, c = 6;

9. sizeof operator:      The sizeof operator is a unary operator used to calculate the  size of datatypes. This operator can be applied to all datatypes. The sizeof operator is used to determine the amount of memory space that the variable/expression/datatypes will take.
      When a type name is used , it is enclosed in parenthesis, but in case of variable names and expression they can be specified with or without parenthesis.
for eg.        int a = 10; 
                  unsigned int result;
                  result = sizeof(a);
       then,    result = 2 

which is the space required to store the  variable 'a' in memory. Since 'a ' is an integer , it requires 2 bytes of storage space.

10. Relational operator:-    A relational operator, also known as a comparison operator, is an operator that compares two values . Relational operator return true or false value, depending on whether the conditional relationship between the two operands holds or not.

Operator                            Meaning                                 Example
   <                                         less than                                     3<5 gives 1
   >                                         greater than                                7>9 gives 0
  >=                                        less than or equal to                  100>= 100 gives 1
  <=                                        greater than or equal to              50>= 100 gives 0


No comments:

Post a Comment

Function

FUNCTION C enables programmer to break up a program into segments commonly known as functions, each of which can be written more or...