函数名: getenv
功 能: 从环境中取字符串,获取环境变量的值
头文件: stdlib.h
用 法:char *getenv(char *envvar);
函数说明:getenv()用来取得参数enwar环境变量的内容。参数enwar为环境变量的名称,如果该变量存在则会返回指向该内容的指针。环境变量的格式为enwar=value
返回值: 执行成功则返回指向该内容的指针,找不到符合的环境变量名称则返回NULL
相关函数:_wgetenv、getenv_s、_wgetenv_s
程序示例
程序例1: #include <stdlib.h>
#include <stdio.h>
int main(void)
{
char *s;
s=getenv("COMSPEC"); /* get the comspec environment parameter */
printf("Command processor: %s/n",s); /* display comspec parameter */
return 0;
}
执行:COMSPEC = name
程序例2:
#include <stdlib.h>
#include <stdio.h>
int main( void )
{
char *libvar;
libvar = getenv( "LIB" ); // C4996
if( libvar != NULL )
printf( "Original LIB variable is: %s/n", libvar );
_putenv( "LIB=c://mylib;c://yourlib" ); // C4996
libvar = getenv( "LIB" ); // C4996
if( libvar != NULL )
printf( "New LIB variable is: %s/n", libvar );
return 0;
}