How to list all environment variables in a c/c++ app

后端 未结 3 1399
独厮守ぢ
独厮守ぢ 2020-12-17 23:00

I know that when programming in c++ I can access individual environment variables with getenv.

I also know that, in the os x terminal, I can list ALL of the current

3条回答
  •  天命终不由人
    2020-12-17 23:20

    Use the environ global variable. It is a null-terminated pointer to an array of strings in the format name=value. Here's a miniature clone of env:

    #include 
    #include 
    
    extern char **environ;
    
    int main(int argc, char **argv) {
        for(char **current = environ; *current; current++) {
            puts(*current);
        }
        return EXIT_SUCCESS;
    }
    

提交回复
热议问题