92

How do I get the list of all environment variables in C and/or C++?

I know that getenv can be used to read an environment variable, but how do I list them all?

1
  • how about calling the env using system(env)?
    – Vijay
    Commented Jan 18, 2010 at 11:22

11 Answers 11

138

The environment variables are made available to main() as the envp argument - a null terminated array of strings:

#include <stdio.h>

int main(int argc, char **argv, char **envp)
{
  for (char **env = envp; *env != 0; env++)
  {
    char *thisEnv = *env;
    printf("%s\n", thisEnv);    
  }
  return 0;
}
2
  • 17
    If you work in windows, and if you have a compiler, why don't you just run it and see? Let us know how it goes.
    – Alex Brown
    Commented Jan 12, 2016 at 23:20
  • 8
    While widely supported (I do not know of any compiler that does not support it), use of the "envp" argument to main is not guaranteed, only "argc" and "argv" are. "envp" also makes it local to main(). A more portable solution is to look at the "environ" extern (global) that just about every compiler provides, as well as the getenv() and setenv() functions (which are supposed to ensure the integrity of manipulating the process' environment). Many implementation also provide a putenv() function, but this function may not ensure the environ integrity. (See the docs, running out of comment space.)
    – C. M.
    Commented Dec 4, 2017 at 20:49
82
#include <stdio.h>

extern char **environ;

int main() {
  char **s = environ;

  for (; *s; s++) {
    printf("%s\n", *s);
  }

  return 0;
}
6
  • 1
    There is a bug in your example. The first environment var is printed twice.
    – user184968
    Commented Jun 10, 2014 at 11:55
  • 1
    You are 100% correct. I've updated initial value of i to 1 from 0. Commented Jul 3, 2014 at 16:03
  • 6
    Advantage of this solution is that it doesn't need to be in main(). It works fine if you stick it it the middle of a large and complicated program. Thanks!! Commented May 5, 2015 at 1:18
  • 3
    This is a posix-specific API. On the other hand, getenv is a C standard library function, which means it works for all C compliant OSes.
    – andrewrk
    Commented Jan 12, 2016 at 5:43
  • 5
    @andrewrk getenv() doesn't allow to iterate over all environment variables, AFAIK.
    – sstn
    Commented Oct 19, 2017 at 9:23
17

I think you should check environ. Use "man environ".

0
10

Your compiler may provide non-standard extensions to the main function that provides additional environment variable information. The MS compiler and most flavours of Unix have this version of main:

int main (int argc, char **argv, char **envp)

where the third parameter is the environment variable information - use a debugger to see what the format is - probably a null terminated list of string pointers.

8
LPTCH WINAPI GetEnvironmentStrings(void);

http://msdn.microsoft.com/en-us/library/ms683187%28VS.85%29.aspx

EDIT: only works on windows.

6
int main(int argc, char **argv, char** env) {
   while (*env)
      printf("%s\n", *env++);
   return 0;
}
0
5
int main(int argc, char* argv[], char* envp[]) {
   // loop through envp to get all environments as "NAME=val" until you hit NULL.
}
4

In most environments you can declare your main as:

main(int argc,char* argv[], char** envp)

envp contains all environment strings.

3

If you're running on a Windows operating system then you can also call GetEnvironmentStrings() which returns a block of null terminated strings.

3

More or less portable C code solution seems for me as follows:

#include <stdlib.h>
void printenv() {
    char ** env;
#if defined(WIN) && (_MSC_VER >= 1900)
    env = *__p__environ();
#else
    extern char ** environ;
    env = environ;
#endif
    for (env; *env; ++env) {
        printf("%s\n", *env);
    }
}

Explanations:

  1. Tested successfully on Linux, Windows, Solaris, AIX.

  2. Tested successfully on new versions of Visual Studio as well. The point is that since at least VS 2017 (probably earlier) the environ global variable is not recognized anymore. If you open the header C:\Program Files\Windows Kits\10\Include\x.x.x.x\ucrt\stdlib.h you will see that this global variable was replaced with the function __p__environ(). Unfortunately it is not documented well. No word about it in https://learn.microsoft.com/en-us/cpp/c-runtime-library/environ-wenviron?view=msvc-170.

  3. The advantage of this approach is that it is also appropriate if you are not allowed to modify the main() function adding there envp[].

  4. Regarding GetEnvironmentStrings(), it returns me an empty list. Probably it works for C++ and not for C. I did not investigate it.

2
  • 2
    I have tested your code on a mac successfully. OS-X Version "macOS 12.4 21F79 arm64" and clang version 13.0.1. I just had two warnings. The fist asks to add "#include <stdio.h> and the other complains about expression result not being used in for (env; *env; ++env) {. Just remove the first env, so you get for (; *env; ++env) { Commented May 31, 2022 at 14:47
  • In C++, had to move the extern declaration outside the anonymous namespace my code was nested into, otherwise it looks for environ in that anon-ns, not the global ns. Same for-loop warning as above. And changed WIN to _WIN32.
    – ddevienne
    Commented Apr 17 at 6:48
1

Most of the answers here point out the possibility to pick the environment from an argument to main supported by most compilers. While Alex' answer:

#include <stdio.h>
extern char **environ;

int main() {
  char **s = environ;
  for (; *s; s++) {
      printf("%s\n", *s);
  }
  return 0;
}

should work always, I wonder what happens to char **environ when you manipulate the environment in your C code (putenv, unsetenv). Then environ may point to somewhere else (when it was reallocated, may depend on the system implementation). If we stick to a parameter passed to main and pass it on to the function requiring it, this pointer may not point to the current environment any more.

2
  • in what manner does this answer differ from that presented by @user1602017 ?
    – DrBeco
    Commented Aug 2, 2021 at 18:07
  • There is nothing wrong in the answer presented by @user160217, I intented to point out the difference between his and the top answer, possibly by reallocation.I would have used a comment instead if I had enough points to do that. I assumed additional value in that. Fetching from **environ fresh should make sure to get recent values - depending on the operating system.
    – toaster
    Commented Aug 3, 2021 at 21:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.