The Wayback Machine - https://web.archive.org/web/20240717024759/https://www.geeksforgeeks.org/logical-operators-in-c/
Open In App

C Logical Operators

Last Updated : 07 Aug, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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




// C program for Logical
// AND Operator
#include <stdio.h>
 
// Driver code
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




// C program for Logical
// OR Operator
#include <stdio.h>
 
// Driver code
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.

     X           !X     

0

1

1

0

Syntax

!(operand_1 && operand_2)

Example

C




// C program for Logical
// NOT Operator
#include <stdio.h>
 
// Driver code
int main()
{
    int a = 10, b = 20;
 
    if (!(a > 0 && b > 0)) {
        // condition returned true but
        // logical NOT operator changed
        // it to false
        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++




// C++ Program to illustrate short circuiting in Logical AND
#include <iostream>
using namespace std;
 
// utility function to check positive
bool is_positive(int number)
{
    if (number > 0)
        return true;
    else
        return false;
}
// utility function to check if the number is even
bool is_even(int number)
{
    if (number % 2 == 0)
        return true;
    else
        return false;
}
 
// driver code
int main()
{
    int x = 10;
 
    // Both conditions are evaluated
    if (is_positive(x) && is_even(x)) {
        cout << "Both conditions are satisfied." << endl;
    }
    else {
        cout << "Conditions not satisfied." << endl;
    }
 
    int y = -5;
 
    // The first condition is evaluated and found to be
    // false, so the second condition is not evaluated
    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++




// C++ program to illustrate the short circuiting in Logical
// OR
#include <iostream>
using namespace std;
 
// utility function to check positive number
bool is_positive(int number)
{
    if (number > 0)
        return true;
    else
        return false;
}
 
// utility function to check if the number is even
bool is_even(int number)
{
    if (number % 2 == 0)
        return true;
    else
        return false;
}
 
// driver code
int main()
{
    int x = 8;
 
    // The first condition is evaluated and found to be
    // true, so the second condition is not evaluated
    if (is_positive(x) || is_even(x)) {
        cout << "At least one condition is satisfied."
             << endl;
    }
    else {
        cout << "Conditions not satisfied." << endl;
    }
 
    int y = -5;
 
    // The first condition is evaluated and found to be
    // false, so the second condition is evaluated
    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


Previous Article
Next Article

Similar Reads

Code Optimization Technique (logical AND and logical OR)
Logical AND (&amp;&amp;)While using &amp;&amp; (logical AND), we must put the condition first whose probability of getting false is high so that compiler doesn’t need to check the second condition if the first condition is false. C/C++ Code #include &lt;bits/stdc++.h&gt; using namespace std; // Function to check whether n is odd bool isOdd(int n) {
7 min read
Order of operands for logical operators
The order of operands of logical operators &amp;&amp;, || are important in C/C++. In mathematics, logical AND, OR, etc... operations are commutative. The result will not change even if we swap RHS and LHS of the operator. In C/C++ (may be in other languages as well)  even though these operators are commutative, their order is critical. For example
1 min read
What are the differences between bitwise and logical AND operators in C/C++?
A Bitwise And operator is represented as '&amp;' and a logical operator is represented as '&amp;&amp;'. The following are some basic differences between the two operators. a) The logical and operator '&amp;&amp;' expects its operands to be boolean expressions (either 1 or 0) and returns a boolean value. The bitwise and operator '&amp;' work on Inte
4 min read
Logical Not ! operator in C with Examples
! is a type of Logical Operator and is read as "NOT" or "Logical NOT". This operator is used to perform "logical NOT" operation, i.e. the function similar to Inverter gate in digital electronics. Syntax: !Condition // returns true if the conditions is false // else returns false Below is an example to demonstrate ! operator: Example: // C program t
1 min read
Conversion Operators in C++
In C++, the programmer abstracts real-world objects using classes as concrete types. Sometimes, it is required to convert one concrete type to another concrete type or primitive type implicitly. Conversion operators play an important role in such situations. It is similar to the operator overloading function in class.For example consider the follow
2 min read
Arithmetic Operators in C
Arithmetic Operators are the type of operators in C that are used to perform mathematical operations in a C program. They can be used in programs to define expressions and mathematical formulas. What are C Arithmetic Operators? The C arithmetic operators are the symbols that are used to perform mathematical operations on operands. There are a total
7 min read
C | Operators | Question 2
#include &lt;stdio.h&gt; int main() {     int i = 1, 2, 3;          printf("%d", i);          return 0; } (A) 1 (B) 3 (C) Garbage value (D) Compile time error Answer: (D) Explanation: Comma acts as a separator here. The compiler creates an integer variable and initializes it with 1. The compiler fails to create integer variable 2 because 2 is not a
1 min read
C | Operators | Question 3
#include &lt;stdio.h&gt; int main() {     int i = (1, 2, 3);          printf("%d", i);          return 0; } (A) 1 (B) 3 (C) Garbage value (D) Compile time error Answer: (B) Explanation: The bracket operator has higher precedence than assignment operator. The expression within bracket operator is evaluated from left to right but it is always the res
1 min read
C | Operators | Question 4
#include &lt;stdio.h&gt; int main() {     int i;        i = 1, 2, 3;     printf("%d", i);          return 0; } (A) 1 (B) 3 (C) Garbage value (D) Compile time error Answer: (A) Explanation: Comma acts as an operator. The assignment operator has higher precedence than comma operator. So, the expression is considered as (i = 1), 2, 3 and 1 gets assign
1 min read
C | Operators | Question 5
C/C++ Code #include int main() { int i = 3; printf(\"%d\", (++i)++); return 0; } What is the output of the above program? (A)3 (B)4 (C)5 (D)Compile-time error Answer: (D)Explanation: In C, prefix and postfix operators need l-value to perform operation and return r-value. The expression (++i)++ when executed increments the value of variable i(i is a
1 min read
Article Tags :
three90RightbarBannerImg