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");


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()

Follow us on Facebook and Twitter for latest update.

C Programming: Tips of the Day

What's the point of const pointers?

const is a tool which you should use in pursuit of a very important C++ concept:

Find bugs at compile-time, rather than run-time, by getting the compiler to enforce what you mean.

Even though it does not change the functionality, adding const generates a compiler error when you're doing things you didn't mean to do. Imagine the following typo:

void foo(int* ptr)
    ptr = 0;// oops, I meant *ptr = 0

If you use int* const, this would generate a compiler error because you're changing the value to ptr. Adding restrictions via syntax is a good thing in general. Just don't take it too far -- the example you gave is a case where most people don't bother using const.

Ref : https://bit.ly/33Cdn3Q