为char * envp []作为第三个参数main()的便携式 [英] Is char *envp[] as a third argument to main() portable

查看:188
本文介绍了为char * envp []作为第三个参数main()的便携式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为了获得在 C 程序的环境变量,我们可以使用以下命令:

In order to get an environment variable in a C program, one could use the following:


  • 的getenv()

  • 的extern字符** ENVIRON;

  • getenv()
  • extern char **environ;

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

But other than the above mentioned, is using char *envp[] as a third argument to main() to get the environment variables considered part of the standard?

#include <stdio.h>

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

的char * envp [] 便携式?

推荐答案

功能 GETENV 是由C标准规定的唯一的一个。该功能的<$c$c>putenv,并且将extern <一个href=\"http://pubs.opengroup.org/onlinepubs/9699919799/functions/environ.html\"><$c$c>environ是POSIX特定的。

The function getenv is the only one specified by the C standard. The function putenv, and the extern environ are POSIX-specific.

参数 envp 不是由POSIX指定,但广泛的支持。

The main parameter envp is not specified by POSIX but is widely supported.

访问环境表的另一种方法是声明
  第三个参数的main()函数:

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[])

此论点然后可以以同样的方式作为ENVIRON进行处理,用
  不同之处在于它的范围是本地的main()。尽管此功能是
  在UNIX系统中广泛推行,它的使用应该因为避免,
  除了范围限制,它不是在SUSv3指定

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.

这篇关于为char * envp []作为第三个参数main()的便携式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