linux-sysprog · intermediate · ~15 min

Env var with default

`getenv` and the convention "missing or empty means absent".

Challenge

Implement const char *getenv_default(const char *name, const char *fallback) returning the value of the environment variable name if set and non-empty, otherwise fallback.

Starter code

#include <stdlib.h>

const char *getenv_default(const char *name, const char *fallback) {
    /* TODO */
    return fallback;
}

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