Set the environment variable putenv () setenv () getenv () (zz) in Linux)
Getenv (get environment variable content)
Putenv, setenv, unsetenv
Header file # include <stdlib. h>
Define the function char * getenv (const char * Name );
Function Description getenv () is used to obtain the content of the parameter name environment variable. The parameter name is an environment variable.
If the variable exists, a pointer to the content is returned. Environment Variable
The format is name = value.
If the returned value is successfully executed, a pointer to the content is returned. if the name of the conforming environment variable is not found
Returns null.
Example # include <stdlib. h>
Mian ()
{
Char * P;
If (P = getenv ("user ")))
Printf ("user = % s/n", P );
}
Run user = root
Putenv (change or add environment variables)
Related functions: getenv, setenv, and unsetenv
Header file # include4 <stdlib. h>
Defines the int putenv (const char * string) function );
Function Description putenv () is used to change or add environment variables. The format of the string parameter is
Name = value. If the environment variable originally exists, the variable content will depend on the parameter
String is changed. Otherwise, the parameter content will become a new environment variable.
If the return value is successfully executed, 0 is returned. If an error occurs,-1 is returned.
The error code enomem memory is insufficient and a new environment variable space cannot be configured.
Example # include <stdlib. h>
Main ()
{
Char * P;
If (P = getenv ("user ")))
Printf ("user = % s/n", P );
Putenv ("user = test ");
Printf ("User + 5s/N", getenv ("user "));
}
Run user = root
User = root
Setenv (change or add environment variables)
Related functions: getenv, putenv, and unsetenv
Header file # include <stdlib. h>
Defines the int setenv (const char * Name, const char * value, int
Overwrite );
Function Description: setenv () is used to change or add environment variables. The parameter name is the environment variable name.
It is called a string.
The value parameter is the variable content, and overwrite is used to determine whether to change the existing
Environment variable. If the value of overwrite is not 0 and the environment variable already exists
The content is changed to the variable content specified by the parameter value. If overwrite is 0
If the environment variable already exists, the parameter value is ignored.
If the return value is successfully executed, 0 is returned. If an error occurs,-1 is returned.
Error code: insufficient enomem memory, unable to configure new environment variable space
Example # include <stdlib. h>
Main ()
{
Char * P;
If (P = getenv ("user ")))
Printf ("user = % s/n", P );
Setenv ("user", "test", 1 );
Printf ("user = % s/n", getenv ("user "));
Unsetenv ("user ");
Printf ("user = % s/n", getenv ("user "));
}
Run user = root
User = test
User = (null)