Operators

Operators

5 | Operators


     Operators are used in C programming language to do operations on variables and datas. Operators are nothing but they are symbols used to do mathematical or logical manipulations. They commands to the computer to do certain operations.


5.0 | Classification of Operators :

     1.   Arithmetic Operators
     2.   Relational Operators
     3.   Logical Operators
     4.   Assignment Operators
     5.   IncreDecrement Operators
     6.   Bitwise Operators
     7.   Conditional Operators
     8.   Special Operators


5.1 | Arithmetic Operators :

     Arithmetic operators are used to perform arithmetic operations like addition, subtraction, multiplication, etc. In C, there are total five arithmetic operators : + , - , * , / and % .


Operator Used for Example
+ Addition a + b
- Subtraction a - b
* Multiplication a * b
/ Division a / b
% Remainder a % b


Example :

Input :

#include<stdio.h>

  int main()
    {
      int a,b,add,sub,mul,rem;
      float div;

      clrscr();

      printf("Enter first number :\n");
      scanf("%d",&a);

      printf("Enter second number :\n");
      scanf("%d",&b);

      add = a+b;
      sub = a-b;
      mul = a*b;
      div = a/b;
      rem = a%b;

      /*displaying value of calculation with inputs*/
      printf("\nAddition of %d and %d is %d\n",a,b,add);
      printf("Subtraction of %d and %d is %d\n",a,b,sub);
      printf("Multiplication of %d and %d is %d\n",a,b,mul);
      printf("Division of %d and %d is %f\n",a,b,div);
      printf("Remainder of %d and %d is %d\n\n",a,b,rem);

      getch();
      return 0;
    }


Output :

Enter first number :
4
Enter second number :
5

Addition of 4 and 5 is 9
Subtraction of 4 and 5 is -1
Multiplication of 4 and 5 is 20
Division of 4 and 5 is 0.800000
Remainder of 4 and 5 is 4


5.2 | Relational Operators :

     Relational operators are used to find out relation between two operands. From table (given below), you can understand more about relational operators.


Operators Meaning Example
< Less than a < b
> Greater than a > b
<= Less than or equal to a <= b
>= Greater than or equal to a >= b
== Equal to a == b
!= Not equal to a != b


Example :

Input :

#include<stdio.h>

  int main()
    {
      int a,b;

      clrscr();

      printf("Enter first number :\n");
      scanf("%d",&a);

      printf("Enter second number :\n");
      scanf("%d",&b);

      if(a==b)
        { printf("\n%d is equal to %d",a,b); }
      else if(a<b)
        { printf("\n%d is less than %d",a,b); }
      else
        { printf("\n%d is greater than %d",a,b); }

      getch();
      return 0;
    }


Output :

Enter first number :
4
Enter second number :
5

4 is less than 5


     Relational operators are used to find out relation between two expressions. From table (given below), you can understand more about relational operators.


Operators Meaning Example
&& Logical AND a > b && c=10
|| Logical OR x < y || z=10
! Logical NOT !(m=n)

Logical AND :
     In expression a > b && c=10, if both the conditions are true, then and only then the whole expression is true.

Logical OR :
     In expression x < y || z=10, if any one condition or both the conditions are true, then the whole expression is true.

Logical NOT :
     In expression !(m=n), if m is equal to n, then the expression returns that m isn't equal to n and vice-versa.


5.4 | Assignment Operators :

     Assignment operators are used to assign the value of an expression to a variable. This type of operator evaulates an expression on the right of the whole expression and assign its value to the variable on left.

The general format is :
          code ope= exp;

     where, code is variable
     and ope is operators
     and exp is expression

     Here, the meaning of code ope= exp; is code = code ope exp;.

For eg.: x += y; is can be also written as x = x + y; and vice-versa.

The table given below, lists assignment operators with there examples :


Operators Meaning Example
+= Addition assign x += y i.e. x = x + y
-= Subtraction assign x -= y i.e. x = x - y
*= Multiple assign x *= y i.e. x = x*y
/= Division assign x /= y i.e. x = x/y
%= Modulo assign x %= y i.e. x = x%y



5.5 | IncreDecrement Operators :

     The increment operator (++) add 1 to the value of variable and the decrement operator (--) subtracts 1 from the value of variable. Increment operators and decrement operators both have two types : pre and post i.e.      

  • Increment Operators :
         
    1. Pre Increment Operators (++variable) : The operator first increase the value of variable by 1 and then assign to the variable on left.
      For eg.: If a=3; and b=++a; , then a=4 and also b=4.

    2. Post Increment Operators (variable++) : The operator first assign the value to the variable on left and then increase the value of variable by 1.
      For eg.: If a=3; and b=a++; , then a=4 and b=3.

  • Decrement Operators :
         
    1. Pre Decrement Operators (--variable) : The operator first decrease the value of variable by 1 and then assign to the variable on left.
      For eg.: If a=3; and b=--a; , then a=2 and also b=2.

    2. Post Decrement Operators (variable--) : The operator first assign the value to the variable on left and then decrease the value of variable by 1.
      For eg.: If a=3; and b=a--; , then a=2 and b=3.


5.6 | Bitwise Operators :

     Bitwise operators are used to perform operations bit by bit. This operator manipulate/operate each bit of data. Bitwise operators can't be apply on float or double data-types.
     In other words, the bitwise operator first converts given integer number into binary number and then perform operation on each binary number. After completing the operation, it converts binary number into integer number and then shows that integer number as output.
         Various types of bitwise operators are listed below :


