且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在没有setenv()的情况下在ISO c99标准C中设置环境变量?

更新时间:2023-09-12 17:45:22

getenv 因此是C90标准的一部分,该标准已包含在C99,但 setenv 仅符合IEEE标准,因此不必严格包含在标准C99中。此外,相应的IEEE是2001年的。

getenv is thus part of the C90 standard which is included in C99 but setenv is only conform to an IEEE standard so it hasn't to be included in strictly standard C99. Moreover, the corresponding IEEE is from 2001.

这意味着C99中没有任何标准的方法,您必须使用特定于平台的代码来设置

This means there isn't any standard way of doing this in C99, you have to use platform-specific code to set the env.

在freebsd上手册页


getenv()函数符合ISO / IEC 9899:1990( ISO C90'')。
setenv(),putenv()和unsetenv()函数符合IEEE Std
1003.1-2001(
POSIX.1'')。

The getenv() function conforms to ISO/IEC 9899:1990 (ISO C90''). The setenv(), putenv() and unsetenv() functions conforms to IEEE Std 1003.1-2001 (POSIX.1'').






setenv putenv 等应该在任何POSIX系统上定义。


setenv, putenv and so on should be defined on any POSIX system.

如果您使用的是Windows,请参阅其他问题,其中讨论了 _putenv_s

If you're using windows, see this other question which talks about _putenv_s.