To set or clear environment variables, use the setenv and unsetenv functions, respectively.
http://siber.cankaya.edu.tr/SystemsProgramming/cfiles/print_env.cprint_env.c
#include <stdio.h>
/* The ENVIRON variable contains the environment. */
extern char** environ;
int main ()
{
char** var;
for (var = environ; *var != NULL; ++var)
printf ("%s\n", *var);
return 0;
}
Don't modify environ yourself; use the setenv and unsetenv functions instead.
http://siber.cankaya.edu.tr/SystemsProgramming/cfiles/client.cclient.c
#include <stdio.h>
#include <stdlib.h>
int main ()
{
char* server_name = getenv ("SERVER_NAME");
if (server_name == NULL)
/* The SERVER_NAME environment variable was not set. Use the
default. */
server_name = "server.my-company.com";
printf ("accessing server %s\n", server_name);
/* Access the server here... */
return 0;
}
$ client
accessing server server.my-company.com
$ export SERVER_NAME=backup-server.elsewhere.net
$ client
accessing server backup-server.elsewhere.net