Thursday, 24 July 2014

Setting and Getting Environment Variable in C

There are a number of way by which you can access environment variable in C. Most common of them is to declare as


envp contains all environment strings. Here is an example program:

We can also use external variable 'environ' to get all the environment variable.
Here is an example program:

Also,there are function available in stdlib through which we can access any particular environment variable as well as add and modify them. getenv is use to get value of an environment variable whereas setenv is use to add and replace them. setenv also takes an optional parameter which specify whether we want to overwrite any existing variable or not.

Here is a sample program:

The output of above code would be:

No comments:

Post a Comment