Jacob Jacob - 1 month ago 12
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

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

Answer

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);    
  }
  return(0);
}