-2
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;
}
Weather Vane
  • 33,872
  • 7
  • 36
  • 56
Dr.Nati M
  • 7
  • 3

1 Answers1

0

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;
}
Allan Wind
  • 23,068
  • 5
  • 28
  • 38
  • yhaa that i did that but i want to insert the var name manually and printout that specific value for the name I inserted how should I proceed – Dr.Nati M May 12 '22 at 05:37
  • That is an entirely different question. If you have the name, say, "PATH", then you call getenv("PATH"). If you don't have the name, then you need to extract it from `env[i]` for example `char *end = index(env[i], '='); if(!end) { ...}; char *name = strndup(env[i], end - env[i] + 1); name[end - env[i]] = '\0'; getenv(name); free(name);` – Allan Wind May 12 '22 at 07:00