What is Operator? What are the different types of Operators.Define.

What is Operator?


An operator is a symbol in C, such as +,-,&&, or ==, which helps the user to perform several mathematical and logical computations. As operator is used basically to work upon certain data and produce an output as a result operation. The operation included in the C programming language is used to operate not only on numbers but also on data and variables. The operator in C are classified into various types.

  1. Unary Operators
  2. Assignment Operators
  3. Arithmetic Operators
  4. Increment and Decrement Operators
  5. Relational Operators
  6. Logical Operator
  7. Bitwise Operators
  8. Conditional Operators
  9. Special Operators
  10. Shorthand Assignment Operators

What are the different types of operator?

Operators are following types

  1. Unary Operator

    The unary operator in care used to act upon only one operand, to produce a new value. The unary oprator appear before the operand and are associated with the operand from left to right. The most commonly used unary operator in C is the unary minus oprator. In the unary minus operator, a minus sign precedes the operand assigning a negative value to the operand. 
    Example
    a=3;
    b=4;
    c=a + (-b);

    In this case, the value of c,when calculated, is -1. This is because b, which initially has a positive value, is changed to negative when preceded by the unary minus operator. 
    -------------------------------------------------------------------------------------------------------------------
    WAP using the Unary Operator
    -------------------------------------------------------------------------------------------------------------------
    #include <stdio.h>
    void main()
    {
      int a , b;
      int sub;
      a = 5;
      b = 3;
      sub = a + b;
      printf("The subtraction of a and b is = %d.", sub);
      getch();
     }
    ---------------------------------------------------------------
    Output:
    The subtraction of a and b is 2.
  2. Assignment Operator

    Assignment operators are used for assigning a new value to a variable. The equal sign (=) is the assignment operator in C. When the C compiler encounter an = sign, it processes on the right side of the operator and the assigns the result to the variable on the left side of the operator.
                The general formate for an assignment operator is
                  var = expression;
                Some example of assignment operator are shown below
               
    a = a + c; a = b + c*d;
         i = i + 2; i = j + 2;
       
    You can also use arithmetic operator  in C to assign a single value to multiple variable in one statement as shown in the following expression:
           
    i = j = k = 10;
    which assigns the value 10 to i, j, and k.
    Let's see the following program to understand the assignment Operator.
    ------------------------------------------
    Q.) WAP using Assignment Operator.

    #include<stdio.h>
    main()
    {
      int a, b, c, d;
      printf("ENTER VALUES OF a,b, c, d;\n");
      a = a + b*c + d;
      printf("\na = %d",a);
      getch();
     }
    ------------------------------------------
    Output:
    ENTER VALUES OF a,b, c, d :
    5 5 7 8
    a = 48
    ------------------------------------------

    Shorthand Assignment Operator

    You can use the assignment operator of C in a compact manner in an expression. These compact expressions are known as the shorthand assignment operators. The shorthand way of working with the operator is also associate with arithmetic and bitwise operators. The various shorthand assignment operator used in C are listen in following table.

    Shorthand Assignment Operators
    Operator
    Assignment
    Shorthand Assignment
    +
    a = a + b
    a+= b;
    -
    a  = a –  b
    a-= b;
    *
    a = a * b
    a*= b;
    /
    a = a / b
    a/= b;
    %
    a = a % b
    a%= b;
    &
    a = a & b
    a&= b;
    |
    a = a | b
    a|= b;
    ^
    a = a ^ b
    a^= b;


    The use of shorthand assignment operator has three advantages:
    (1)  The statement is more concise and easier to read.
    (2)  The statement is more efficient
    (3)  What appear on the left-hand side need not be repeated and therefore it becomes easier to            write.
         Let's consider the following program to understand the shorthand assignment operators in C.
    -------------------------------------------------------------------------------------------------------------------
    Q.) WAP using Shorthand Assignment Operators

    #include<Stdio.h>
    void main
    {
      int x, y, z;
      printf("\nEnter Value of x, y, z;");
      scanf("%d%d%d",&x,&y,&z);
      x+=y;
      y-=z;
      z*=x;
      printf("\nx = %d", x);
      printf("\ny = %d", y);
      printf("\nz = %d", z);
      getch();
    }
    ---------------------------------------------------------------
    Output:
    Enter Value of x, y, z: 5 8 10
    x = 13
    y = -2
    z = 130
    ---------------------------------------------------------------
  3. Arithmetic Operator

    C has all the basic type of arithmetic operators. They are listed in following table.

    Arithmetic Operator in C

    Operator
    Example
    Function
    +
    a + b
    A ddition or unary Plus
    -
    a – b
    Subtraction or unary minus
    *
    a * b
    Multiplication
    /
    a / b
    Division
    %
    a % b
    Modulo Division


    The +, -, *, and \ arithmetic operator perform the usual arithmetic operations in a C program. However, the % (modulo) operator is used to return the remainder value after dividing into integers. The % operator cannot be used for float data type or double data type. If both operands a and b are integers then the expression a/b provides integer division. Even if the target is floating point variable, it provides integer division. The arithmetic operators have the normal precedence rules as Follows:
    1. Unary operators like -, + are evaluated.
    2. The multiplication (*) and divided (/,%) operators are evaluated.
    3. The addition (+) and subtraction (-) operators are evaluated.
    4. The assignment operator is evaluated.
    5. The expression are evaluated from left to right for unary operators.
        
        Let's consider the following program to understand the arithmetic operators in C

    ____________________________________________________________________________
    Q.) WAP using the arithmetic operators
    -------------------------------------------------------------------------------------------------------------------
    #include<stdio.h>
    main()
    {
      int a, b, c, d;
      int sum, sub, mul, rem;
      float div;
      printf("Enter Values of b, c, d: ");
      scanf("%d%d%d", %b,%c,%d);
      sum = b + c;
      sub = b - c;
      mul = b * c;
      div = b / c;
      rem = b % d;
      a = b/c * d;
      printf("\nsum = %d, \nmul = %, \ndiv= %f", sum, sub, mul,      div);
      printf("\nRemainder of division of b & d is %d.", rem);
      printf("\n a = %d,a);
      getch();
     }
    ---------------------------------------------------------------
    Output:
    Enter Values of b, c, d : 10 5 3
    sum = 15
    sub = 5
    mul = 50
    div = 2.000000
    Remainder of division of b & d is 1.
    a = 6
    ---------------------------------------------------------------
  4. Increment and Decrement Operators

    You  can increment and decrement the value of a variable by using the increment (++) or decrement (--) operator. The increment operator is used to add 1 toits operand and the decrement operator is used to subtract 1 fromits oprands. In others words, the increment operator increase the value of the integer by 1 and the decrement operator decrease the value of the integer by 1. These operator can be applied only two variable by using the either the prefix from or the postfix form.
          When prefix form is used, the value of the variable is either increment or decrement first and is then assigned. however, in the postfix form, the value is used and only after the assignment operator has performed the operations,  that the value is incremented or  drcremented.
         The following are example of using the increment and decrement operator in prefix form.
    a = ++b; i = --j

    The above both statement are equal to following assignment statement:
    b = b + 1; a = b;
    j = j - 1; i = j;

    The following are example of using the increment and decrement operator in post form
    a = b++; i = j--;

    The above both statement are equal to following asssignment statement;
    a = b; b = b + 1;
    i = j; j = j - 1;

    Let's consider the following program to understand the increment and decrement operators:
    -------------------------------------------------------------------------------------------------------------------
    Q.) WAP using the increment and Decrement Operators
    -------------------------------------------------------------------------------------------------------------------
    #include<stdio.h>
    void main()
    {
      int i, j, k, l, m, n,
      i = 7;
      j = 3;
      k = i++ + -j;
      l = ++i + -j;
      m = i-- + -j;
      n = --i + -j;
      printf("i = %d,\tj = %d", i, j);
      printf("\nk = %d, \tl = %d, \tm = %d, \tn = %d", k, l, m, n);
      getch();
    }
    ---------------------------------------------------------------
    Output:
    i = 7, j = 3,
    k = 4, l = 6, m = 6, n = 4
    ---------------------------------------------------------------

Post a Comment

0 Comments