Logical Operators in C
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.
| X | Y | X&&Y; |
|---|---|---|
| 1 | 1 | 1 |
| 1 | 0 | 0 |
| 0 | 1 | 0 |
| 0 | 0 | 0 |
Syntax:
(condition_1 && condition_2)
Example:
C
// C program for Logical// AND Operator#include <stdio.h>// Driver codeint 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;} |
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.
| X | Y | X||Y |
|---|---|---|
| 1 | 1 | 1 |
| 1 | 0 | 1 |
| 0 | 1 | 1 |
| 0 | 0 | 0 |
Syntax:
(condition_1 || condition_2)
Example:
C
// C program for Logical// OR Operator#include <stdio.h>// Driver codeint 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;} |
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 |
|---|---|
| 0 | 1 |
| 1 | 0 |
Syntax:
!(condition_1 && condition_2)
Example:
C
// C program for Logical// NOT Operator#include <stdio.h>// Driver codeint 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;} |
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.
| X | Y | X^Y |
| 0 | 0 | 0 |
| 0 | 1 | 1 |
| 1 | 0 | 1 |
| 1 | 1 | 0 |
Syntax:-
((condition1) ^ (condition2))
Example:
C++
// C program for Logical// XOR Operator#include <stdio.h>// Driver codeint main(){ int a = 11, b = 11; printf("%d\n",a^b); return 0;} |
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 codeint 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;} |
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






Please Login to comment...