Logical Operators in C: A Comprehensive Guide
Understand the logical operators in C programming that evaluate expressions as True or False. C treats "0" as False and any non-zero value as True, making logical operators essential for decision-making in programs. Learn how to use logical operators efficiently to control the flow of your code.
Logical Operators in C
Logical operators in C evaluate to either True or False and are typically used with Boolean operands. Since C treats "0" as False and any non-zero number as True, any operand to a logical operator is converted to a Boolean value.
Logical Operators
The following table shows the logical operators in C:
Operator | Description | Example |
---|---|---|
&& | Logical AND operator. If both operands are non-zero, the result is True. | (A && B) |
|| | Logical OR operator. If any operand is non-zero, the result is True. | (A || B) |
! | Logical NOT operator. It negates the logical state of its operand. | !(A) |
Truth Tables
Logical AND (&&) Operator
The &&
operator has the following truth table:
a | b | a && b |
---|---|---|
True | True | True |
True | False | False |
False | True | False |
False | False | False |
Logical OR (||) Operator
The ||
operator has the following truth table:
a | b | a || b |
---|---|---|
True | True | True |
True | False | True |
False | True | True |
False | False | False |
Logical NOT (!) Operator
The !
operator has the following truth table:
A | !A |
---|---|
True | False |
False | True |
Examples
Example 1
The following example demonstrates the usage of logical operators in C:
Code
#include <stdio.h>
int main() {
int a = 5;
int b = 20;
if (a && b) {
printf("Line 1 - Condition is true\n");
}
if (a || b) {
printf("Line 2 - Condition is true\n");
}
a = 0;
b = 10;
if (a && b) {
printf("Line 3 - Condition is true\n");
} else {
printf("Line 3 - Condition is not true\n");
}
if (!(a && b)) {
printf("Line 4 - Condition is true\n");
}
return 0;
}
Output
Line 1 - Condition is true
Line 2 - Condition is true
Line 3 - Condition is not true
Line 4 - Condition is true
Example 2
Logical operators can also work with char
types as they are a subset of int
:
Code
#include <stdio.h>
int main() {
char a = 'a';
char b = '\0'; // Null character
if (a && b) {
printf("Line 1 - Condition is true\n");
}
if (a || b) {
printf("Line 2 - Condition is true\n");
}
return 0;
}
Output
Line 2 - Condition is true
Example 3
The following example shows a compound Boolean expression in a C program:
Code
#include <stdio.h>
int main() {
int phy = 50;
int maths = 60;
if (phy < 50 || maths < 50) {
printf("Result: Fail");
} else {
printf("Result: Pass");
}
return 0;
}
Output
Result: Pass
Example 4
The same logic can be expressed using the &&
operator as follows:
Code
#include <stdio.h>
int main() {
int phy = 50;
int maths = 60;
if (phy >= 50 && maths >= 50) {
printf("Result: Pass");
} else {
printf("Result: Fail");
}
return 0;
}
Output
Result: Pass
Example 5
The following code uses the !
operator in a while loop:
Code
#include <stdio.h>
int main() {
int i = 0;
while (!(i > 5)) {
printf("i = %d\n", i);
i++;
}
return 0;
}
Output
i = 0
i = 1
i = 2
i = 3
i = 4
i = 5
Logical operators are generally used to build compound Boolean expressions and are often used in decision-control and looping statements in C. C also has bitwise counterparts of the logical operators such as bitwise AND (&
), bitwise OR (|
), and bitwise NOT (~
).