A structured lesson workspace with readable content, hands-on examples, and a clean path to completion.
Three operators: && (AND), || (OR), ! (NOT). Simple enough. The complexity hides in two behaviors that trip up every cohort of students I have taught. First, short-circuit evaluation means the right operand might never execute. Second, operator precedence means a && b || c does not group the way your eyes expect.
In the expression a && b, if a is false, b is never evaluated. The result is already determined. This is not an optimization the compiler might skip. It is guaranteed by the standard. Programmers exploit this deliberately to guard against null pointer dereferences and division by zero.
#include <stdio.h>int main(void) {int *ptr = NULL;if (ptr != NULL && *ptr > 10) {printf("Pointer is valid and value exceeds 10\n");} else {printf("Guarded safely. No crash.\n");}int divisor = 0;if (divisor != 0 && 100 / divisor > 5) {printf("This never prints\n");} else {printf("Division by zero avoided.\n");}return 0;}
If you reverse the order and write *ptr > 10 && ptr != NULL, you dereference NULL before checking it. Segfault. The order of operands in a logical AND is not cosmetic. It is a safety mechanism.
The expression a || b && c parses as a || (b && c), not (a || b) && c. This matches mathematical convention (AND before OR), but if you are reading code at speed, your brain might group it wrong. When in doubt, add parentheses. They compile to nothing. They cost zero cycles. They save the next reader ten minutes of staring.
#include <stdio.h>int main(void) {int a = 1, b = 0, c = 1;int result1 = a || b && c;int result2 = (a || b) && c;printf("a || b && c = %d\n", result1);printf("(a || b) && c = %d\n", result2);return 0;}
In this particular case both evaluate to 1, but change a to 0 and they diverge. The point is not the specific output. The point is that you should never have to mentally parse precedence rules while reading production code. Parenthesize compound logical expressions.
Logical operators are the connective tissue of every conditional in C. You will use them in every loop guard, every error check, every validation function from here to the end of this course. Next: switch statements, which solve a different problem and have their own notorious fall-through behavior.
Finish the lesson once you have worked through the material. This awards ★ 20 XP.