Pejman Moghadam / C-programming

C - Environment variables access

Public domain


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

int main ()
{
  char *test = getenv ("TEST");
  if (test == NULL)
    printf ("TEST environment variable is not set because getenv returned NULL");
  else
    printf ("TEST environment variable is set and equal to: %s", test);
  return 0;
}

Output

$ ./a.out 
TEST environment variable is not set because getenv returned NULL

$ TEST=1

$ ./a.out 
TEST environment variable is not set because getenv returned NULL

$ echo $TEST 
1

$ export TEST

$ ./a.out 
TEST environment variable is set and equal to: 1

$ unset TEST  

$ ./a.out 
TEST environment variable is not set because getenv returned NULL

$ export TEST=""

$ ./a.out 
TEST environment variable is set and equal to:

BY: Pejman Moghadam
TAG: c, getenv, environment
DATE: 2011-06-02 16:59:40


Pejman Moghadam / C-programming [ TXT ]