getenv() — Get value of environment variables

Standards

Standards / Extensions C or C++ Dependencies
ISO C
POSIX.1
XPG4
XPG4.2
C99
Single UNIX Specification, Version 3
both  

Format

#include <stdlib.h>

char *getenv(const char *varname);

General description

Searches the table of environment variables for an entry corresponding to varname and returns a pointer to a buffer containing the current string value of varname.

Special behavior for POSIX: Under POSIX, the value of the char **environ pointer is honored and used by getenv(). You can declare and use this pointer. Under POSIX(OFF) this is not the case: the table start cannot be modified.

Start of change

Usage Note

To avoid changing the environment variables or management process, the caller must ensure not to modify or release the buffer that is pointed by the returned pointer.

End of change

Returned value

If successful, getenv() returns a pointer to a buffer containing the current string value of varname. You should copy the string that is returned because a subsequent call to getenv() will overwrite it.

If the varname is not found, getenv() returns a NULL pointer. The returned value is NULL if the given variable is not currently defined.

Example

CELEBG05
/* CELEBG05

   In this example, *pathvar points to the value of the PATH
   environment variable.
   In a POSIX environment, this variable would be from the CENV
   group ID.

 */
#include <stdlib.h>
#include <stdio.h>

int main(void)
{
   char *pathvar;

   pathvar = getenv("PATH");
   printf("pathvar=%s",pathvar);
}

Related information