How to use environment variable in a C program
43,236
Solution 1
You can use following functions -
char * getenv (const char *name)
-returns a string that is the value of the environment variable name.
char * secure_getenv (const char *name)
Read about some more functions here -http://www.gnu.org/software/libc/manual/html_node/Environment-Access.html#Environment-Access
Solution 2
Use the getenv function from stdlib.h
. That's it!
Solution 3
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[])
{
char* my_env_var = getenv("MY_ENV_VAR");
if(my_env_var)
printf("Var found: %s", my_env_var );
else
printf("Var not found.");
return 0;
}
Author by
Alex
Updated on July 09, 2022Comments
-
Alex almost 2 years
I need to know a way for use environment variables in the C programming language. How can I use and read them?
For example, read an environment variable or take the value of an environment variable and load it in another variable.