basics · beginner · ~15 min
Branch on a value's sign.
Reduce a number to its sign: negative, zero, or positive.
Implement int sign_of(long n) that returns -1 if n is negative, 0 if n is zero, and 1 if n is positive. No main — the grader calls it.
A single long n.
Returns -1, 0, or 1.
sign_of(-9) -> -1
sign_of(0) -> 0
sign_of(42) -> 1
A single long n.
-1, 0, or 1 for negative, zero, or positive n.
n is a 64-bit signed integer (long). Returns 1 for positive, -1 for negative, 0 for zero.
int sign_of(long n) {
/* TODO */
return 0;
}
Negating LONG_MIN which can cause undefined behavior in signed arithmetic.
n == 0, n == LONG_MIN, n == LONG_MAX, n == -1, n == 1.
Solve this exercise in the browser editor — compile and run against the test harness, no setup required.