Print the Environment Variables Using Environ

How to print the environment variables in a C program using "environ".

extern char **environ

2 Answers

#include <unistd.h>
#include <stdio.h>

extern char **environ;
//...

int i = 0;
while(environ[i]) {
  printf("%s\n", environ[i++]); // prints in form of "variable=value"
}
3

Do you mean

int main(int argc, char **argv, char **envp)
{
    while(*envp!=null) {
        printf("%s\n", *envp);
        envp++;
    }
    return 0;
}
2

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Robert Thorne

Robert Thorne

Automotive & Future Transportation Editor

Robert Thorne covers electric vehicle innovations, autonomous driving systems, global mobility trends, and automotive engineering developments.

Share this article
Twitter Facebook Pinterest