Logical Operators in C
In the previous tutorial, we learned about relational operators, which allow us to compare values. Now, in this lesson, we will explore logical operators. These operators help us handle more complex conditions by combining multiple relational expressions together.
What are Logical Operators?
A logical operator is a special symbol that allows us to combine multiple conditions or expressions together to create more complex conditions. These operators help us make decisions based on multiple conditions at once.
Let’s consider the following statement as an example:
“Ram’s current salary is 10000 and he has completed more than 10 years of his service.”
We can represent this statement using variables. Let’s assume Ram’s salary is stored in the variable “salary” and the number of years of his service is stored in the variable “years.”
The representation would be: salary == 10000 && years > 10
In this representation, the logical AND operator (&&) is used. It checks if both conditions on either side of it are true. If Ram’s salary is indeed equal to 10000 AND his years of service are greater than 10, the overall expression will evaluate to true. Otherwise, it will evaluate to false.
In the C programming language, we have three logical operators:
- Logical AND (&&)
- Logical OR (||)
- Logical NOT (!)
Now, let’s understand these operators in detail.
Logical Operators in Details
- Logical AND (&&) operator combines two relational expressions and returns true only when both the expressions evaluate to true; otherwise, it returns false. For example, the logical expression salary == 10000 && years > 10 returns true only when the salary is equal to 10000 and years of service is greater than 10.
- Logical OR (||) operator returns true when at least one condition evaluates to true, otherwise it returns false. For example, the logical expression salary == 10000 && years > 10 returns true when either the salary is equal to 10000 or the years of service is greater than 10 or both are true.
- Logical NOT (!) operator is a unary operator which means it can operate on a single operand. It returns true when the operand evaluates to false. For example, !age > 25 returns true when the age is either less than or equal to 25.
Example Program
The following program demonstrates the use of logical operators:
#include <stdio.h>
int main()
{
int salary = 10000;
int years = 12;
printf("%d\n", (salary == 10000) && (years > 10));
printf("%d\n", (salary > 10000) || (years > 10));
printf("%d", !(salary > 10000));
return 0;
}
Output:
1
1
1
Precedence and Associativity of Logical Operators
The following table shows the precedence and associativity of relational operators (in continuation with the arithmetic and relational operators):
Precedence | Operators | Associativity |
---|
1st | Unary + and -, Logical negation (!) | Right to left |
2nd | *, /, % | Left to right |
3rd | +, – | Left to right |
4th | <, <=, >, >= | Left to right |
5th | ==, != | Left to right |
6th | && | Left to right |
7th | || | Left to right |
8th | = | Right to left |
Example: Solve 9 * 7 - 10 > 3 && !0
.
Solution:
Note that in the C programming language, any non-zero value is considered true.
9 * 7 - 10 > 3 && !0
9 * 7 - 10 > 3 && 1 (!0 = 1)
63 - 10 > 3 && 1 (9 * 7 = 63)
53 > 3 && 1 (63 - 10 = 53)
1 && 1 (53 > 3 = 1)
1 (1 && 1 = 1)
Leave a comment