getenv() c – getenv C Library Function

getenv() c: The function getenv 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

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

C program using getenv function

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

getenv C Library Function

#include <stdio.h>
#include <stdlib.h>
 
int main ()
{
   char *path = getenv("PATH");
   if(path!= NULL){
       printf("PATH : %s", path);
   }
   return(0);
}

Output

PATH : /sbin:/usr/sbin:/bin:/usr/bin:/usr/local/bin