How to use the getenv function to access the list of environment variables in C

2 Answers

0 votes
#include <stdio.h>
#include <stdlib.h>

// Windows
 
int main(void)
{
    char *env_path = getenv("PATH");

    if (env_path)
        printf("PATH = %s\n", env_path);
        
    return 0;
}

/*
run:
 
PATH = C:\ProgramData\Oracle\Java\javapath;C:\Windows\system32;C:\Windows;C:\Win
dows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;c:\Program Files\
Java\jdk1.8.0_91\bin\;C:\Program Files\CodeLite

*/

 



answered Aug 27, 2016 by avibootz
edited Oct 20 by avibootz
0 votes
#include <stdio.h>
#include <stdlib.h>
 
// Linux
  
int main(void)
{
    char* p = getenv("PATH");
  
    if (p)
        printf("PATH = %s\n", p);
}
  
  
  
/*
run:
  
PATH = /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
 
*/

 



answered Oct 20 by avibootz

Related questions

2 answers 271 views
271 views asked May 4, 2021 by avibootz
1 answer 231 views
1 answer 238 views
238 views asked Aug 24, 2020 by avibootz
1 answer 255 views
255 views asked Jan 13, 2017 by avibootz
1 answer 279 views
1 answer 287 views
...