C language putenv () function and getenv () function of the use of detailed _c language

Source: Internet
Author: User

C language Putenv () function: changing or increasing environment variables
header file:

#include4 <stdlib.h>

To define a function:

int putenv (const char * string);

Function Description: Putenv () is used to change or increase the contents of an environment variable. The format of the parameter string is Name=value, and if the environment variable originally existed, the contents of the variable would change depending on the parameter string, otherwise the parameter contents would become the new environment variable.

Return value: Returns 0 for successful execution, or 1 for error occurrence.

Error code: Enomem Not enough memory to configure new environment variable space.

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"));
}

Perform:

User=root
User=root

C language getenv () function: Get the content of environment variables
header file:

#include <stdlib.h>

To define a function:

char * getenv (const char *name);

Function Description: getenv () is used to get the contents of the parameter name environment variable. The parameter name is the name of the environment variable, and a pointer to that content is returned if the variable exists. The format of the environment variable is name=value.

Return value: Successful execution returns a pointer to the content, and NULL if no compatible environment variable name is found.

Example

#include <stdlib.h>
Main ()
{
  char *p;
  if (p = getenv ("user"))
  printf ("user =%s\n", p);

Perform:

USER = root

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.