Logical Operators in C
Logical operators combine conditions: && (and), || (or), and ! (not). Both && and || short-circuit — evaluation stops the moment the result is decided, so in p && q, q never runs when p is false. This is both a performance feature and a safety idiom: p && *p == v never dereferences a null pointer.
Precedence trips people up: && binds tighter than ||, so a || b && c means a || (b && c) — parentheses remove all doubt. Results are ints 1 or 0, like all C comparisons.
&&, ||, !
And needs both sides nonzero, or needs at least one, not flips zero/nonzero. Results are int 1 or 0.
Short-circuit evaluation
&& stops at the first zero, || stops at the first nonzero — later operands may never execute, guarding null checks and bounds.
Precedence: !, then &&, then ||
!a && b parses as (!a) && b, and a || b && c as a || (b && c). Parenthesize anything non-trivial.
Any nonzero is true
Pointers, chars, and floats all test directly — if (p) means p != NULL, if (*s) means not at the terminator.
#include <stdio.h>#include <stdbool.h> int main(void) { int age = 20; bool hasId = true; printf("%d\n", age >= 18 && hasId); printf("%d\n", age < 18 || !hasId); return 0;}Recommended resourcesAffiliate links · may earn us a commission
Recommended for learners
Practice what you learned
Re-implement each visualized algorithm in C yourself — building beats watching.