linux-sysprog · beginner · ~10 min

Translate an errno value into a friendly string

Compose `strerror` + `snprintf` with a cap.

Challenge

Implement void describe_errno(int err, char *out, size_t cap).

Format: "errno=%d (%s)" where the string is strerror(err). Cap-aware: snprintf semantics.

Why this matters

A user-facing error message that says 'errno=2' is useless. Every defensive tool wraps strerror into something readable.

Input format

errno value + buffer.

Output format

Formatted string in out.

Constraints

Use snprintf. Cap at cap.

Starter code

#include <stddef.h>
void describe_errno(int err, char *out, size_t cap) { /* TODO */ }

Common mistakes

Calling strerror without a cap.

Edge cases to handle

errno == 0 ("Success"); unknown errno value (strerror returns "Unknown error N").

Complexity

O(1).

Background lessons

Up next

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