c - char *envp[] 是 main() 可移植的第三个参数吗

标签 c environment-variables program-entry-point portability function-signature

为了在 C 程序中获取环境变量,可以使用以下方法:

  • getenv()
  • extern char **environ;

但除了上面提到的以外,是否使用 char *envp[] 作为 main() 的第三个参数来使环境变量被视为标准的一部分?

#include <stdio.h>

int main(int argc, char *argv[], char *envp[])
{
    while(*envp)
        printf("%s\n",*envp++);
}

char *envp[] 可移植吗?

最佳答案

函数getenv 是C 标准指定的唯一函数。函数putenv , 和外部 environ是特定于 POSIX 的。

编辑

main 参数 envp 未由 POSIX 指定,但得到广泛支持。

An alternative method of accessing the environment list is to declare a third argument to the main() function:

int main(int argc, char *argv[], char *envp[])

This argument can then be treated in the same way as environ, with the difference that its scope is local to main(). Although this feature is widely implemented on UNIX systems, its use should be avoided since, in addition to the scope limitation, it is not specified in SUSv3.

关于c - char *envp[] 是 main() 可移植的第三个参数吗,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10321435/

相关文章:

node.js - Azure NODE_PATH环境变量没有任何作用

c - 在 Linux Ubuntu 系统上,main 函数是由 _libc_start_main 函数调用的吗?

c - exit(0) 的目的是什么?

c - 输入流的队列出队入队错误

c++ - vs2010 C4353 为什么这不是错误

c - 深度优先目录遍历导致C中的段错误

linux - 在 AWS 实例中设置环境变量

windows - 通过 Powershell 添加 JAVA_HOME 到系统变量 Path

c - void main() { } 无需头文件即可完美运行。怎么会这样,用C写的main函数的定义在哪里?

c# - 如何让一个类首先在 Winforms 应用程序中运行