testing-debugging · beginner · ~15 min

Errno Diagnostic Translator

Interpret system error codes and provide defensive diagnostic strings.

Challenge

System call failures in C populate the global errno variable. Defensive diagnostics translate raw error numbers into clear, standardized operational status messages for telemetry and logs.

Your Task

Implement:

const char *translate_system_error(int err_num);

Translation Table

  • 0: return "Success"
  • 2 (ENOENT): return "No such file or directory"
  • 13 (EACCES): return "Permission denied"
  • 12 (ENOMEM): return "Out of memory"
  • 22 (EINVAL): return "Invalid argument"
  • 28 (ENOSPC): return "No space left on device"
  • Any other value: return "Unknown error"

Example

translate_system_error(13); // returns "Permission denied"
translate_system_error(999); // returns "Unknown error"

Input format

err_num: integer errno value.

Output format

Returns static string literal with error description.

Constraints

Must return exact strings as specified. Thread-safe read-only strings.

Starter code

#include <stddef.h>

/* Return human-readable diagnostic description for given errno value. */
const char *translate_system_error(int err_num) {
    (void)err_num;
    return NULL;
}

Common mistakes

Returning dynamically allocated memory that leaks; returning stack-local buffers; typos in strings.

Edge cases to handle

Negative error numbers return Unknown error; 0 returns Success; standard codes match exact casing.

Background lessons

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