C语言 如何在C程序中使用环境变量

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31906192/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 12:03:48  来源:igfitidea点击:

How to use environment variable in a C program

cvariablespathenvironment

提问by Alex

I need to know a way for use environment variables in the C programming language. How can I use and read them?

我需要知道一种在 C 编程语言中使用环境变量的方法。我如何使用和阅读它们?

For example, read an environment variable or take the value of an environment variable and load it in another variable.

例如,读取环境变量或获取环境变量的值并将其加载到另一个变量中。

回答by ameyCU

You can use following functions -

您可以使用以下功能 -

char * getenv (const char *name)-returns a string that is the value of the environment variable name.

char * getenv (const char *name)- 返回一个字符串,它是环境变量名称的值。

char * secure_getenv (const char *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

在此处阅读更多功能 - http://www.gnu.org/software/libc/manual/html_node/Environment-Access.html#Environment-Access

回答by user3334059

Use the getenvfunction. That's it!

使用getenv函数。就是这样!

回答by Deepesh

#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

这就是你如何获得这些变量