Standards / Extensions | C or C++ | Dependencies |
---|---|---|
ISO C |
both |
#include <stdlib.h>
char *getenv(const char *varname);
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.
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.
⁄* 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);
}