Pejman Moghadam / C-programming

C - Enumerating all environment variables

Public domain

Using 'environ' variable

#include <stdio.h> 

/*  The 'environ' variable points to an array of
 *  pointers to strings called the "environment" 
extern char** environ; 

int main() 
  char **var;
  for (var = environ; *var != NULL; var++) 
    printf ("%s\n", *var); 
  return 0; 

Using third argument of the main function

This is not compatible with POSIX.1, so to be portable do not use this

#include <stdio.h>
/* Unix systems, and the GNU system, pass 
 * the initial value of environ as the third 
 * argument to main */
int main (int argc, char *argv[], char *envp[])

  for (; *envp != NULL; envp++) 
    printf ("%s\n",*envp);
  return 0; 

BY: Pejman Moghadam
TAG: c, environ, envp, environment
DATE: 2011-06-02 10:30:06

Pejman Moghadam / C-programming [ TXT ]