Showing posts with label C. Show all posts
Showing posts with label C. Show all posts

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:

Saturday, 9 November 2013

Always include the header files when using some standard C libraries

Yesterday, I was working on a C code of mine when I  came across a very peculiar problem. I even wrote a test code to regenerate the problem I was facing.

int main()
{
    char str[10]="3.5";
    printf("%lf",atof(str));
    return 0;
}
This is a simple code I am testing at ideone.com. I am getting the output as