Namespaces
Variants
Views
Actions

getenv

From cppreference.com
< c‎ | program
Defined in header <stdlib.h>
const 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.

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] Example

#include <stdio.h>
#include <stdlib.h>
 
int main(void)
{
    char * env_p;
    env_p = getenv ("PATH");
    if (env_p == NULL)
    { 
       printf("getenv() failed in file %s at line # %d", __FILE__,__LINE__);
       exit(1);
    }
    printf("PATH = %s\n", env_p);
 
    env_p = getenv ("HOME");
    printf("HOME = %s\n", env_p);
 
    env_p = getenv ("PWD");
    printf("PWD = %s\n", env_p);
 
    env_p = getenv ("LD_LIBRARY_PATH");
    printf("LD_LIBRARY_PATH = %s\n", env_p);
 
    env_p = getenv ("LANG");
    printf("LANG = %s\n", env_p);
 
    env_p = getenv ("LC_ALL");
    printf("LC_ALL = %s\n", env_p);
 
    env_p = getenv ("TZ");
    printf("TZ = %s\n", env_p);
 
    return 0;
}

Output:

PATH = /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
HOME = /home/sandbox
PWD = /tmp/1398126808-1302695176
LD_LIBRARY_PATH = /usr/local/lib
LANG = en_US.UTF-8
LC_ALL = C
TZ = (null)

[edit] See also