networking · beginner · ~15 min

Well-known port?

Recall the privileged port range.

Challenge

Decide whether a port is in the "well-known" range — ports 1 through 1023, which require privilege (root) to bind.

Task

Implement int is_well_known_port(int p) that returns 1 if p is in the well-known range 1..1023, otherwise 0.

Input

One int p.

Output

Returns 1 if 1 <= p <= 1023, else 0.

Example

is_well_known_port(80)     ->   1
is_well_known_port(1023)   ->   1
is_well_known_port(1024)   ->   0
is_well_known_port(0)      ->   0

Edge cases

  • 1023 is well-known; 1024 is the first non-privileged port.
  • 0 is not in the range.

Input format

One integer p.

Output format

1 if 1 <= p <= 1023, else 0.

Constraints

Well-known range is 1..1023 inclusive.

Starter code

int is_well_known_port(int p) {
    /* TODO */
    return 0;
}

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