getenv

From cppreference.com
< c‎ | program
Defined in header <stdlib.h>
char *getenv( const char *env_var );

Searches for an environmental variable with name env_var in the host-specified environment list and returns information associated with it. The set of environmental variables and methods of altering it are implementation-defined.

This function is not required to be thread-safe. Another call to getenv, as well as a call to the POSIX functions setenv(), unsetenv(), and putenv() may invalidate the pointer returned by a previous call or modify the string obtained from a previous call.

Modifying the string returned by getenv invokes undefined behavior.

Contents

[edit] Parameters

env_var - null-terminated character string identifying the name of the environmental variable to look for

[edit] Return value

character string identifying the value of the environmental variable or NULL if such variable is not found.

[edit] Notes

On POSIX systems, the environment variables are also accessible through the global variable environ, declared as extern char **environ; in <unistd.h>, and through the optional third argument, envp, of the main function.

[edit] Example

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

Output:

PATH = /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin

[edit] See also

C++ documentation for getenv