Home » C Logical Operators – Complete Guide with Examples for Beginners

C Logical Operators – Master Conditional Logic in C

Logical operators in C are used to combine multiple conditions and make decisions in programs. They are essential for writing if statements, loops, and complex conditions.

If you want to master decision-making in programming, understanding logical operators is a must.

What are Logical Operators in C?

Logical operators are used to perform operations on conditions (true/false values).

They return:

  • 1 (true)
  • 0 (false)

Why Use Logical Operators?

When you want to check more than one condition at a time, logical operators help you build compound expressions:

  • Is the user logged in AND has permission?

  • Is the value greater than 10 OR less than 5?

  • Is the user NOT an admin?

List of Logical Operators in C

OperatorNameDescriptionExample
&&Logical ANDTrue if both conditions are true(a > 0 && b > 0)
` `Logical OR
!Logical NOTReverses the result (true ↔ false)!(a > 0)

Example Program: Logical Operators in Action

#include <stdio.h>

int main() {
    int a = 5, b = -3;

    // Logical AND
    if (a > 0 && b > 0) {
        printf("Both a and b are positive.\n");
    } else {
        printf("At least one is not positive.\n");
    }

    // Logical OR
    if (a > 0 || b > 0) {
        printf("At least one is positive.\n");
    }

    // Logical NOT
    if (!(b > 0)) {
        printf("b is not positive.\n");
    }

    return 0;
}

Output:

At least one is not positive.
At least one is positive.
b is not positive.

Logical AND Operator (&&)

The AND operator returns true only if both conditions are true.

Example:

#include <stdio.h>

int main() {
    int age = 20;

    if (age > 18 && age < 30) {
        printf("Eligible");
    }

    return 0;
}

Output: Eligible

Truth Table for AND

Condition 1Condition 2Result
111
100
010
000

Logical OR Operator (||)

The OR operator returns true if at least one condition is true.

 Example:

#include <stdio.h>

int main() {
    int marks = 40;

    if (marks >= 33 || marks == 0) {
        printf("Pass or Absent");
    }

    return 0;
}

Output: Pass or Absent

Truth Table for OR

Condition 1Condition 2Result
111
101
011
000

 

Logical NOT Operator (!)

The NOT operator reverses the result.

Example:

#include <stdio.h>

int main() {
    int isLoggedIn = 0;

    if (!isLoggedIn) {
        printf("Please login");
    }

    return 0;
}

Output: Please login

Truth Table for NOT

ConditionResult
10
01

Step-by-Step Guide to Use Logical Operators

Step 1: Understand conditions

Use comparison operators (>, <, ==)

Step 2: Combine conditions

Use && or ||

Step 3: Use in if statements

if (age > 18 && salary > 20000) {
    printf("Eligible");
}

Step 4: Test different inputs

Best Practices for C Logical Operators

PracticeWhy?
Use parentheses to group conditionsImproves readability and correctness
Don’t rely on non-zero being true blindlyBe explicit with boolean logic
Understand short-circuitingPrevents unnecessary condition checks

What is Short-Circuit Evaluation?

Logical operators use short-circuiting:

  • For &&: if the first condition is false, the second is not checked.

  • For ||: if the first condition is true, the second is not checked.

if (a != 0 && (10 / a) > 2) { ... } // Safe: won't divide by 0

Comparison Table (Logical Operators)

OperatorSymbolUse CaseResult Condition
AND&&Multiple conditionsBoth must be true
OR   
NOT!Reverse conditionOpposite result

Common Mistakes to Avoid

MistakeProblem
Using & instead of &&Bitwise AND, not logical AND
Confusing !a with a == 0They’re not always the same
Forgetting parenthesesMay lead to incorrect logic order

FAQs (Frequently Asked Questions)

What are logical operators in C?

They are used to combine conditions and return true or false.

2. What is && in C?

Logical AND operator.

3. What is || in C?

Logical OR operator.

4. What does ! mean in C?

Logical NOT operator.

5. Where are logical operators used?

In if statements, loops, and decision-making logic.

Scroll to Top