The C++ <cstdlib> getenv function returns a pointer to the environmental information associated with the string passed as the argument.The declaration of the function is given below.
|char *getenv(const char *name);|
name -The name of the environmental variable.
char* -The value of the environmental variable (if you can’t understand what this mean,you will understand it later)
For WINDOWS OS fan
To understand what ‘getenv’ function actually does.Go to
i)”This PC” and right click anywhere and click’ on ‘Properties‘.
ii)At the left column you will see “Advanced system settings“,click on it ,a new window will open up and click the “Environment variables…” below.
iii)After clicking the “Environment Variables…” ,a new window will open up.In the “System variables” panel do you see two column? One has the name ‘Variable‘ and the second columns has the name ‘Value‘ .
In the ‘Variable‘ column you will see list of of names .Now if you pass one of these names as an argument to the ‘getenv‘,it will return the value found in the same row at the ‘Value‘ column.For instance in my computer there is a name ‘PROCESSOR_ARCHITECTURE’ in the ‘Variable’ column,passing this name to ‘getenv’ returns ‘AMD64’.
char c*=getenv( “PROCESSOR_ARCHITECTURE” );
cout<< c ;
Try passing one of the names found in the ‘Variable’ column in your computer.
For LINUX OS fan
Open the terminal and given the command ‘printenv‘and press ‘Enter‘.Now you will see a lists of ‘environmental variables’ and their ‘value’.
If you give any of the variable name as the argument to the ‘getenv‘,it will return the name on the right side of the ‘=’ sign.For instance a code example is given below.
Code example :I ran this code in Ubuntu
cout<< c << endl ;
cout<< getenv( “XDG_RUNTIME_DIR” ) ;
Test in your OS by passing any of the environmental variable name.
Some points to note about ‘getenv’ function:
i) The implementation shall behave as if no library function calls the ‘getenv’ function.
ii) Calls to the function ‘getenv’ shall not introduce a data race provided that nothing modifies the environment.