getenv : <stdlib.h> library function

The function char *getenv(const char *name); searches for the environment variable whose name is specified by pointer argument name and returns a C string containing the value of the requested environment variable. The getenv function returns a null pointer, requested environment variable does not exist.

Function prototype of getenv

char *getenv(const char *name);
  • name : This is a pointer to a C string containing the name of the requested environment variable.

Return value of getenv

This function returns a null terminated string with the value of the requested environment variable, If it exists otherwise a null pointer.

C program to show the use of getenv function

The following program shows the use of getenv function to read environment variable.

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

int main ()
{
   char *path = getenv("PATH");
   if(path!= NULL){
       printf("PATH : %s", path);
   }
   return(0);
}

Program Output
PATH : /sbin:/usr/sbin:/bin:/usr/bin:/usr/local/bin