networking · beginner · ~15 min
Track progress through a partial-send loop.
send() may transmit only part of the buffer, so a correct sender loops, advancing by each call's return value. Compute how many bytes are still unsent.
Implement int bytes_remaining(int total, int sent) that returns total - sent, but never less than 0.
total: total bytes to send.sent: bytes sent so far.Returns the number of bytes still unsent, floored at 0.
bytes_remaining(100, 30) -> 70
bytes_remaining(100, 100) -> 0
bytes_remaining(100, 120) -> 0
sent exceeds total, return 0 (never a negative count).Two integers: total bytes to send and bytes already sent.
total - sent, never below 0.
Floor the result at 0.
int bytes_remaining(int total, int sent) {
/* TODO */
return 0;
}
Solve this exercise in the browser editor — compile and run against the test harness, no setup required.