The Logical operators in C are used to combine two or more conditions. And perform the logical operations using && (AND), || (OR) and ! (NOT)
The Relational operators are used to compare two variables; what if we want to compare more than one condition? Very simple, logical operators will do the trick for you.
Types of Logical Operators in C
The table below shows the list of Logical Operators in this programming language with examples.
| OPERATORS | NAME | DESCRIPTION | EXAMPLE |
|---|---|---|---|
| && | AND | It returns true when both conditions are true | If (age > 18 && age <=35) |
| || | OR | It returns true when at least one of the conditions is true | If (age > 35 || age < 60) |
| ! | NOT | If the condition is true, the logical NOT operator makes it false | If age = 18 then !( age = 18) returns false. |
Let us see the truth tables behind the logical operators in C language to understand Operators better. For more, please refer to the All Operators and Relational operators articles in C Programming.
Logical AND Operator (&&)
The logical AND operator returns true when both operands or conditions are true. If any one of the conditions fails, it returns False. When we want to check multiple conditions and require both conditions to be met, we can use the logical AND operator. For example, the password should not be empty, and it should contain special characters.
The truth table behind the Logical AND operators in C Programming is as shown below.
| Condition 1 | Condition 2 | Condition 1 && Condition 2 |
|---|---|---|
| True | True | True |
| True | False | False |
| False | True | False |
| False | False | False |
Logical AND Syntax
Condition1 && condition2
C Logical AND Operators Example
In the following example, we declared two variables and assigned 10 and 20 to them. The if statement checks whether a is greater than 0 and b is greater than 15. As both conditions are true, the logical AND operator returns true and prints the statement inside the if condition.
If we change the condition to if (a > 10 && b > 15), meaning the first condition is false. Therefore, the result is false, and the message from the else block is displayed.
#include <stdio.h>
int main()
{
int a = 10, b = 20;
if (a > 0 && b > 15)
{
printf("Both conditions are True");
}
else
{
printf("One of the two conditions Fail");
}
}
Both conditions are True
Logical OR Operator (||)
The logical OR operator returns true if any one of the conditions is evaluated to true. If both conditions fail, it returns False. We can use the logical OR operator when evaluating a series of expressions, and any one of them should be true. For example, the incoming file may be text, PDF, or CSV.
The truth table behind the Logical OR operators in C programming is as shown below.
| Condition 1 | Condition 2 | Condition 1 || Condition 2 |
|---|---|---|
| True | True | True |
| True | False | True |
| False | True | True |
| False | False | False |
Logical OR Syntax
Condition1 || condition2
C Logical OR Operators (||) Example
In the following example, we use the logical OR operator (||) to evaluate two expressions inside the if-else block. Here, the first condition (a <0) is false, and the second expression (b > 10) is true. As one of the conditions is true, the logical operator returns True and prints the message inside the block. It is helpful in else if ladder or while loop.
#include <stdio.h>
int main()
{
int a = 10, b = 20;
if (a < 0 || b > 10)
{
printf("Either one of the conditions is true");
}
else
{
printf("Both Conditions Fail");
}
}
Either one of the conditions is true
Logical NOT Operator (!)
The logical NOT operator (!) returns the opposite result. If the condition is true, it returns false, and if the expression is false, it returns true.
The truth table behind the Logical NOT operators in C Programming is
| x | !x |
|---|---|
| 0 | 1 |
| 1 | 0 |
Logical NOT syntax
!expression !(condition1 && condition2)
Logical NOT Example
In the following example, the if condition (a <0) checks whether a is less than 0. Next, the logical NOT operator returns the opposite result. Here, a < 0 condition fails, but the logical NOT operator converts false to true and prints the message inside the if block.
#include <stdio.h>
int main()
{
int a = 10;
if (!(a < 0))
{
printf("Positive Number");
}
else
{
printf("Negative Number");
}
}
Positive Number
Examples of Logical Operators in C Programming
The following are some practical examples of the logical operators.
Voting
In the following example, the logical AND operator checks whether the person’s age is greater than or equal to 18 and has a voter ID (provided by the Election Commission). If both are true, then they can vote.
#include <stdio.h>
int main() {
int age = 25;
int voterIDCard = 1;
if (age >= 18 && voterIDCard)
printf("Eligible to Vote in Elections");
else
printf("Not Eligible for Voting");
return 0;
}
Eligible to Vote in Elections
Interview Criteria
Imagine a company is hiring a BI professional, and the candidate should have at least 3 years of Tableau experience or at least 1 year of Power BI experience. If the candidate has experience with either of the tools, they can apply for the job.
#include <stdio.h>
int main() {
int TableauExperience = 3;
int PowerBIExperience = 2;
if (TableauExperience >= 5 || PowerBIExperience >= 1)
printf("Eligible for Interview");
else
printf("Not Eligible");
return 0;
}
Eligible for Interview
NOT example
In the following example checks the status of the card. If the card is blocked, it informs the user otherwise; it allows the user to enter the PIN.
#include <stdio.h>
int main()
{
int cardBlocked = 0;
if (!cardBlocked)
printf("Enter Pin");
else
printf("Sorry! Card Blocked");
}
Enter Pin