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).
| Operator | Name | Result is true when... | Example |
|---|---|---|---|
&& | AND | both conditions are true | marks >= 40 && attendance >= 75 |
|| | OR | at least one condition is true | age < 12 || age > 60 |
! | NOT | the condition is false (reverses it) | !is_member |
Truth tables (1 = true, 0 = false)
| A | B | A && B | A || B | !A |
|---|---|---|---|---|
| 0 | 0 | 0 | 0 | 1 |
| 0 | 1 | 0 | 1 | 1 |
| 1 | 0 | 0 | 1 | 0 |
| 1 | 1 | 1 | 1 | 0 |
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
marks >= 40is true, butattendance >= 75is false (70). AND needs both, so the result is 0.age > 60is true (65). OR needs only one, so the result is 1.is_memberis 0 (false); NOT makes it 1.!5: 5 is non-zero (true), NOT gives 0.!0gives 1.
How Short-Circuit Evaluation Works
- C checks the left condition first.
- For
&&: if the left side is 0, the whole result is 0. The right side is skipped. - For
||: if the left side is 1, the whole result is 1. The right side is skipped. - Only when the left side does not decide the answer is the right side checked.
- 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
| Mistake | What happens | Fix |
|---|---|---|
if (x > 10 & x < 20) | Bitwise AND: works here by luck, but no short-circuit | Use && |
if (10 < x < 20) | Always true | if (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 run | It may be skipped | Don't put i++ or function calls you need on the right side |
Logical Operators Practice Questions
- Name the three logical operators and their symbols.
- Write the truth table of AND and OR.
- What is short-circuit evaluation?
- What is the output of
printf("%d", !(5 > 3));? - Write a condition to check if a number is between 1 and 100 (both included).
Show answers
- Logical AND
&&, logical OR||, logical NOT!. - 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.
- 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. - 0
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 && 0gives:A) 1 B) 0 C) 10 D) Error
-
0 || 1gives:A) 0 B) 1 C) 01 D) Error
-
In
a && b, ifais false:A) b is checked B) b is skipped C) Error D) Result is 1
-
Which is a unary logical operator?
A)
&&B)||C)!D)== -
Is 1900 a leap year?
A) Yes B) No
Show answers
- B: 0.
- B: 1.
- B: b is skipped (short-circuit).
- C:
! - 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 when | Both are true |
| OR true when | At least one is true |
| NOT | Reverses: !0 = 1, !5 = 0 |
| Short-circuit | Right 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.