gtpc2m3pC/C++ Language Support User's Guide

getenv-Get Value of Environment Variables

This function searches the environment list for the variable and returns a pointer to a string containing its associated value.

Format

#include <stdlib.h>
char *getenv(const char *varname);

varname
A variable in the environment list.

Normal Return

Pointer to a string containing the current value associated with the variable name.

Error Return

If the variable name is not found, the getenv function returns a null pointer.

Programming Considerations

You must copy the string that is returned because a subsequent call to getenv could overwrite it.

Examples

The following example prints the value of the PATH environment variable pointed to by *pathvar.

#include <stdlib.h>
#include <stdio.h>
 
int main(void)
{
   const char *pathvar = getenv("PATH");
   printf("PATH = %s\n", pathvar ? pathvar : "<NULL>");
}

Related Information