How to use environment variable in a C program

佐手、 提交于 2020-07-08 05:33:44

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!