networking · beginner · ~15 min

Port within scan range?

Bound a port to a scan window.

Challenge

Check whether a port number falls within an inclusive range.

Task

Implement int port_in_range(int port, int lo, int hi).

Input

  • port: the port number to test.
  • lo, hi: the inclusive bounds of the range.

Output

Return 1 if lo <= port <= hi, else 0.

Example

port_in_range(80,   1, 1024)  ->  1
port_in_range(1,    1, 1024)  ->  1   (inclusive lower bound)
port_in_range(2000, 1, 1024)  ->  0

Input format

port: the port to test; lo and hi: the inclusive range bounds.

Output format

1 if lo <= port <= hi, else 0.

Constraints

Both ends of the range are inclusive.

Starter code

int port_in_range(int port, int lo, int hi) {
    /* TODO */
    return 0;
}

Solve this exercise in the browser editor — compile and run against the test harness, no setup required.