C Reference function getenv()

The function getenv() of the stdlib.h will get the environment string.

Usage of getenv():

char * getenv ( const char * name );

The function will retrieve a C string that is containing a value of the environment variable, which is specified with the name argument.

Parameters:

The name of the variable that you want to get.

Return value:


If the requested variable is not part of the environment list, the function returns a NULL pointer.

Source code example of abort():


	#include<stdio.h>
	#include<stdlib.h>

	int main ()
	{
		char * ptr_path;
		ptr_path = getenv ("PATH");

		if (ptr_path!=NULL)
			printf ("The set path is: %s",ptr_path);

		return 0;
	}

This entry was posted in C Reference stdlib.h Functions. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site. Tweet This! Tweet This! or use to share this post with others.

There are currently 2 responses to “C Reference function getenv()”

Why not let us know what you think by adding your own comment!

  1. sivaraman on January 12th, 2012:

    Please explain a case in which getenv() is
    successful. here PATH is not found for getenv(). SO it will return NULL.

  2. admin on January 12th, 2012:

    @sivaraman – That is not correct, if null is returned then no environment path is found. If it is not null then an environment path is found. If I compile the sample then the output on a Windows 7 system is:

    The set path is: C:\Program Files (x86)\NVIDIA Corporation\PhysX\Common;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\Program Files (x86)\ImageConverter Plus\Microsoft.VC90.CRT;C:\Program Files (x86)\ImageConverter Plus\Microsoft.VC90.MFC;C:\Program Files (x86)\QuickTime\QTSystem\;C:\Program Files (x86)\ATI Technologies\ATI.ACE\Core-Static

    So the example works. I hope that this helps you.

Leave a Reply: