The Wayback Machine - https://web.archive.org/web/20230117132115/https://www.geeksforgeeks.org/logical-operators-in-c/
Skip to content
Related Articles

Related Articles

Logical Operators in C

Improve Article
Save Article
  • Last Updated : 14 Oct, 2022
Improve Article
Save Article

Logical operators in C are used to combine multiple conditions/constraints. Logical Operators returns either 0 or 1, it depends on the expression result true or false. In C programming for decision-making, we use logical operators. We have 3 major logical operators in the C language:

  • Logical AND (&&)
  • Logical OR (||)
  • Logical NOT (!) 
  • Logical XOR(^)

Types of Logical Operators

1. Logical AND Operator 

If both operands are non zero then the condition becomes true. Otherwise, the result has a value of 0. The return type of the result is int. Below is the truth table for the logical AND operator.

XYX&&Y;
111
100
010
000

Syntax:

(condition_1 && condition_2)

Example:

C




// C program for Logical
// AND Operator
#include <stdio.h>
 
// Driver code
int main()
{
  int a = 10, b = 20;
   
  if (a > 0 && b > 0)
  {
    printf("Both values are greater than 0\n");
  }
  else
  {
    printf("Both values are less than 0\n");
  }
  return 0;
}

Output

Both values are greater than 0

2. Logical OR Operator 

The condition becomes true if any one of them is non-zero. Otherwise, it returns false i.e, 0 as the value. Below is the truth table for the logical OR operator.

XYX||Y
111
101
011
000

Syntax:

(condition_1 || condition_2)

Example:

C




// C program for Logical
// OR Operator
#include <stdio.h>
 
// Driver code
int main()
{
  int a = -1, b = 20;
   
  if (a > 0 || b > 0)
  {
    printf("Any one of the given value is "
           "greater than 0\n");
  }
  else
  {
    printf("Both values are less than 0\n");
  }
  return 0;
}

Output

Any one of the given value is greater than 0

3. Logical NOT Operator

If the condition is true then the logical NOT operator will make it false and vice-versa. Below is the truth table for the logical NOT operator.

X!X
01
10

Syntax:

!(condition_1 && condition_2)

Example:

C




// C program for Logical
// NOT Operator
#include <stdio.h>
 
// Driver code
int main()
{
  int a = 10, b = 20;
   
  if (!(a > 0 and b > 0))
  {
    // condition returned true but
    // logical NOT operator changed
    // it to false
    printf("Both values are greater than 0\n");
  }
  else
  {
    printf("Both values are less than 0\n");
  }
  return 0;
}

Output

Both values are less than 0

4. XOR (^) Logical Operator:

If both bits are the same then it will return false otherwise true. Below is the truth table for the logical XOR operator.

XYX^Y
000
011
101
110

Syntax:-

((condition1) ^ (condition2))

Example:

C++




// C program for Logical
// XOR Operator
#include <stdio.h>
 
// Driver code
int main()
{
  int a = 11, b = 11;
  printf("%d\n",a^b);
  return 0;
}

Output

0

Below is an example of the logical operator to understand more about all logical operators in C.

C




// C program to demonstrate
// Logical Operators
#include <stdio.h>
 
// Driver code
int main()
{
  int x = 7, y = 3;
   
  // Logical operators
  printf("Following are the logical operators in C \n");
  printf("The value of this logical and operator ((x==y) "
         "&& (x<y)) is: %d \n",
         ((x == y) && (x < y)));
   
  printf("The value of this logical or operator ((x==y) "
         "|| (x<y)) is: %d \n",
         ((x == y) || (x < y)));
   
  printf("The value of this logical not operator "
         "(!(x==y)) is: %d ",
         (!(x == y)));
  return 0;
}

Output

Following are the logical operators in C 
The value of this logical and operator ((x==y) && (x<y)) is: 0 
The value of this logical or operator ((x==y) || (x<y)) is: 0 
The value of this logical not operator (!(x==y)) is: 1 

My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!