networking · beginner · ~15 min
Bound-check a port number.
Check whether an integer is a usable TCP/UDP port number.
Implement int is_valid_port(int p) that returns 1 if p is in the usable port range 1..65535, otherwise 0.
One int p (may be 0, negative, or larger than 65535).
Returns 1 for a valid port, 0 otherwise.
is_valid_port(80) -> 1
is_valid_port(65535) -> 1
is_valid_port(0) -> 0
is_valid_port(70000) -> 0
One integer p.
1 if 1 <= p <= 65535, else 0.
Valid range is 1..65535 inclusive; 0 is not valid.
int is_valid_port(int p) {
/* TODO */
return 0;
}
Solve this exercise in the browser editor — compile and run against the test harness, no setup required.