Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

C Library Function - getenv()

C Library Function - getenv()

C Standard Library - <stdlib.h> C Standard Library -

Description

C library function **char getenv(const char name) Search for the environment string pointed to by name and return the associated value as a string.

Declaration

Below is the declaration of the getenv() function.



char *getenv(const char *name)

Parameters

Return Value

This function returns a null-terminated string containing 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 

#include 



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 Standard Library -