w3resource

C Exercises: Get the environment string


9. Retrieve Environment String

Write a C program to get the environment string.

Sample Solution:

C Code:

#include<stdio.h>      // Include the standard input/output header file.
#include<stdlib.h>     // Include the standard library header file.

int main ()          // Start of the main function.
{
char * PathPtr;   // Declare a pointer to char 'PathPtr'.

PathPtr = getenv ("PATH");   // Get the value of the environment variable "PATH" and assign it to 'PathPtr'.

if (PathPtr!=NULL)   // Check if 'PathPtr' is not NULL (i.e., if the environment variable was found).
printf ("\nThe set path is: %s\n\n",PathPtr);   // Print the value of the environment variable "PATH".

return 0;   // Return 0 to indicate successful execution of the program.
}   // End of the main function.

Sample Output:

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

Flowchart:

C Exercises Flowchart: Get the environment string

For more Practice: Solve these Related Problems:

  • Write a C program to retrieve and display the PATH environment variable using getenv().
  • Write a C program to iterate over all environment variables and print those containing a specific keyword.
  • Write a C program to safely retrieve an environment variable and handle the case when it is not set.
  • Write a C program to parse the environment string into individual key-value pairs and display them.

Go to:


PREV : Absolute Value of a Long Integer.
NEXT : Division Quotient and Remainder.

C Programming Code Editor:



Improve this sample solution and post your code through Disqus

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.