C Library Functions -getenv()

C Standard Library - <stdlib.h>

Description

C Library Functionschar *getenv(const char *name)Search the environment string pointed to by name, and return the associated value to the string.

Declaration

Below is the declaration of the getenv() function.

char *getenv(const char *name)

Parameter

  • name-- A C string containing the name of the requested variable.

Return Value

This 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 usage 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 us 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>

other extensions