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

后端 未结 3 1656
逝去的感伤
逝去的感伤 2020-12-17 22:57

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:28

    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;
    }
    

提交回复
热议问题