为了在 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/