linux-sysprog · beginner · ~15 min

getenv with a default

Read the environment with a fallback.

Challenge

Implement const char *getenv_or(const char *name, const char *dflt) returning the environment variable name, or dflt if it is unset.

Starter code

#include <stdlib.h>

const char *getenv_or(const char *name, const char *dflt) {
    /* TODO */
    return dflt;
}

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