w3resource logo


C Programming Exercises

C Exercises: Get the environment string

Secondary Nav

C Variable Type : Exercise-9 with Solution

Write a C program to get the environment string.

Expected Output :

The set path is: /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin
:/bin:/usr/games:/usr/local/games      

C Code:

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

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

		if (PathPtr!=NULL)
			printf ("\nThe set path is: %s\n\n",PathPtr);

		return 0;
	}

Solution

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

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

		if (PathPtr!=NULL)
			printf ("\nThe set path is: %s\n\n",PathPtr);

		return 0;
	}

Improve this sample solution and post your code through Disqus



Join our Question Answer community to learn and share your programming knowledge.