Skip to content
StudyFame

Logical Operators in C

Real decisions often have more than one condition: "Pass if marks ≥ 40 and attendance ≥ 75%." "Discount if the customer is a student or a senior citizen."

Logical operators join conditions like this. In this lesson you will learn AND, OR and NOT with truth tables, and a smart C feature called short-circuit evaluation.

What You Will Learn

  • The 3 logical operators: &&, ||, !.
  • Truth tables for each one.
  • Short-circuit evaluation and why it is useful.
  • The leap year condition (a classic exam program).
  • The difference between && and &.

Logical Operators in C Explained

Exam definition: Logical operators combine or reverse conditions. C has three: && (logical AND), || (logical OR) and ! (logical NOT). They return 1 (true) or 0 (false).

OperatorNameResult is true when...Example
&&ANDboth conditions are truemarks >= 40 && attendance >= 75
||ORat least one condition is trueage < 12 || age > 60
!NOTthe condition is false (reverses it)!is_member

Truth tables (1 = true, 0 = false)

ABA && BA || B!A
00001
01011
10010
11110

Memory trick: AND is strict like a strict teacher: everything must be right (one 0 → 0). OR is kind: one right answer is enough (one 1 → 1). NOT is opposite: it flips 0 ↔ 1.

Short-circuit evaluation

  • A && B: if A is false, the answer is already false, so C does not check B.
  • A || B: if A is true, the answer is already true, so C does not check B.
  • This makes programs faster, and lets you write safe checks like b != 0 && a / b > 2: the division never runs when b is 0.

Syntax of Logical Operators

(condition1) && (condition2)
(condition1) || (condition2)
!(condition)

Example 1: AND, OR and NOT in Action

#include <stdio.h>

int main(void)
{
    int marks = 65, attendance = 70;
    int age = 65;
    int is_member = 0;

    printf("Pass (marks>=40 AND attendance>=75): %d\n",
           marks >= 40 && attendance >= 75);
    printf("Ticket discount (age<12 OR age>60) : %d\n",
           age < 12 || age > 60);
    printf("Not a member (!is_member)         : %d\n", !is_member);
    printf("!5 = %d, !0 = %d\n", !5, !0);

    return 0;
}

Output

Pass (marks>=40 AND attendance>=75): 0
Ticket discount (age<12 OR age>60) : 1
Not a member (!is_member)         : 1
!5 = 0, !0 = 1

Code Explanation

  1. marks >= 40 is true, but attendance >= 75 is false (70). AND needs both, so the result is 0.
  2. age > 60 is true (65). OR needs only one, so the result is 1.
  3. is_member is 0 (false); NOT makes it 1.
  4. !5: 5 is non-zero (true), NOT gives 0. !0 gives 1.

How Short-Circuit Evaluation Works

  1. C checks the left condition first.
  2. For &&: if the left side is 0, the whole result is 0. The right side is skipped.
  3. For ||: if the left side is 1, the whole result is 1. The right side is skipped.
  4. Only when the left side does not decide the answer is the right side checked.
  5. So put the safety check first: if (b != 0 && a / b > 2).

Example 2: Leap Year and a Safe Division Check

#include <stdio.h>

int main(void)
{
    int year;
    int a = 10, b = 0;

    printf("Enter a year: ");
    scanf("%d", &year);

    int leap = (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0);
    printf("%d is %s\n", year, leap ? "a leap year" : "not a leap year");

    /* b is 0, so a / b is never calculated: no crash */
    if (b != 0 && a / b > 2)
        printf("Big ratio\n");
    else
        printf("Division skipped safely\n");

    return 0;
}

Output

Enter a year: 2024
2024 is a leap year
Division skipped safely

A year is a leap year if it is divisible by 4 but not by 100, or if it is divisible by 400. So 2024 and 2000 are leap years, but 1900 is not.

Important Points to Remember

  • 3 logical operators: && (AND), || (OR), ! (NOT).
  • AND: true only if both are true. OR: true if any one is true.
  • ! is unary; && and || are binary.
  • Short-circuit: the right side is skipped when the left side decides.
  • && has higher precedence than ||; use brackets to be clear.

Common Mistakes Beginners Make

MistakeWhat happensFix
if (x > 10 & x < 20)Bitwise AND: works here by luck, but no short-circuitUse &&
if (10 < x < 20)Always trueif (x > 10 && x < 20)
if (x == 1 || 2)Always true (2 is non-zero)if (x == 1 || x == 2)
Expecting the right side to always runIt may be skippedDon't put i++ or function calls you need on the right side

Logical Operators Practice Questions

  1. Name the three logical operators and their symbols.
  2. Write the truth table of AND and OR.
  3. What is short-circuit evaluation?
  4. What is the output of printf("%d", !(5 > 3));?
  5. Write a condition to check if a number is between 1 and 100 (both included).
Show answers
  1. Logical AND &&, logical OR ||, logical NOT !.
  2. AND: 0 0→0, 0 1→0, 1 0→0, 1 1→1. OR: 0 0→0, 0 1→1, 1 0→1, 1 1→1.
  3. C stops evaluating as soon as the result is known: && skips the right side if the left is false, || skips it if the left is true.
  4. 0
  5. n >= 1 && n <= 100

Coding Exercise

Read a character and print 1 if it is a vowel (a, e, i, o, u, lowercase) and 0 otherwise, using ||.

Show solution
#include <stdio.h>

int main(void)
{
    char ch;

    printf("Enter a letter: ");
    scanf(" %c", &ch);

    int vowel = ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u';
    printf("Is vowel: %d\n", vowel);
    return 0;
}
Enter a letter: e
Is vowel: 1

Quick Quiz: Logical Operators in C (MCQ)

  1. 1 && 0 gives:

    A) 1   B) 0   C) 10   D) Error

  2. 0 || 1 gives:

    A) 0   B) 1   C) 01   D) Error

  3. In a && b, if a is false:

    A) b is checked   B) b is skipped   C) Error   D) Result is 1

  4. Which is a unary logical operator?

    A) &&   B) ||   C) !   D) ==

  5. Is 1900 a leap year?

    A) Yes   B) No

Show answers
  1. B: 0.
  2. B: 1.
  3. B: b is skipped (short-circuit).
  4. C: !
  5. B: No. It is divisible by 100 but not by 400.

Quick Revision for Exams and Interviews

Read this before your exam. The highlighted words are the answer.

Must-remember facts

Operators&& AND, || OR, ! NOT
AND true whenBoth are true
OR true whenAt least one is true
NOTReverses: !0 = 1, !5 = 0
Short-circuitRight side skipped if left decides
Precedence! > && > ||
Leap year(y%4==0 && y%100!=0) || y%400==0

Frequently asked questions

What is short-circuit evaluation?
C stops checking as soon as the answer is known: false && ... and true || ... skip the right side.
What is the difference between && and &?
&& is logical AND with short-circuit; & is bitwise AND on every bit, with no short-circuit.
What does !5 give?
0, because 5 is non-zero (true) and NOT flips it to false.
Why write b != 0 first in b != 0 && a / b?
So that, by short-circuit, the division never runs when b is 0.

Remember

  • AND is strict, OR is kind, NOT is opposite.
  • Put the safety check first in &&.

Next Topic

Next lesson: Assignment Operators in C. You will learn shortcuts like += and *= for updating variables.