The function getenv () in C

Char * getenv (const char * name) function searches the environment string pointed to by the name parameter and returns the value associated with that string.

Char * getenv (const char * name) function searches the environment string pointed to by the name parameter and returns the value associated with that string.

Declare getenv () function in C

Here is the declaration for getenv () in C:

 char * getenv ( const char * name ) 

Parameters

name : This is the string containing the name of the requested variable.

Returns the value

This function returns a string (terminated by null) with the value of the required environment variable, or returns NULL if the environment variable does not exist.

For example

The following C program illustrates the use of getenv () in C:

 #include #include int main () { printf ( "PATH : %sn" , getenv ( "PATH" )); printf ( "HOME : %sn" , getenv ( "HOME" )); printf ( "ROOT : %sn" , getenv ( "ROOT" )); return ( 0 ); } 

Compiling and running the above C program will result:

Picture 1 of The function getenv () in C

According to Tutorialspoint

Previous lesson: Function exit () in C

Next lesson: Function system () in C

Update 25 May 2019
Category

System

Mac OS X

Hardware

Game

Tech info

Technology

Science

Life

Application

Electric

Program

Mobile