An environment variable consists of a pair of strings: the variable itself followed by = and its associated value. For example:
PATH=C:\WINDOWS
In DOS and Windows, you can define an environment variable from the command line or from a batch file using the command SET followed by an assignment expression. The following line creates an environment variable called VERSION whose value is 1.1.3:
SET VERSION=1.1.3
In Unix, you omit the SET command:
VERSION=1.1.3
Reading an Environment Variable
A program's environment is made available to it at startup. The environment consists of an array of C-strings, each in the form: "VAR=value". You access environment variables from a program using the functions getenv() and putenv().
The ANSI C function getenv() provides an easy and portable way to read an environment variable. getenv() is declared in <stdlib.h> and has the following prototype:
char * getenv(const char * name);
On success, it returns a pointer to a null-terminated char array that contains the variable's value. If the variable is not defined, getenv() returns NULL. For example:
#include <stdlib.h>
#include <stdio.h>
int main()
{
char * descr = getenv("PATH");
if (descr)
printf("value of PATH is: %s", descr);
else
printf("variable not defined");
}
Setting an Environment Variable
To define an environment variable or change the value of an existing variable, use the putenv() function. putenv() isn't defined by the ANSI/ISO standard. In practice, however, both Unix and Windows declare it in <stdlib.h> as a non-standard extension, so it's relatively portable. putenv() has the following prototype:
int putenv(const char * var);
The argument var must be a string in the form "VAR=VAL". putenv() adds the variable VAR to the current environment and assigns the value VAL to it. If the variable already exists, putenv() overrides the existing value. If you prefer not to override a variable's value, check whether it already exists by calling getenv() before calling putenv(). The following example adds a new environment variable called TEMP whose value is C:\TEMP (recall that a backslash must appear as a \\ inside a quoted string in C/C++). On success, putenv() returns 0, and -1 otherwise.
int stat = putenv("TEMP=C:\\TEMP");
if (!stat)
{
printf("failed to define environment variable");
}
Support for Wide Character Variables
Win32 also defines wide character versions of getenv() and putenv(). Note that _wputenv() exists only on NT and Win2000:
wchar_t * _wgetenv(const wchar_t * name);
int _wputenv(const wchar_t * var);
Uses of Environment Variables
Environment variables enable you to control the program's behavior without having to make any changes to its source files or recompiling it. For instance, you can control whether a program displays debug information on the screen simply by defining an appropriate environment variable. This is particularly useful if you need to switch on debug output at a customer's site. In the following example, the program displays debug information only if the environment variable DBG is defined:
void func();
#include <stdlib.h>
#include <stdio.h>
int main()
{
bool debug=false;
if (getenv("DBG"))
debug=true;
if (debug)
printf("calling func");
func();
if (debug)
printf("returned from func");
}