Logical operators in C are used to combine multiple conditions/constraints. Logical Operators returns either 0 or 1, it depends on whether the expression result is true or false. In C programming for decision-making, we use logical operators.
We have 3 logical operators in the C language:
- Logical AND ( && )
- Logical OR ( || )
- Logical NOT ( ! )
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
(operand_1 && operand_2)
Example
C
#include <stdio.h>
int 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;
}
|
Output
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
(operand_1 || operand_2)
Example
C
#include <stdio.h>
int 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;
}
|
Output
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.
Syntax
!(operand_1 && operand_2)
Example
C
#include <stdio.h>
int 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;
}
|
Short Circuit Logical Operators
When the result can be determined by evaluating the preceding Logical expression without evaluating the further operands, it is known as short-circuiting.
Short-circuiting can be seen in the equation having more than one Logical operator. They can either AND, OR, or both.
1. Short Circuiting in Logical AND Operator
The logical AND operator returns true if and only if all operands evaluate to true. If the first operand is false, then the further operands will not be evaluated. This is because even if the further operands evaluate to true, the entire condition will still return false.
Example
C++
#include <iostream>
using namespace std;
bool is_positive( int number)
{
if (number > 0)
return true ;
else
return false ;
}
bool is_even( int number)
{
if (number % 2 == 0)
return true ;
else
return false ;
}
int main()
{
int x = 10;
if (is_positive(x) && is_even(x)) {
cout << "Both conditions are satisfied." << endl;
}
else {
cout << "Conditions not satisfied." << endl;
}
int y = -5;
if (is_positive(y) && is_even(y)) {
cout << "Both conditions are satisfied." << endl;
}
else {
cout << "Conditions not satisfied." << endl;
}
return 0;
}
|
Output
Both conditions are satisfied.
Conditions not satisfied.
2. Short Circuiting in Logical OR Operator
OR operator returns true if at least one operand evaluates to true. If the first operand is true, then the further operands will not be evaluated. This is because even if the further operands evaluate to false, the entire condition will still return true.
Example
C++
#include <iostream>
using namespace std;
bool is_positive( int number)
{
if (number > 0)
return true ;
else
return false ;
}
bool is_even( int number)
{
if (number % 2 == 0)
return true ;
else
return false ;
}
int main()
{
int x = 8;
if (is_positive(x) || is_even(x)) {
cout << "At least one condition is satisfied."
<< endl;
}
else {
cout << "Conditions not satisfied." << endl;
}
int y = -5;
if (is_positive(y) || is_even(y)) {
cout << "At least one condition is satisfied."
<< endl;
}
else {
cout << "Conditions not satisfied." << endl;
}
return 0;
}
|
Output
At least one condition is satisfied.
Conditions not satisfied.
FAQs on Logical operators
Q1. What is the precedence of logical operators in programming?
Answer:
The precedence of logical operators is: NOT, AND, OR. However, it is always recommended to use parentheses to make the order of evaluation explicit and avoid confusion.
Q2. Can logical operators be chained together?
Answer:
Yes, logical operators can be chained together to create complex conditions. For example, we can combine multiple logical AND (&&) or logical OR (||) operators in a single expression to evaluate multiple conditions simultaneously.
Q3. What will be the output of the following code?
C
#include <stdio.h>
void main()
{
int a = 1, b = 0, c = 5;
int d = a && b || c++;
printf ( "%d" , c);
}
|
Answer:
6
Q4. What will be the output of the following code?
C
#include <stdio.h>
int main()
{
int i = 1;
if (i++ && (i == 1))
printf ( "GeeksforGeeks\n" );
else
printf ( "Coding\n" );
}
|
Answer:
Coding
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!
Last Updated :
07 Aug, 2023
Like Article
Save Article