networking · beginner · ~15 min
Check the socket() result.
socket() returns a non-negative file descriptor on success, or -1 on error. Check whether a returned fd indicates success.
Implement int socket_ok(int fd) that returns 1 if fd >= 0 (success), otherwise 0.
One int fd — a value returned by socket().
Returns 1 if fd >= 0, else 0.
socket_ok(3) -> 1
socket_ok(0) -> 1
socket_ok(-1) -> 0
fd == 0 is a valid descriptor; only -1 (negative) is the error sentinel.One integer fd returned by socket().
1 if fd >= 0, else 0.
Only a negative fd indicates failure; 0 is valid.
int socket_ok(int fd) {
/* TODO */
return 0;
}
Solve this exercise in the browser editor — compile and run against the test harness, no setup required.