Jacob Jacob - 1 year ago 219
C Question

Printing all environment variables in C / C++

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

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

Answer Source

env is available as an argument to main, as envp - a null terminated array of strings:

int main(int argc, char **argv, char** envp)
  char** env;
  for (env = envp; *env != 0; env++)
    char* thisEnv = *env;
    printf("%s\n", thisEnv);    
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download