Nearby lessons

30 of 124

C - Logical Operators

Logical Operators is one of the foundational topics in C programming. This lesson explains Logical Operators (&& || !) and Program: AND, OR and NOT with complete, runnable code examples, clear step-by-step explanations, and common mistakes to avoid.

Logical Operators (&& || !)

OperatorMeaningTrue when
&&ANDBoth sides are true
||ORAt least one side is true
!NOTThe condition is false
Trainer's Note: Memory trick: `&&` is strict — BOTH must say yes. `||` is relaxed — ONE yes is enough. `!` is the liar — it flips the answer. Practise: ! on a true condition gives false (0).

Program: AND, OR and NOT

Example02
CCode Cell
1#include <stdio.h>
2 
3void main()
4{
5 int age = 25, marks = 80;
6 
7 printf("AND: age>18 && marks>70 : %d\n", age > 18 && marks > 70); // 1
8 printf("AND: age<18 && marks>70 : %d\n", age < 18 && marks > 70); // 0
9 printf("OR : age<18 || marks>70 : %d\n", age < 18 || marks > 70); // 1
10 printf("OR : age<18 || marks<40 : %d\n", age < 18 || marks < 40); // 0
11 printf("NOT: !(age > 18) : %d\n", !(age > 18)); // 0
12 printf("NOT: !(age > 30) : %d\n", !(age > 30)); // 1
13}
Output

AND: age>18 && marks>70  : 1
AND: age<18 && marks>70  : 0
OR : age<18 || marks>70  : 1
OR : age<18 || marks<40  : 0
NOT: !(age > 18)         : 0
NOT: !(age > 30)         : 1
      
📝 Key Takeaways
  • Operators: arithmetic (+ - * / %), relational (== != < > <= >=), logical (&& || !), assignment (= += -= *= /= %=), ++ --, ternary ?:.
  • Each operator family has its own complete program — copy and run one at a time.
  • Integer division drops decimals: 5/2 = 2; use 5.0/2 for 2.5.
  • % gives the remainder and works only on integers.
  • a++ uses then increases; ++a increases then uses.
  • Ternary: condition ? value1 : value2.
  • Use brackets to control the order of evaluation.