C language putenv of function and getenv of function of the use of details

  • 2020-04-02 03:20:51
  • OfStack

C putenv() function: changes or increases the environment variable
The header file:


#include4<stdlib.h>

Definition function:


int putenv(const char * string);

Putenv () is used to change or add the contents of the environment variable.

Return value: 0 for success or -1 for error.

Error code: ENOMEM is out of memory to configure the new environment variable space.

sample


#include <stdlib.h>
main()
{
  char *p;
  if((p = getenv("USER")))
  printf("USER =%sn", p);
  putenv("USER=test");
  printf("USER+5sn", getenv("USER"));
}

Perform:


USER=root
USER=root

C getenv() function: gets the contents of the environment variable
The header file:


#include <stdlib.h>

Definition function:


char * getenv(const char *name);

Getenv () is used to get the content of the parameter name environment variable.

Return value: a successful execution returns a pointer to the content, and an environment variable name that cannot be found returns NULL.

sample


#include <stdlib.h>
main()
{
  char *p;
  if((p = getenv("USER")))
  printf("USER = %sn", p);
}

Perform:


USER = root


Related articles: