Logical Operators


C has the following three logical operators.

&& meaning logical AND
|| meaning logical OR
! meaning logical NOT

Logical Operators && and || Used when we want to test and decide on multiple conditions. An example is:

a == 10 && b == 10

An expression of this kind, a combination of two or more relational expressions is called a logical expression or a compound relational expression. Like the simple relational expressions, a logical expression also yields a value of one or zero. The logical expression given above is true only if a == 10 is true and b == 10 is also true. If either of them is false, the expression is false.

Example of && operators

Program
#include <stdio.h>

int main() 
{
  int a=2,b=3,c=1;
  if(a>b && a>c)
   printf("a is big");
  else
   if(b>c)
   printf("b is big");
   else
   printf("c is big");
  return 0;
}

The example above shows which of the three numbers is larger with the && logical operator. When the && operator is inserted between the two expressions, the second expression will not be checked if the first expression is false. For this, the operation will be proved to be true only if two expressions are true, otherwise, if anyone is false, it will be proved to be false. Take a look at the && gate table below:

Expression Expression Result
0(false) 0(false) 0(false)
0(false) 1(true) 0(false)
1(true) 0(false) 0(false)
1(true) 1(true) 1(true)

Example of || operator

Program
#include <stdio.h>

int main() 
{
  int a=2,b=3,c=1;
  if(a>b || a>c)
   printf("a is big");
  else
   if(b>c)
   printf("b is big");
   else
   printf("c is big");
  return 0;
}

The example above shows which of the three numbers is larger with the OR logical operator. When between two expressions. If the first expression is true then the second expression will not be checked. For this, the operation will be proved to be true only if two expressions are true, otherwise it will be proved to be true if either one is true. Below is the OR gate table:

Expression Expression Result
0(false) 0(false) 0(false)
0(false) 1(true) 1(true)
1(true) 0(false) 1(true)
1(true) 1(true) 1(true)