What are Logical Operators in C? See Examples

Logical operators in C are used to combine multiple conditions or to test logical relationships between expressions. They are essential when writing decision-making statements such as if, while, and for loops.

C provides three logical operators:

  1. Logical AND ( && )
  2. Logical OR ( || )
  3. Logical NOT ( ! )

Each operator returns either:

  • 1 (true)
  • 0 (false)

These operators work on conditions, not arithmetic values.


1. Logical AND ( && )

The result is true (1) only if both conditions are true.

Condition 1Condition 2Result
TrueTrueTrue
TrueFalseFalse
FalseTrueFalse
FalseFalseFalse

Example

int age = 20;
int hasID = 1;

if (age >= 18 && hasID == 1) {
    printf("Allowed to enter");
}

Explanation

Both conditions must be true, so the person is allowed.


2. Logical OR ( || )

The result is true if at least one condition is true.

Condition 1Condition 2Result
TrueTrueTrue
TrueFalseTrue
FalseTrueTrue
FalseFalseFalse

Example

int marks = 90;
if (marks >= 90 || marks == 100) {
    printf("Excellent performance");
}

Even if only the first condition is true, OR returns true.


3. Logical NOT ( ! )

Logical NOT reverses the result of a condition.

  • If condition is true, ! makes it false
  • If condition is false, ! makes it true

Example

int isOnline = 0;
if (!isOnline) {
    printf("User is offline");
}

Since isOnline is 0 (false), !isOnline becomes 1 (true).


Combined Example Using All Logical Operators

#include <stdio.h>

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

    printf("a == c && b > a : %d\n", (a == c && b > a));   // 1
    printf("a == b || b > c : %d\n", (a == b || b > c));   // 1
    printf("!(a == c)       : %d\n", !(a == c));           // 0

    return 0;
}

Using Logical Operators in Real Programs

Example 1: Validating Login

int correctUser = 1;
int correctPassword = 1;

if (correctUser && correctPassword) {
    printf("Login successful");
}

Example 2: Check If Number Is Between a Range

int num = 45;

if (num >= 1 && num <= 100) {
    printf("Number is within range");
}

Example 3: At Least One Condition Must Be True

int isSunday = 0;
int isHoliday = 1;

if (isSunday || isHoliday) {
    printf("You can relax today!");
}

Short-Circuit Evaluation in C

Logical operators in C use short-circuit evaluation, meaning:

AND (&&)

If first condition is false → second is NOT evaluated.
This improves performance and prevents errors.

OR (||)

If first condition is true → second is NOT evaluated.

Example

int x = 0;
if (x != 0 && (10 / x) > 1) {
    // second condition is never checked
}

Prevents a runtime error (division by zero).


Conclusion

Logical operators are fundamental in controlling program flow. They allow you to combine multiple conditions, evaluate decisions efficiently, and create powerful logical expressions. Understanding AND, OR, and NOT is essential for writing clean and effective C programs.


🔗 View other articles about C Programming:

https://savanka.com/category/learn/c-programming

🔗 External C Documentation:

https://www.w3schools.com/c

Leave a Comment

Comments

No comments yet. Why don’t you start the discussion?

Leave a Reply

Your email address will not be published. Required fields are marked *