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 getenv():


	#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. Both comments and pings are currently closed. Tweet This! Tweet This! or use to share this post with others.

There are currently 4 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.

  3. Mark L. on January 4th, 2013:

    The title of the code example says:
    “Source code example of abort():”

    This is a copy/paste problem. It should say “Source code example of getenv():”

    this page is about getenv(), not abort()

  4. admin on January 6th, 2013:

    @Mark L. : thx, i’ve changed the typo from abort() to getenv()