To print the environment from the stdin (var name) & execute the getenv() function it compiles correctly but no output what am i missing?

104 views Asked by At
int main(int ac, char **av, char **env) 
{
    unsigned int i = 1;
    while(getenv("env[i]") != NULL)
    {
        printf("%s\n", env[i]);
        i++;
    }
    return 0;
}
1

There are 1 answers

2
Allan Wind On

As discussed above, arrays start at index 0 and the loop condition is wrong ("env[i]" is a fixed string, also getenv() takes a name but env[i] is a name=value pair):

#include <stdio.h>

int main(int ac, char **av, char **env) {
    for(unsigned i = 0; env[i]; i++) {
        printf("%s\n", env[i]);
    }
    return 0;
}