Operators in C programming language [ Part-1 ]

An operator in C programming language is simply a symbol that is used to perform different types of  operations. There are following types of operators to perform different types of operations in C language.
  • Arithmetic Operators
  • Relational Operators
  • Logical Operators
  • Bitwise Operators
  • Assignment Operators
  • Misc Operators

Arithmetic Operators : The following table shows all the arithmetic operators supported by the C language. Assume variable A holds 10 and variable B holds 20, then :

Operators Description Examples
+ Adds two operands A + B = 30
- Subtracts second operand from the first A - B = -10
* Multiplies both operands A * B = 200
/ Divides numerator by de-numerator B/A = 2
% Modulus Operator and remainder of after an integer division B % A = 0
++ Increment operator increases the integer value by one A++ = 11
-- Decrement operator decrease the integer value by one A-- = 9

Example :
#include<stdio.h>
int main()
{
      int a=10, b=5;
      printf("Sum = %d\n",a+b);
      printf("Subtract = %d\n",a-b);
      printf("Multiplication = %d\n",a*b);
      printf("Division = %d\n",a/b);
      return 0;
}

Output :
Sum = 15
Subtract = 5
Multiplication = 50
Division = 2

Relational Operators : The following table shows all the relational operators supported by C. Assume variable A holds 10 and variable B holds 20, then :

Operators Description Examples
== Checks if the values of two operands are equal or not. If yes, then the condition becomes true. (A == B) is not true.
!= Checks if the values of two operands are equal or not. If the values are not equal, then the condition becomes true. (A != B) is true.
> Checks if the value of left operand is greater than the value of right operand. If yes, then the condition becomes true. (A > B) is not true.
< Checks if the value of left operand is less than the value of right operand. If yes, then the condition becomes true. (A < B) is true
>= Checks if the value of left operand is greater than or equal to the value of right operand. If yes, then the condition becomes true. (A >= B) is not true
<= Checks if the value of left operand is less than or equal to the value of right operand. If yes, then the condition becomes true. (A <= B) is true

Example :
#include <stdio.h>
int main()
{
      int a=10, b=5;
      if(a==b)
      {
            printf("%d is equal to %d\n",a,b);
      }
      else
      {
            printf("%d is not equal to %d\n",a,b);
      }
      if(a>b)
      {
            printf("%d is greater than %d\n",a,b);
       }
      else
      {
            printf("%d is not greater than %d\n",a,b);
      }
      if(a<b)
      {
            printf("%d is less than %d\n",a,b);
      }
      else
      {
            printf("%d is not less than %d\n",a,b);
      }
}

Output :
10 is not equal to 5
10 is greater than 5
10 is not less than 5

Logical Operators : Following table shows all the logical operators supported by C language. Assume variable A holds 1 and variable B holds 0, then :

Operators Description Examples
&& Called Logical AND operator. If both the operands are non-zero, then the condition becomes true. (A && B) is false.
|| Called Logical OR Operator. If any of the two operands is non-zero, then the condition becomes true. (A || B) is true.
! Called Logical NOT Operator. It is used to reverse the logical state of its operand. If a condition is true, then Logical NOT operator will make it false. !(A && B) is true.

Example :
#include <stdio.h>
int main()
{
      int a=5;
      int b=20;
      if( a && b )
      {
            printf("The condition is true");
      }
      else
      {
            printf("The condition is false");
      }
}

Output :
The condition is true

Click Here for Part-2

0 Comments