C Logical Operators

Last Updated : 2 Sep, 2026

Logical operators in C are used to combine or reverse conditions. They help a program make decisions based on whether conditions are true or false.

  • Logical operators return 1 when the result is true and 0 when the result is false.
  • C provides three logical operators: AND (&&), OR (||), and NOT (!).

Types of Logical Operators

C provides three logical operators:

1. Logical AND Operator ( && )

The logical AND operator (&&) returns 1 when both conditions are true. If either condition is false, it returns 0.

     X     

Y      X && Y     

1

1

1

1

0

0

0

1

0

0

0

0

Syntax

condition1 && condition2

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("One or both values are less than or equal to 0\n");
    }
    return 0;
}

Output
Both values are greater than 0

Here, both a > 0 and b > 0 are true, so the AND condition is true.

2. Logical OR Operator ( || )

The logical OR operator (||) returns 1 when at least one condition is true. It returns 0 only when both conditions are false.

      X          Y          X || Y     

1

1

1

1

0

1

0

1

1

0

0

0

Syntax

condition1 || condition2

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 or equal to 0\n");
    }
    return 0;
}

Output
Any one of the given value is greater than 0

Here, a > 0 is false, but b > 0 is true. Therefore, the OR condition is true.

3. Logical NOT Operator ( ! )

The logical NOT operator (!) reverses the result of a condition. It changes true to false and false to true.

      X          !X     

0

1

1

0

Syntax

!(condition)

C
#include <stdio.h>

int main() {
    int a = 10, b = 20;

    if (!(a > 0 && b > 0)) {
        printf("Both values are not greater than 0\n");
    }
    else {
        printf("Both values are greater than 0\n");
    }

    return 0;
}

Output
Both values are greater than 0

Here, a > 0 && b > 0 is true. The ! operator reverses it to false, so the else block is executed.

Short-Circuit Evaluation

C sometimes stops evaluating a logical expression as soon as its result is known. This is called short-circuit evaluation.

  • With &&, if the first condition is false, the remaining conditions are not evaluated.
  • With ||, if the first condition is true, the remaining conditions are not evaluated.

1. Short-Circuiting with Logical AND (&&)

The logical AND operator returns true only if both operands evaluate to true. If the first operand is false, the second operand is not evaluated because the overall result will always be false. This is because even if the further operands evaluate to true, the entire condition will still return false.

C++
#include <stdio.h>

int main()
{
    int a = 10, b = 5, c = 0;

    // c is 0(false), so (a / b) is not evaluated and division will not be done
    if (c && (a = a / b))
        printf("This won't be printed\n");
    printf("%d", a);
    return 0;
}

Output
10

Here, c is 0, so the first condition is false. Therefore, (a = a / b) is not evaluated, and a remains 10.

2. Short-Circuiting with Logical OR (||)

If the first condition in an OR expression is true, the second condition is not evaluated because the complete expression will already be true.

C++
#include <stdio.h>

int main() {
    int a = 10, b = 5, c = 1;

    if (c || (a = a / b)) {
        printf("Condition is true\n");
    }

    printf("%d", a);

    return 0;
} 

Output
Condition is true
10

Here, c is 1, so the first condition is true. Therefore, (a = a / b) is not evaluated, and a remains 10.

Comment