function
<cstdlib>

getenv

char* getenv (const char* name);
Get environment string
Retrieves a C-string containing the value of the environment variable whose name is specified as argument. If the requested variable is not part of the environment list, the function returns a null pointer.

The pointer returned points to an internal memory block, whose content or validity may be altered by further calls to getenv (but not by other library functions).

The string pointed by the pointer returned by this function shall not be modified by the program. Some systems and library implementations may allow to change environmental variables with specific functions (putenv, setenv...), but such functionality is non-portable.

Parameters

name
C-string containing the name of the requested variable.
Depending on the platform, this may either be case sensitive or not.

Return Value

A C-string with the value of the requested environment variable, or a null pointer if such environment variable does not exist.

Example

1
2
3
4
5
6
7
8
9
10
11
12
/* getenv example: getting path */
#include <stdio.h>      /* printf */
#include <stdlib.h>     /* getenv */

int main ()
{
  char* pPath;
  pPath = getenv ("PATH");
  if (pPath!=NULL)
    printf ("The current path is: %s",pPath);
  return 0;
}


The example above prints the PATH environment variable, if such a variable exists in the hosting environment.

Data races

Concurrently calling this function is safe, provided that the environment remains unchanged.

Exceptions (C++)

No-throw guarantee: this function never throws exceptions.

See also