basics · beginner · ~10 min
Recognize implicit conversion in signed/unsigned compares.
Compare a signed int against an unsigned size_t correctly, avoiding C's implicit-conversion trap where -1 < 5u evaluates to false.
Implement int safer_lt(int a, size_t b) that returns 1 if a is mathematically less than b, else 0. The catch: in a naive a < b, C promotes a to size_t, so -1 becomes SIZE_MAX and the comparison is wrong. Handle the sign of a explicitly before any unsigned comparison.
A signed int a and an unsigned size_t b.
Returns 1 when a < b mathematically, otherwise 0. Any negative a is less than any (non-negative) b.
safer_lt(-1, 5) -> 1 (naive a < b would wrongly give 0)
safer_lt(5, 10) -> 1
safer_lt(10, 5) -> 0
safer_lt(0, 0) -> 0
safer_lt(0, 1) -> 1
a (including INT_MIN) is always less than any size_t.a == 0, b == 0 returns 0.b near SIZE_MAX must compare correctly.a first.if (signed_var < unsigned_var) triggers implicit conversion that flips negative numbers to huge positives. Real-world CVEs (e.g. in mmap-size validation) come from this.
A signed int a and an unsigned size_t b.
1 if a is mathematically less than b, otherwise 0.
Handle negative a before any unsigned comparison; no casts that hide the rule.
#include <stddef.h>
int safer_lt(int a, size_t b) { /* TODO */ return 0; }
Writing the naïve return a < b; and not catching that -1 < (size_t)5 evaluates false because (size_t)-1 == SIZE_MAX.
a == INT_MIN; a == 0, b == 0; large b near SIZE_MAX.
O(1).
Solve this exercise in the browser editor — compile and run against the test harness, no setup required.