问题
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.
回答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
回答2:
Use the getenv function. That's it!
回答3:
#include <stdio.h>
int main(int argc, char **argv, char **envp)
{
char **env;
for(env = envp; *env != 0; env++)
{
char *thisEnv = *env;
printf("%s\n", thisEnv);
}
return 0;
}
thats how you can get those variables
来源:https://stackoverflow.com/questions/31906192/how-to-use-environment-variable-in-a-c-program