Translate

Thursday, June 13, 2013

Environment Variables


.: Printing the environment variables :.

Avoid modifying the environment directly. Prefer using functions like setenv and unsetenv.

Environment variables are accessed through the array of char pointers that represents the list of variables and their values​​.

The sample code bellow prints the environment variables and their values


 1 /* 
 2  * A demonstration on how to print the execution environment 
 3  * Never modify environ directly! Use setenv and unsetenv instead.
 4  */
 5 
 6 #include <stdio.h>
 7 #include <stdlib.h>
 8 
 9 /* The variable 'environ' keeps the environment. 
10  * An array of pointers to the variables list.
11  */
12 extern char **environ;
13 
14 int main() {
15  
16  char **var;
17  for (var = environ; *var != NULL; ++var)
18   printf("%s\n", *var);
19 
20  return 0;
21 }

No comments:

Post a Comment