Operators Meaning Examples
& Bitwise AND View
| Bitwise OR View
^ Bitwise Exclusive OR View
<< Bitwise Left Shift View
>> Bitwise Right Shift View
~ Bitwise Complement View
     Take three variables x, y and z of datatype int (i.e. integer).

     Let x = 9, y = 11 and z = x & y
Now, the bitwise and (&) operator will convert 9 and 11 (i.e. values of x and y) in binary. Binary of 9 and 11 is 1001 and 1011 respectively.

     By using truth table for AND operator :
x        : 1 0 0 1
y        : 1 0 1 1
x & y : 1 0 0 1

      Now, 1001 is in binary. So, it will be converted into decimal (the decimal value of binary number 1001 is 9).

      Now, the value 9 is assign to the variable z (i.e. z=9).

      In short, if x = 9, y = 11, z = x & y then z = 9. Close
     Take three variables x, y and z of datatype int (i.e. integer).

     Let x = 9, y = 11 and z = x | y
Now, the bitwise or (|) operator will convert 9 and 11 (i.e. values of x and y) in binary. Binary of 9 and 11 is 1001 and 1011 respectively.

     By using truth table for OR operator :
x       : 1 0 0 1
y       : 1 0 1 1
x | y  : 1 0 1 1

      Now, 1011 is in binary. So, it will be converted into decimal (the decimal value of binary number 1011 is 11).

      Now, the value 11 is assign to the variable z (i.e. z=11).

      In short, if x = 9, y = 11, z = x | y then z = 11. Close
     Take three variables x, y and z of datatype int (i.e. integer).

     Let x=9 and y=11
     Since, we used bitwise operator exclusive or (^).
    Hence, first of all, it converts 9 and 11 (values of x and y) in binary. The binary of 9 and 11 is 1001 and 1011 respectively.

     By using truth table for Exclusive OR operator :
x      : 1 0 0 1
y      : 1 0 1 1
x ^ y : 0 0 1 0

      Now, 0010 is in binary. So, it will be converted into decimal (the decimal value of binary number 0010 is 2).

      Now, the value 2 is assign to the variable z (i.e. z=2).

      In short, if x = 9, y = 11, z = x ^ y then z = 2. Close
     Take two variables x and y of datatype int (i.e. integer).

     Let x=9 and y=x<<2
Here, x<<2 means x is left shifted by 2.
Let us simplify it :

     Since, we used bitwise operator left shift (<<).
    Hence, first of all, it converts 9 (value of x) in binary (binary of 9 is 1001) and then the operator will shift 1001 towards left by 2. In other words, the operator will add two zeros after 1001 i.e. 1001 becomes 100100.

     Now, 100100 is in binary, so it will be converted into decimal (the decimal value of binary number 100100 is 36).
And the value 36 is assign to the variable y.

     In short, if x=9 and y=x<<2, then y=36. Close
     Take two variables x and y of datatype int (i.e. integer).

     Let x=11 and y=x>>2
Here, x>>2 means x is right shifted by 2.
Let us simplify it :

     Since, we used bitwise operator right shift (>>).
    Hence, first of all, it converts 11 (value of x) in binary (binary of 11 is 1011) and then the operator will shift 1011 towards right by 2. In other words, the operator will remove last two digits from 1011 i.e. 1011 becomes 10.

     Now, 10 is in binary, so it will be converted into decimal (the decimal value of binary number 10 is 2).
And the value 2 is assign to the variable y.

     In short, if x=11 and y=x>>2, then y=2. Close
     Take two variables x and y of datatype int (i.e. integer).

     Let x=9 and y=~x
     Since, we used bitwise operator complement (~).
    Hence, first of all, it converts 9 (value of x) in binary. The binary of 9 is 1001.

     By using truth table for Complement operator :
x         : 1 0 0 1
y = ~x : 0 1 1 0

      Now, 0110 is in binary. So, it will be converted into decimal (the decimal value of binary number 0110 is 6).

      Now, the value 6 is assign to the variable y (i.e. y=2).

      In short, if x = 9 and y = ~x then y = 6. Close



5.7 | Conditional Operator :

     Conditional operator is also called as ternary operator. It is used for conditional statements.

The general format is :
          condition ? expression1 : expression2 ;

     Here, if condition is true, then expression1 is evluated and the result of expression1 becomes the result of the whole expression. But if condition is false, then expression2 is evaluated and the result of expression2 becomes the result of the whole expression.

For eg.: int c = (a==b) ? x : y ;

     Here, if value of a is equal to value of b, then expression x is evaluated and the result of expression x becomes the result of whole expression. But if, value of a isn't equal to value of b, then expression y is evaluated and the result of expression y becomes the result of whole expression.


5.8 | Special Operator :

     C has set of some special operators like comma operator, sizeof operator, pointer operators and member selection operators. Comma and sizeof operators are discussed here and remaining operators are discussed after some lessons.      

  • Comma Operator : In C, many expressions are separated with comma operator. These expressions are evaluated from left to right and the result of right most expression is the result of combined expression.

         For eg.: result = (a=2,b=3,a*b);

         Here, values of a and b are 2 and 3 respectively and right most expression is a*b. Since 2*3 is 6, hence a*b is also 6. i.e. the value of result is 6.

  • sizeof Operator : The sizeof operator gives the size of operand in bytes. The operand may be a variable, a constant or a data-type.

         For eg.:
              a=sizeof(code);
              b=sizeof(425);
              c=sizeof(double);




Comments