ala*_*mar 53
NAME
putenv - change or add an environment variable
SYNOPSIS
#include <stdlib.h>
int putenv(char *string);
DESCRIPTION
The putenv() function adds or changes the value of environment
variables. The argument string is of the form name=value. If name does
not already exist in the environment, then string is added to the
environment. If name does exist, then the value of name in the
environment is changed to value. The string pointed to by string becomes
part of the environment, so altering the string changes the environment.
在Win32上它被称为_putenv我相信.
如果您是长而丑陋的函数名称的粉丝,请参阅SetEnvironmentVariable.
小智 9
还有setenv,它比 稍微灵活一些putenv,它会setenv检查环境变量是否已经设置并且不会覆盖它,如果您设置了“overwrite”参数表明您不想覆盖它,并且因为名称和值是单独的参数setenv:
NAME
setenv - change or add an environment variable
SYNOPSIS
#include <stdlib.h>
int setenv(const char *name, const char *value, int overwrite);
int unsetenv(const char *name);
Feature Test Macro Requirements for glibc (see feature_test_macros(7)):
setenv(), unsetenv():
_POSIX_C_SOURCE >= 200112L
|| /* Glibc versions <= 2.19: */ _BSD_SOURCE
DESCRIPTION
The setenv() function adds the variable name to the environment with
the value value, if name does not already exist. If name does exist
in the environment, then its value is changed to value if overwrite
is nonzero; if overwrite is zero, then the value of name is not
changed (and setenv() returns a success status). This function makes
copies of the strings pointed to by name and value (by contrast with
putenv(3)).
The unsetenv() function deletes the variable name from the
environment. If name does not exist in the environment, then the
function succeeds, and the environment is unchanged.
Run Code Online (Sandbox Code Playgroud)
我并不是说其中一个比另一个更好或更差;我只是说其中一个比另一个更好。这仅取决于您的应用程序。
请参阅http://man7.org/linux/man-pages/man3/setenv.3.html