getenv() function allows you to find the value of a specific environment variable, but does not provide a mechanism for scanning the entire list of environment variables. The envp argument allows you to envp over the entire list of environment variables, as your demo code shows, which is simply not possible using the getenv() interface.
On POSIX systems, there is a global variable extern char **environ; , which also indicates the environment. The putenv() functions (the old one, not preferable, because it presents memory management problems), setenv() and unsetenv() can also manage the list of environment variables (as defined by environ ). A program can directly modify environ or the values ββit points to, but this is not recommended.
If you use fork() and the exec*() family of functions, if you do not use execve() and specify the environment explicitly, the child process will receive the environment defined by environ .
The header does not declare environ - AFAIK, it is the only variable defined by POSIX without a header to declare it. The C standard recognizes the signature int main(int argc, char **argv, char **envp) for main() as the general extension of the standard documented in Appendix J.
source share