testing-debugging · beginner · ~15 min
Interpret system error codes and provide defensive diagnostic strings.
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.
Implement:
const char *translate_system_error(int err_num);
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""Unknown error"translate_system_error(13); // returns "Permission denied"
translate_system_error(999); // returns "Unknown error"
err_num: integer errno value.
Returns static string literal with error description.
Must return exact strings as specified. Thread-safe read-only strings.
#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;
}
Returning dynamically allocated memory that leaks; returning stack-local buffers; typos in strings.
Negative error numbers return Unknown error; 0 returns Success; standard codes match exact casing.
Solve this exercise in the browser editor — compile and run against the test harness, no setup required.