-->

Types of operators

6 minute read

         Types of operators

Explain operators in c language.
There are eight types of operator in c language

  1. arithmetic operators
There are five arithmetic operators.
Ø  +         Addition or unary plus
Ø  -          Subtraction or unary minus
Ø  *          Multiplication
Ø  /          Division
Ø  %        Modulo operator
Program
#include<stdio.h>
#include<conio.h>
void main ()
{
  // Variable Declaration
  Int a=100;
  Int b=2;
  Int result;
 result = a - b; // subtraction
 printf ("a - b = %d”, result);
 }

Output
a - b = 98
   2.  Relational operator
   It Compare two operands and depending upon their relation.

There are six relational operators. They are,


Ø <  less than
Ø >  greater than
Ø <= less than or equal to
Ø >= greater than or equal to
Ø == equal to
Ø != not equal to

 Program
#include<stdio.h>
#include<conio.h>
void main()
{
           // Variable Declaration
           int a = 25, b = 5;
           if( a>b )
           printf (A is Big);
           else
           printf ( B is Big);
           // Wait For Output Screen
           getch();
}
Output
A is Big.

 3. Logical Operators

  AND,OR operators are used when we want to use two or more     Conditions.

  Types Of Logical Operators
Ø && Logical AND
Ø || Logical OR
Ø !  Logical NOT

Logical And (&&) Operator 

If both the conditions are true, then the operation becomes true.

Syntax

expr1 && expr2

Logical And Operator Example

if( (a>10) && (a<20) )
  printf(A is between 10 and 20);

Logical And Operator Example 

#include<stdio.h>
#include<conio.h>
void main()
{
      // Variable Declaration
    int num1 = 50;
    int num2 = 40;
 
    if(num1>=40 && num2>=40){
        printf("both condition are true");
    }
      // Wait For Output Screen
      getch();
}
Output:
Both condition are true 

Logical OR (||) Operator

Logical OR  Operator Definition

If any one of the condition is successful, then the operation becomes true

Syntax

expr1 || expr2

 Example

if( (a>10) ||  (a<20) )
  printf(A is greater than 10 or less than  20);

Logical OR  Operator Example 

#include<stdio.h>
#include<conio.h>
void main()
{
      // Variable Declaration
    int num1 = 30;
    int num2 = 40;
 
    if(num1>=40 || num2>=40){
        printf("From two condition one condition is true");
    }
      // Wait For Output Screen
      getch();
}
 Output
From two condition one condition is true

Logical NOT (!) Operator 

If a condition is true, Logical NOT operator reverses the result and produces false output.

Syntax

!expression

Example

if( !(a>10))
  printf(A is less than 10 );

 Program

#include<stdio.h>

#include<conio.h>

int main()

{

    int number;

     printf("Please enter an integer value:");

    scanf("%d", &number);

     if(!(number>40)){

        printf("no is greater than 40 - TRUE");

    }else{

        printf("no is greater than 40 - FALSE");

    }

      // Wait For Output Screen

      getch();

     return 0;

}

Output

Please enter an integer value: 36

no is greater than 40 - FALSE

 4.  Assignment Operators

It is used to assign the result of an expression to a variable.

Syntax

identifier = Expression

There are five assignment operators. They are,

a=a+1          a+=1
a=a- 1          a-= 1
a=a*2          a*=2
a=a/2           a/= 2
a=a%2         a%=2

Program

#include <stdio.h>

#include<conio.h>

void main(){

    int a = 10; 

    // = Operator

    int b = a; 

    printf("b = %d\n",b);

    // += Operator

    b += 10;

    printf("b += 10;b = %d\n",b);

    // -= Operator

    b -=5;

    printf("b -=5;b = %d\n",b);

    // *= Operator

    b *=4;

    printf("b *=4;b = %d\n",b);

    // /= Operator

    b /=2;

    printf("b /=2;b = %d\n",b);

}

Output:

b = 10

b += 10;b = 20

b -=5;b = 15

b *=4;b = 60

b /=2;b = 30

5.  Increment or Decrement Operators

Increment or Decrement operator are also called unary increment or unary decrement.

Increment Unary Operator 

variable++ 
++variable 
Is Equivalent i=i+1

Increment Unary Operator Types

·                    Post Increment i++
·                    Pre Increment ++i

Decrement Unary Operator 

variable--;
--variable;
Is Equivalent i=i-1

Decrement Unary Operator Types

·               Post Decrement i--
·               Pre Decrement --i

Unary Operators Explanation

·                ++i : it increase the l and then uses its value as the value of the                     expression;
·               i++ : uses l as the value of the expression and then increase the l;
·                 --i : it decrease the l and then uses its value as the value of the expression;
·                 i-- : uses l as the value of the expression and then decrease the l.

Example Program

#include<stdio.h>
#include<conio.h>
int main()
{
      int a;
 
      /* Increment Operators */
      a = 5;
      printf("Post Increment = %d\n",a++);
       a = 5;
      printf("Pre Increment = %d\n",++a);
    /* Decrement Operators */
      a = 5;
      printf("Post Decrement = %d\n",a--);
      a = 5;
      printf("Pre Decrement = %d\n",--a);
     // Wait For Output Screen
     getch();
     //Main Function return Statement
     return 0;
}

Sample Output:

Post Increment = 5

Pre Increment = 6

Post Decrement = 5

Pre Decrement = 4

6.  Conditional or Ternary operator

  Check the condition if it is true than it returns first variables value otherwise it return second variable value. Sometimes it replaces if..else statement

Syntax

Condition? Expression1: Expression2

Example

(a>50) ? b : c
It is also called ternary operators.
Example Program 
#include <stdio.h>
#include<conio.h>
void main() {
    int a = 10;
    int b = 15;
    int c
    c = a <= b  ? a : b;
    printf("C Is %d", c);
    getch();
}
Output:
C Is 10

7.  Bitwise Operators

Following are the bitwise operators.

Bit Operators

Ø  & Bitwise AND
Ø  | Bitwise inclusive-OR
Ø  ^ Bitwise OR
Ø  ~ Ones complement
Ø  << Left shift
Ø  >> Right shift

8.  Special Operators

Ø  The Comma Operator
Ø  Type cast Operator
Ø  Reference operator or Address Operater ("&")
Ø  Dereference operator ("*") or Pointer Operater
Ø  Double Pointer operator ("**")
Ø  sizeof operator


Example For Comma Operator

int a,b;

Type cast Operator

·            Converts to the specified type.

Example

float s= 12.5;      
  int a;
a = (int) s;
now variable a’s value is 12.

Reference operator or Address Operater ("&")

The reference operator noted by ampersand ("&"), is also a unary operator in c languages that uses for assign address of the variables. It returns the pointer address of the variable. This is called "referencing" operater. 
Syntax
data_type x;
data_type *pt;
pt = &x

Dereference operator ("*") or Pointer Operater

The dereference operator or indirection operator, represent by asterisk ("*"). It is also called unary operator in c languages that uses for pointer variables. 
data_type *pt;

Double Pointer operator ("**")

Double pointer points to another pointer variable address.
data_type **pt;

sizeof operator

 It gives the size of a variable.
sizeof (data_type)
Example 
sizeof(int)
output
2
    1. There are six relational operators. They are,
  1.  3. Logical Operators
  2. Logical And (&&) Operator 
    1. Syntax
    2. Logical And Operator Example
    3. Logical And Operator Example 
  3. Logical OR (||) Operator
    1. Logical OR  Operator Definition
    2. Syntax
    3.  Example
    4. Logical OR  Operator Example 
  4. Logical NOT (!) Operator 
    1. Syntax
    2. Example
    3.  Program
    4. #include<stdio.h>
    5. #include<conio.h>
    6. int main()
    7. {
    8.     int number;
    9.      printf("Please enter an integer value:");
    10.     scanf("%d", &number);
    11.      if(!(number>40)){
    12.         printf("no is greater than 40 - TRUE");
    13.     }else{
    14.         printf("no is greater than 40 - FALSE");
    15.     }
    16.       // Wait For Output Screen
    17.       getch();
    18.      return 0;
    19. }
    20. Output
    21. Please enter an integer value: 36
    22. no is greater than 40 - FALSE
    23.  4.  Assignment Operators
    24. Syntax
    25. There are five assignment operators. They are,
    26. Program
    27. #include <stdio.h>
    28. #include<conio.h>
    29. void main(){
    30.     int a = 10; 
    31.     // = Operator
    32.     int b = a; 
    33.     printf("b = %d\n",b);
    34.     // += Operator
    35.     b += 10;
    36.     printf("b += 10;b = %d\n",b);
    37.     // -= Operator
    38.     b -=5;
    39.     printf("b -=5;b = %d\n",b);
    40.     // *= Operator
    41.     b *=4;
    42.     printf("b *=4;b = %d\n",b);
    43.     // /= Operator
    44.     b /=2;
    45.     printf("b /=2;b = %d\n",b);
    46. }
    47. Output:
    48. b = 10
    49. b += 10;b = 20
    50. b -=5;b = 15
    51. b *=4;b = 60
    52. b /=2;b = 30
  5. 5.  Increment or Decrement Operators
    1. Increment Unary Operator 
    2. Increment Unary Operator Types
    3. Decrement Unary Operator 
    4. Decrement Unary Operator Types
  6. Unary Operators Explanation
  7. Example Program
    1. Sample Output:
    2. Post Increment = 5
    3. Pre Increment = 6
    4. Post Decrement = 5
    5. Pre Decrement = 4
  8. 6.  Conditional or Ternary operator
    1. Syntax
    2. Example
    3. 7.  Bitwise Operators
    4. Bit Operators
  9. 8.  Special Operators
    1. Example For Comma Operator
  10. Type cast Operator
    1. Example
  11. Reference operator or Address Operater ("&")
  12. Dereference operator ("*") or Pointer Operater
  13. Double Pointer operator ("**")
  14. sizeof operator