C Logical Operators

In C, logical operators are used to perform logical operations on expressions. These operators evaluate Boolean values and are commonly used in decision-making constructs such as if statements and loops.

The three logical operators in C are && (Logical AND), || (Logical OR), and ! (Logical NOT).


Logical Operators in C

The following table gives the operator symbol and description for each logical operator in C.

Operator NameOperator SymbolDescription
Logical AND&&Returns true if both operands are true; otherwise, returns false.
Logical OR||Returns true if at least one operand is true; otherwise, returns false.
Logical NOT!Returns the opposite of the operand’s truth value. Converts true to false and vice versa.

Examples of Logical Operators

1. Using Logical AND (&&)

In this example, we will use the Logical AND operator to check whether two conditions are true.

main.c

</>
Copy
#include <stdio.h>

int main() {
    int age = 25;
    int hasLicense = 1; // 1 means true

    if (age >= 18 && hasLicense) {
        printf("You are eligible to drive.\n");
    } else {
        printf("You are not eligible to drive.\n");
    }

    return 0;
}

Explanation:

  1. We declare an integer age and set it to 25.
  2. We declare hasLicense with a value of 1 (true).
  3. The if condition uses age >= 18 && hasLicense to check if both conditions are true.
  4. Since both conditions are true, “You are eligible to drive.” is printed.

Output:

You are eligible to drive.

2. Using Logical OR (||)

In this example, we will use the Logical OR operator to check if at least one of the conditions is true.

main.c

</>
Copy
#include <stdio.h>

int main() {
    int raining = 1; // 1 means true
    int umbrella = 0; // 0 means false

    if (raining || umbrella) {
        printf("You should stay dry.\n");
    } else {
        printf("You might get wet.\n");
    }

    return 0;
}

Explanation:

  1. We declare two integer variables raining (set to 1) and umbrella (set to 0).
  2. The if condition checks if either raining or umbrella is true.
  3. Since raining is 1 (true), the condition evaluates to true, and “You should stay dry.” is printed.

Output:

You should stay dry.

3. Using Logical NOT (!)

In this example, we will use the Logical NOT operator to reverse the Boolean value of a condition.

main.c

</>
Copy
#include <stdio.h>

int main() {
    int isDaytime = 0; // 0 means false

    if (!isDaytime) {
        printf("It is nighttime.\n");
    } else {
        printf("It is daytime.\n");
    }

    return 0;
}

Explanation:

  1. We declare an integer variable isDaytime and set it to 0 (false).
  2. The if condition uses !isDaytime to negate the value.
  3. Since isDaytime is false, !isDaytime becomes true.
  4. The message “It is nighttime.” is printed.

Output:

It is nighttime.

Conclusion

In this tutorial, we explored the three logical operators in C:

  1. Logical AND (&&): Returns true if both conditions are true.
  2. Logical OR (||): Returns true if at least one condition is true.
  3. Logical NOT (!): Negates the Boolean value of a condition.

Logical operators are essential for controlling flow in C programs, making decisions, and handling multiple conditions efficiently.