C Programs Tutorials | IT Developer
IT Developer

C Programming - C Logical Operators



Share with a Friend

C Programming - C Logical Operators

C Logical Operators

Logical operators in C are used to perform logical operations, typically in decision-making and control flow statements. They evaluate expressions and return a boolean result: 1 (true) or 0 (false). Logical operators often work in conjunction with relational operators to build complex conditions.

List of Logical Operators

Operator Description Syntax Example Result

&&

Logical AND

expr1 && expr2

a > 5 && b < 10

True if both are true

`

 

`

Logical OR

`expr1

!

Logical NOT

!expr

!(a > 5)

True if the condition is false

Key Characteristics

  1. Logical AND (&&):
    • Evaluates to true (1) only if both operands are true.

C

int a = 10, b = 20;

if (a > 5 && b < 30) {

    printf("Condition is True\n");

} else {

    printf("Condition is False\n");

}

// Output: Condition is True

  1. Logical OR (||):
    • Evaluates to true (1) if at least one operand is true.

C

int a = 10, b = 20;

if (a > 5 || b > 50) {

    printf("Condition is True\n");

} else {

    printf("Condition is False\n");

}

// Output: Condition is True

  1. Logical NOT (!):
    • Reverses the logical state of its operand.
    • If the condition is true, ! makes it false, and vice versa.

C

int a = 10;

if (!(a > 5)) {

    printf("Condition is False\n");

} else {

    printf("Condition is True\n");

}

// Output: Condition is True

Truth Tables

Logical AND (&&):

Operand 1 Operand 2 Result

True

True

True

True

False

False

False

True

False

False

False

False

Logical OR (||):

Operand 1 Operand 2 Result

True

True

True

True

False

True

False

True

True

False

False

False

Logical NOT (!):

Operand Result

True

False

False

True

Example Program

C

#include <stdio.h>

int main() {

    int a = 10, b = 5, c = 0;

    // Logical AND

    if (a > b && b > c) {

        printf("Both conditions are true.\n");

    }

    // Logical OR

    if (a > b || c > b) {

        printf("At least one condition is true.\n");

    }

    // Logical NOT

    if (!c) {

        printf("c is zero (false).\n");

    }

    return 0;

}

Output:

Both conditions are true.

At least one condition is true.

c is zero (false).

Short-Circuit Evaluation

  • AND (&&): If the first operand is false, the second operand is not evaluated because the result is already determined to be false.
  • OR (||): If the first operand is true, the second operand is not evaluated because the result is already determined to be true.

Example:

C

int a = 5, b = 0;

if (a > 10 && b++) {

    // b++ is not executed because a > 10 is false.

}

printf("b = %d\n", b); // Output: b = 0

Operator Precedence

Operator Precedence Associativity

!

High

Right-to-left

&&

Medium

Left-to-right

`

 

`

Example:

C

int a = 5, b = 10, c = 15;

if (a < b || b > c && c > a) {

    // Evaluates as: b > c && c > a (true), then a < b || true (true)

    printf("True\n");

}

Applications

  1. Decision Making:
    • Combining multiple conditions in if statements.

C

if (age > 18 && age < 60) {

    printf("Eligible\n");

}

  1. Loops:
    • Controlling loop execution with complex conditions.

C

while (x > 0 && y < 100) {

    // Perform operations

}

  1. Validations:
    • Checking multiple constraints for input validation.

C

if (score >= 0 && score <= 100) {

    printf("Valid score.\n");

}

Logical operators are essential for writing efficient and readable conditional expressions in programming. They provide flexibility in evaluating complex scenarios with minimal code.