Linux C language environment variable ENV/getenv get

Source: Internet
Author: User

Obtain the Linux C language environment variable ENV/getenv, and obtain the C language usage method!

# Include <stdio. h>
Int main (INT argc, char ** argv, char ** env)
{
Extern environ;
Printf ("environ = % P \ n", Environ );
Printf ("Env = % P \ n", ENV );

}


# Include <stdio. h>
# Include <string. h>
Int main ()
{
Extern char ** environ;
Printf ("environ = % P \ n", Environ );
Char ** Env = environ;
While (* env! = NULL ){
Printf ("% s \ n", * env ++ );
}
/* Want To Know The Lang environment variable value */
Char * value = NULL; // This variable stores the value
ENV = environ;
While (* env! = NULL ){
Char * STR = * env ++;
If (strncmp (STR, "Lang", strlen ("Lang") = 0) {// lang = zh_cn
Value = STR + strlen ("Lang") + 1;
Break;
}
}
Printf ("lang = % s \ n", value );
}

# Include <stdio. h>
# Include <stdlib. h>

Int main ()
{
Char * value = getenv ("Lang ");
Printf ("value = % s \ n", value );
Putenv ("Var = ABC"); // Add an environment variable
Putenv ("lang = C"); // modify the environment variable
Printf ("Var = % s \ n", getenv ("Var "));
Printf ("lang = % s \ n", getenv ("Lang "));
/* If the 3rd parameters are non-zero, replace them if the Lang environment variable already exists.
If it is zero, if the environment variable already exists, do not replace
*/
Setenv ("Lang", "C", 1 );

}

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.