w3resource

C getenv() function

C getenv() function - Get value of an environment variable

Syntax getenv() function

char *getenv(const char *varname)

The getenv() function is used to search the list of environment variables for an entry corresponding to varname.

Parameters getenv() function

Name Description Required /Optional
varname Environment variable name. Required

Return value from getenv()

  • This function returns a null-terminated string with the value of the requested environment variable, or NULL if that environment variable does not exist.
  • Null pointers are returned if the specified name cannot be found in the environment of the calling process.

Example: getenv() function

The following example shows the file-system directory that corresponds to the user's Startup program group in windows environment.

#include  <stdio.h>
#include  <stdlib.h>
 
/* Where the environment variable 'PATH' is set to a value. */
 
int main(void)
{
   char *pathvar; 
   pathvar = getenv("PATH");
   printf("pathvar=%s",pathvar);
}

Output:

pathvar=H:\Dev-Cpp\MinGW64\bin;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\Windows\System32\OpenSSH\;C:\Program Files (x86)\NVIDIA Corporation\PhysX\Common;C:\Program Files\NVIDIA Corporation\NVIDIA NvDLISR;C:\Program Files (x86)\Intel\Intel(R) Management Engine Components\DAL;C:\Program Files\Intel\Intel(R) Management Engine Components\DAL;"C:\Program Files\Java\jdk1.7.0_02\bin;";H:\bin;C:\Program Files\dotnet\;C:\Program Files\Graphviz\bin;C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\WINDOWS\System32\WindowsPowerShell\v1.0\;C:\WINDOWS\System32\OpenSSH\;C:\Program Files (x86)\dotnet\;C:\Program Files\PuTTY\;C:\Program Files\nodejs\;C:\Program Files\Git\cmd;C:\Users\ME\AppData\Local\Programs\Python\Python311\Scripts\;C:\Users\ME\AppData\Local\Programs\Python\Python311\;C:\Users\ME\AppData\Local\Microsoft\WindowsApps;C:\Users\ME\.dotnet\tools;C:\Users\ME\AppData\Roaming\npm

C Programming Code Editor:

Previous C Programming: C exit()
Next C Programming: C bsearch()



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://www.w3resource.com/c-programming/stdlib/c-getenv.php