Easy Tutorial
❮ C Function Tanh C Function Exit ❯

C Library Function - getenv()

C Standard Library - <stdlib.h>

Description

The C library function char *getenv(const char *name) searches for the environment string pointed to by name and returns the associated value to the string.

Declaration

Here is the declaration for the getenv() function.

char *getenv(const char *name)

Parameters

Return Value

The function returns a null-terminated string that is the value of the requested environment variable. If the environment variable does not exist, it returns NULL.

Example

The following example demonstrates the use of the getenv() function.

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

int main ()
{
   printf("PATH : %s\n", getenv("PATH"));
   printf("HOME : %s\n", getenv("HOME"));
   printf("ROOT : %s\n", getenv("ROOT"));

   return(0);
}

Let's compile and run the above program, which will produce the following result:

PATH : /sbin:/usr/sbin:/bin:/usr/bin:/usr/local/bin
HOME : /
ROOT : (null)

C Standard Library - <stdlib.h>

❮ C Function Tanh C Function Exit ❯