🚀 Learn BLE on Linux (BlueZ) – Programming Part 1
Hands-on Bluetooth Low Energy course. Special price: ₹399
Enroll Now on Udemy →C Logical OR NOT Operators
Master the || and ! operators with truth tables, short-circuit proofs and embedded fault-check examples in our free C programming course.
Welcome to Lecture 8 of our free C programming course. In the last lecture you learned the logical AND operator. Today you finish the logical family with the OR operator (||) and the NOT operator (!). Every if, while and fault check you will write in embedded C programming is built from these three operators, so it is worth getting them completely right.
This lesson is part of a free C tutorial written for students who want C programming for embedded systems. Every program below was compiled with GCC 13 using -Wall -Wextra -std=c11 and run, so the output you see is real output, not a guess.
What You Will Learn in This Free C Programming Course Lesson
- How the
||operator decides its result, with a full truth table - Why
||sometimes skips its right-hand side (short-circuit evaluation) - How the
!operator flips a condition and why!!xis useful - How to simplify conditions with De Morgan’s laws
- How OR and NOT appear in real sensor and fault-checking code
- The classic mistakes (
x == 1 || 2and friends) that even experienced programmers make
Prerequisites
You should be comfortable with relational operators (<, >, ==, !=) and with the logical AND operator from the previous lecture. You also need a C compiler. On Ubuntu, sudo apt install gcc is enough; on Windows use MinGW or WSL. If you are following this free embedded C course for a microcontroller, you can still run every example on your PC first.
The C Logical OR Operator (||)
The OR operator joins two conditions and asks one question: is at least one of them true? It gives false (0) only when both operands are false. If either operand is true, or both are, the result is true (1).
Remember the C rule for truth: zero is false, every non-zero value is true. So the operands do not have to be comparisons. A plain variable, a function return value or a register bit can be an operand. Whatever the operands are, the result of || is always exactly 0 or 1, never the original operand value.
OR Truth Table
| Condition 1 | Condition 2 | Condition 1 || Condition 2 |
|---|---|---|
| False (0) | False (0) | False (0) |
| False (0) | True (non-zero) | True (1) |
| True (non-zero) | False (0) | True (1) |
| True (non-zero) | True (non-zero) | True (1) |
How || reads its two operands
Seeing the OR Result for Real Values
Take a = 10, b = 5 and c = 0. Variables a and b are non-zero so they count as true; c is zero so it counts as false. The program prints the truth table and then five expressions:
#include <stdio.h>
int main(void)
{
int a = 10, b = 5, c = 0;
printf("cond1 cond2 | cond1 || cond2\n");
for (int x = 0; x <= 1; x++)
for (int y = 0; y <= 1; y++)
printf(" %d %d | %d\n", x, y, x || y);
printf("\na || b = %d\n", a || b);
printf("a || c = %d\n", a || c);
printf("(a < 9) || (b > 10) = %d\n", (a < 9) || (b > 10));
printf("(b != 7) || c = %d\n", (b != 7) || c);
printf("c || c = %d\n", c || c);
return 0;
}
Build and run it:
gcc -Wall -Wextra -std=c11 -o ep_or_table ep_or_table.c
./ep_or_table
Expected output:
cond1 cond2 | cond1 || cond2
0 0 | 0
0 1 | 1
1 0 | 1
1 1 | 1
a || b = 1
a || c = 1
(a < 9) || (b > 10) = 0
(b != 7) || c = 1
c || c = 0
| Expression | Meaning with a=10, b=5, c=0 | Result |
|---|---|---|
a || b | true || true | 1 |
a || c | true || false | 1 |
(a < 9) || (b > 10) | false || false | 0 |
(b != 7) || c | true || false | 1 |
c || c | false || false | 0 |
Short-Circuit Evaluation in the OR Operator
C guarantees that the operands of || are evaluated left to right, and that evaluation stops as soon as the answer is known. If the left operand is true, the whole expression must be true, so the right operand is never evaluated. This is called short-circuit evaluation. It matters when the right operand is a function call or has a side effect such as i++.
Proving the Right Side Is Skipped
The function probe() prints a message every time it runs, so we can see exactly which operands were evaluated.
#include <stdio.h>
static int calls = 0;
static int probe(const char *name, int value)
{
calls++;
printf(" probe(%s) called, returns %d\n", name, value);
return value;
}
int main(void)
{
int r;
puts("Case 1: left side true");
calls = 0;
r = probe("L", 1) || probe("R", 0);
printf(" result=%d, calls=%d\n\n", r, calls);
puts("Case 2: left side false");
calls = 0;
r = probe("L", 0) || probe("R", 1);
printf(" result=%d, calls=%d\n", r, calls);
return 0;
}
Build and run it:
gcc -Wall -Wextra -std=c11 -o ep_or_short ep_or_short.c
./ep_or_short
Expected output:
Case 1: left side true
probe(L) called, returns 1
result=1, calls=1
Case 2: left side false
probe(L) called, returns 0
probe(R) called, returns 1
result=1, calls=2
In case 1 the right-hand probe(R) never ran. In case 2 the left side was false, so C had to evaluate the right side to decide the answer.
Why Short-Circuit Makes Code Safer
You can put a cheap or safe test on the left and a risky test on the right. For example if (buf == NULL || buf->len == 0) is safe: if buf is NULL the left side is true and buf->len is never touched. Reverse the order and you dereference a NULL pointer.
The C Logical NOT Operator (!)
NOT is a unary operator: it takes a single operand and negates it. A false (zero) operand becomes 1. A true (non-zero) operand becomes 0. Like the other logical operators, the result is always exactly 0 or 1.
| Condition | !Condition |
|---|---|
| False (0) | True (1) |
| True (non-zero) | False (0) |
The NOT operator flips the truth value
NOT in Action
With a = 10, b = 5, c = 0 the program prints these results. Notice the last two lines: !!a and !!25 both give 1.
#include <stdio.h>
int main(void)
{
int a = 10, b = 5, c = 0;
printf("!a = %d\n", !a);
printf("!c = %d\n", !c);
printf("!(b > c) = %d\n", !(b > c));
printf("!(a && c) = %d\n", !(a && c));
printf("!(a == 10) = %d\n", !(a == 10));
printf("!!a = %d\n", !!a);
printf("!!25 = %d\n", !!25);
return 0;
}
Build and run it:
gcc -Wall -Wextra -std=c11 -o ep_not ep_not.c
./ep_not
Expected output:
!a = 0
!c = 1
!(b > c) = 0
!(a && c) = 1
!(a == 10) = 0
!!a = 1
!!25 = 1
The double-NOT trick !!x converts any non-zero value to exactly 1 and zero to 0. Embedded programmers use it to turn a masked register bit such as status & 0x40 into a clean 0 or 1.
| Expression | Inside value | Result |
|---|---|---|
!a | !true | 0 |
!c | !false | 1 |
!(b > c) | !true | 0 |
!(a && c) | !false | 1 |
!(a == 10) | !true | 0 |
Precedence: ! Binds Tightest, || Binds Loosest
Among the logical operators, ! has very high precedence (it is a unary operator), && comes next, and || is lower still. Relational operators such as < and == bind tighter than && and ||, which is why a < 9 || b > 10 works without parentheses.
| Highest to lowest | Operators | Example effect |
|---|---|---|
| 1 | ! | !x == 0 means (!x) == 0 |
| 2 | < <= > >=, then == != | comparisons are done before joining |
| 3 | && | a || b && c means a || (b && c) |
| 4 | || | evaluated after all the above |
When an expression mixes && and ||, add parentheses even if the precedence rules would work. Readers (and the compiler’s warnings) will thank you.
Combining OR and NOT: De Morgan’s Laws
Two identities let you rewrite conditions. NOT of an OR equals the AND of the NOTs, and NOT of an AND equals the OR of the NOTs:
De Morgan’s laws
Checking De Morgan’s Laws by Brute Force
The program tries all four input combinations and prints both sides of each law. Matching columns prove the laws for every case.
#include <stdio.h>
int main(void)
{
printf("a b | !(a||b) (!a && !b) | !(a&&b) (!a || !b)\n");
for (int a = 0; a <= 1; a++)
for (int b = 0; b <= 1; b++)
printf("%d %d | %d %d | %d %d\n",
a, b, !(a || b), (!a && !b), !(a && b), (!a || !b));
return 0;
}
Build and run it:
gcc -Wall -Wextra -std=c11 -o ep_demorgan ep_demorgan.c
./ep_demorgan
Expected output:
a b | !(a||b) (!a && !b) | !(a&&b) (!a || !b)
0 0 | 1 1 | 1 1
0 1 | 0 0 | 1 1
1 0 | 0 0 | 1 1
1 1 | 0 0 | 0 0
Use these laws to make a negative condition read naturally. if (!(ready || timeout)) becomes if (!ready && !timeout), which reads as plain English: “not ready and not timed out”.
Embedded C Programming Example: Sensor Fault Check
In embedded systems, OR is the natural operator for fault detection: raise an alarm if any of several things is wrong. The check below flags a fault when the sensor reports not-OK, or the temperature is below -20 or above 85. Putting !sensor_ok first means the temperature value is only trusted when the sensor itself is healthy, a cheap application of short-circuiting.
A Real-World || and ! Example
Each call to check() prints the verdict:
#include <stdio.h>
static int check(int temp_c, int sensor_ok)
{
if (!sensor_ok || temp_c < -20 || temp_c > 85) {
printf("temp=%4d ok=%d -> FAULT, raise alarm\n", temp_c, sensor_ok);
return 1;
}
printf("temp=%4d ok=%d -> normal\n", temp_c, sensor_ok);
return 0;
}
int main(void)
{
check(25, 1);
check(90, 1);
check(-40, 1);
check(25, 0);
return 0;
}
Build and run it:
gcc -Wall -Wextra -std=c11 -o ep_or_sensor ep_or_sensor.c
./ep_or_sensor
Expected output:
temp= 25 ok=1 -> normal
temp= 90 ok=1 -> FAULT, raise alarm
temp= -40 ok=1 -> FAULT, raise alarm
temp= 25 ok=0 -> FAULT, raise alarm
Common Mistakes with || and !
Mistake 1: Writing x == 1 || 2
In English you say “key is 1 or 2”. In C, key == 1 || 2 means (key == 1) || 2, and the constant 2 is always true, so the whole test is always true. Watch the buggy line and the correct line side by side:
#include <stdio.h>
int main(void)
{
int key;
for (key = 0; key <= 3; key++) {
if (key == 1 || 2) /* BUG: 2 is always true */
printf("key=%d buggy test says: accepted\n", key);
if (key == 1 || key == 2) /* correct */
printf("key=%d correct test says: accepted\n", key);
}
return 0;
}
Expected output:
key=0 buggy test says: accepted
key=1 buggy test says: accepted
key=1 correct test says: accepted
key=2 buggy test says: accepted
key=2 correct test says: accepted
key=3 buggy test says: accepted
The buggy test accepted keys 0 and 3 as well. The scary part: GCC 13 with -Wall -Wextra printed no warning for this program, so only your own review will catch it.
Mistake 2: Mixing Up || and |
|| is the logical operator and produces 0 or 1. | is the bitwise OR, which combines bits of integers and always evaluates both sides. 1 | 2 is 3, while 1 || 2 is 1. Bitwise operators are coming later in this free embedded C course.
Mistake 3: Precedence of ! Against ==
if (!x == 0) is read as if ((!x) == 0). That is true whenever x is non-zero, which is probably the opposite of what you meant. Write if (!(x == 0)) or, better, if (x != 0).
Hidden Side Effects
Because the right side of || may be skipped, never hide an important side effect there. if (ready || i++ > 3) increments i only when ready is false. That is legal but a debugging nightmare.
Best Practices
- Put the cheapest or safest condition on the left of
||. - Repeat the variable in each comparison:
k == 1 || k == 2, neverk == 1 || 2. - Use parentheses whenever you mix
&&and||. - Prefer
x != 0to!x == 0; use!xonly when you really mean “x is zero/false”. - Name boolean-like variables so
!reads well:if (!buffer_full). - Include
<stdbool.h>and usebool,true,falsefor flags in modern C.
Performance and Safety Notes
Short-circuiting is a free optimisation: the CPU skips work when the left side already decides the answer. On small microcontrollers, order your conditions from “most likely to decide” to “least likely”. For safety-critical code, standards such as MISRA C discourage side effects on the right side of || and &&.
Summary: Key Takeaways from This Free C Programming Course Lesson
| Operator | Operands | Result is 1 when | Short-circuits? |
|---|---|---|---|
|| | 2 | at least one operand is non-zero | Yes, when the left side is true |
! | 1 | the operand is zero | Not applicable |
- Zero is false and every non-zero value is true; results are only ever 0 or 1.
||evaluates left to right and stops early;!flips the truth value.!(a || b)equals!a && !b(De Morgan).x == 1 || 2is a bug; write the comparison twice.
Conclusion
You now know all three logical operators. They look small, but conditions built from them decide what every program does, from a loop that polls a UART to an interrupt handler that shuts a motor off on overheating. In the next lecture of this free C programming course you will meet the only operator in C with three operands, the conditional (ternary) operator.
Quiz: Test Your Understanding
Try each question first, then click to check. Assume a = 10, b = 5, c = 0 where variables are used.
Question 1. What is the value of (a < 9) || (b > 10)?
Show answer
0. Both comparisons are false, so OR gives false.
Question 2. What does printf("%d", !5); print?
Show answer
0. Five is non-zero (true), and NOT turns it into false.
Question 3. If f() returns 5, what is the value of f() || g(), and is g() called?
Show answer
The value is 1, not 5, and g() is not called because the left side is already true.
Question 4. Given int x = 0; what is !!x + !x?
Show answer
!!0 is 0 and !0 is 1, so the sum is 1.
Question 5. When is if (k == 3 || 4) true?
Show answer
Always. The constant 4 is non-zero, so the right side is always true regardless of k.
Question 6. Which expression equals !(a || b)?
Show answer
!a && !b, by De Morgan’s law.
Interview Questions: Free C Programming Course Edition
Interview Question 1. What is short-circuit evaluation?
Show answer
The logical operators && and || stop evaluating as soon as the result is known. For || the right side is skipped when the left side is true.
Interview Question 2. What is the difference between || and |?
Show answer
|| is logical OR on truth values and returns 0 or 1 with short-circuiting. | is bitwise OR on the bits of integers and always evaluates both operands.
Interview Question 3. What does !!x do and why would you use it?
Show answer
It normalises any value to exactly 0 or 1. It is common for converting a masked bit into a clean boolean.
Interview Question 4. Is the order of evaluation of a() || b() defined?
Show answer
Yes. There is a sequence point after the left operand, so a() always runs first and completely before b().
Interview Question 5. Rewrite !(x > 5 || y < 2) without the outer NOT.
Show answer
x <= 5 && y >= 2, using De Morgan’s law and flipping each comparison.
Interview Question 6. What does if (n == 1 || n == 2 || n == 3) look like as a switch?
Show answer
switch (n) { case 1: case 2: case 3: ... break; }. Both are fine; switch scales better as the list grows.
Frequently Asked Questions
What does the || operator do in C?
It is the logical OR operator. It returns 1 if at least one operand is non-zero and 0 only when both operands are zero.
Does C evaluate both sides of ||?
Not always. If the left operand is true, the right operand is skipped because the result is already known. This is called short-circuit evaluation.
What value does the ! operator return?
It returns 1 if its operand is zero and 0 if its operand is non-zero. The result is always exactly 0 or 1.
Why is if (x == 1 || 2) always true?
It is parsed as (x == 1) || 2. The constant 2 is non-zero, so the right side is always true. Write x == 1 || x == 2 instead.
What is the difference between || and | in C?
|| is logical OR, working on truth values with short-circuiting. | is bitwise OR, combining the bits of two integers and evaluating both sides.
What are De Morgan’s laws in C?
!(a || b) is the same as !a && !b, and !(a && b) is the same as !a || !b. They help you simplify negative conditions.
Is this C programming lesson free?
Yes. This lesson is part of the free C programming course on EmbeddedPathashala, designed for students learning embedded C programming.
Keep Going in the Free C Programming Course
Next up: the conditional (ternary) operator. Practise today’s programs, change the values, and predict the output before you run them.

2 Comments