the function getenv() always returning null

c

Solution

Function `getenv` shows a very interesting results if you forget to write `#include <stdlib.h>` into header of your file.

For example, code:

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

int main(void){
  printf("Log file location : %s\n", getenv("LOG_FILE"));
  return 0;
}

works pretty well:

LOG_FILE="log" ./a.out 
Log file location : log

But when I comment out second line, I get:

LOG_FILE="log" ./a.out 
Segmentation fault (core dumped)

Problem

my program ``` int main(void){ printf("Log file location : %s\n", getenv("LOG_FILE")); return 0; } ``` is always printing null. but i already set the env in /etc/nagios/.profile file where my user is "nagios" by export LOG_FILE=/root/log and i am also able to see the same when i am executing the "env" command in terminal.

Original source

Related problems