Printing all environment variables in C / C++

后端 未结 9 1625
时光说笑
时光说笑 2020-11-27 14:23

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 th

相关标签:
9条回答
  • 2020-11-27 14:47
    LPTCH WINAPI GetEnvironmentStrings(void);
    

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

    EDIT: only works on windows.

    0 讨论(0)
  • 2020-11-27 14:56
    #include<stdio.h>
    
    extern char **environ;
    
    int main() {
      int i = 1;
      char *s = *environ;
    
      for (; s; i++) {
        printf("%s\n", s);
        s = *(environ+i);
      }
    
      return 0;
    }
    
    0 讨论(0)
  • 2020-11-27 14:57

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

    0 讨论(0)
提交回复
热议问题