In C, logical expressions return 1 for true and 0 for false, which makes them easy to use in conditional statements.
Table of Contents
Types of Logical Operators in C
There are three main logical operators in C:
Logical AND (&&)
This operator returns true only when both conditions are true.- Dual Check: This operator checks whether both conditions are true at the same time before returning a true result.
- Strict Result: This operator returns false if even one of the conditions is false, making it very strict in evaluation.
- Common Usage: This operator is commonly used when multiple conditions must be satisfied together in a program.
// C program to show logical AND operator
#include stdio.h
// Main function
int main()
{
int a = 10, b = 5;
if (a 5 && b 10)
{
printf("Both conditions are true\n");
}
else
{
printf("At least one condition is false\n");
}
return 0;
}
Output:
Both conditions are true
Logical OR (||)
This operator returns true if at least one condition is true.- Flexible Check: This operator checks multiple conditions and returns true if any one of them is true.
- Quick Result: This operator stops evaluating further conditions once it finds a true condition.
- Wide Usage: This operator is widely used when any one of several conditions is acceptable.
// C program to show logical OR operator
#include stdio.h
// Main function
int main()
{
int a = 2, b = 8;
if (a 5 || b 5)
{
printf("At least one condition is true\n");
}
else
{
printf("Both conditions are false\n");
}
return 0;
}
Output:
At least one condition is true
Logical NOT (!)
This operator reverses the result of a condition.- Condition Reverse: This operator converts a true condition into false and a false condition into true.
- Single Operand: This operator works on only one condition at a time, unlike AND and OR.
- Useful Negation: This operator is useful when a program needs to check the opposite of a condition.
// C program to show logical NOT operator
#include stdio.h
// Main function
int main()
{
int a = 5;
if (!(a 10))
{
printf("Condition is false, so NOT makes it true\n");
}
else
{
printf("Condition is true\n");
}
return 0;
}
Output:
Condition is false, so NOT makes it true
Truth Table of Logical Operators
The truth table helps in understanding how logical operators behave with different inputs:| A | B | A && B | A || B | !A |
|---|---|---|---|---|
| 0 | 0 | 0 | 0 | 1 |
| 0 | 1 | 0 | 1 | 1 |
| 1 | 0 | 0 | 1 | 0 |
| 1 | 1 | 1 | 1 | 0 |
Short-Circuit Evaluation
Short-circuit evaluation means that C stops evaluating an expression as soon as the result is determined.- AND Behavior: In the case of &&, if the first condition is false, the second condition is not evaluated because the result will already be false.
- OR Behavior: In the case of ||, if the first condition is true, the second condition is skipped because the result will already be true.
- Efficiency Gain: This behavior improves performance and also helps in avoiding errors such as division by zero.

// C program to show short-circuit
// evaluation
#include stdio.h
// Main function
int main()
{
int a = 0, b = 5;
if (a != 0 && b / a 1)
{
printf("Safe division\n");
}
else
{
printf("Short-circuit prevents error\n");
}
return 0;
}
Output:
Short-circuit prevents error
Operator Precedence in Logical Operators
Logical operators follow a fixed order of evaluation in C.- Highest Priority: The logical NOT (!) operator is evaluated first among logical operators.
- Medium Priority: The logical AND (&&) operator is evaluated after NOT.
- Lowest Priority: The logical OR (||) operator is evaluated at the end.
- Parentheses Usage: Parentheses should be used to make expressions clear and to control evaluation order.
// C program to show operator precedence
#include stdio.h
// Main function
int main()
{
int result = 1 || 0 && 0;
printf("Result = %d\n", result);
return 0;
}
Output:
Result = 1
Common Mistakes to Avoid
Here are some common mistakes while using logical operators:- Wrong Operator: Using the bitwise AND (&) instead of logical AND (&&) can lead to incorrect results in conditions.
- Missing Parentheses: Not using parentheses can make expressions confusing and may produce unintended results.
- Assignment Error: Writing = instead of == inside a condition assigns a value instead of comparing it.
- Ignoring Short-Circuit: Not understanding short-circuit evaluation can sometimes result in runtime errors like division by zero.
Conclusion
Logical operators in C form the backbone of decision-making in programs. They allow you to combine conditions, reverse logic, and control how your program behaves under different situations. Understanding how &&, ||, and ! work, along with their precedence and short-circuit behavior, makes your code more efficient and reliable.Once you get comfortable with these operators, writing complex conditions becomes much simpler and more intuitive.
Frequently Asked Questions
1. What are logical operators in C?2. How many logical operators are there in C?Logical operators are used to combine or modify conditions and return either true (1) or false (0).
3. What is short-circuit evaluation?There are three logical operators in C: &&, ||, and !.
4. What is the difference between && and ||?Short-circuit evaluation means that the second condition is not evaluated if the result is already known from the first condition.
5. Can logical operators be used with integers?The && operator requires both conditions to be true, whereas the || operator requires only one condition to be true.
Yes, in C any non-zero value is treated as true and zero is treated as false.
0 Comments