Reputation: 6383
If getenv is supposed to return a pointer to the value in the environment shouldn't this program work for printing out the string value of the environment variable?
#include <stdio.h>
#include <unistd.h>
int main() {
printf("HOME=%s\n",getenv("HOME"));
return 0;
}
I am getting the following warning when I compile:
format ‘%s’ expects argument of type ‘char *’, but argument 2 has type ‘int’
And when I run the program I get a Segmentation Fault.
I am using Ubuntu and I am wondering if this has to do with permissions or another form of security.
Upvotes: 0
Views: 662
Reputation: 340218
You need #include <stdlib.h>
since that's where getenv()
is declared.
Also, consider using the -Wall
option to enable more diagnostics from gcc or MSVC (or most compilers). In this case, gcc would have said:
warning: implicit declaration of function 'getenv' [-Wimplicit-function-declaration]
and MSVC would have said:
warning C4013: 'getenv' undefined; assuming extern returning int
Upvotes: 2