Environment variables are a universal mechanism for conveying configuration information to Unix programs. Let’s look at how to set, get, and list environment variables.
Running the program shows that we pick up the value for FOO that we set in the program, but that BAR is empty.
The list of keys in the environment will depend on your particular machine.
If we set BAR in the environment first, the running program picks that value up.
Note that in C, we need to include the necessary header files and use the environ external variable to access the environment. The setenv and getenv functions are used to set and get environment variables, respectively. We use strdup and free to safely handle memory when splitting the environment strings.