Latest web development tutorials

C library functions - getenv ()

C standard library - <stdlib.h> C standard library - <stdlib.h>

description

C library functionschar * getenv (const char * name ) Search name pointed to environment strings, and returns the associated value to the string.

statement

Here is () statement getenv function.

char *getenv(const char *name)

parameter

  • name - the name of the variable that contains the requested C string.

return value

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

Examples

The following example demonstrates the getenv () function is used.

#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 result in the following:

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

C standard library - <stdlib.h> C standard library - <stdlib.h>