手机
当前位置:查字典教程网 >编程开发 >C语言 >C语言putenv()函数和getenv()函数的使用详解
C语言putenv()函数和getenv()函数的使用详解
摘要:C语言putenv()函数:改变或增加环境变量头文件:#include4定义函数:intputenv(constchar*string);函...

C语言putenv()函数:改变或增加环境变量

头文件:

#include4<stdlib.h>

定义函数:

int putenv(const char * string);

函数说明:putenv()用来改变或增加环境变量的内容. 参数string 的格式为name=value, 如果该环境变量原先存在, 则变量内容会依参数string 改变, 否则此参数内容会成为新的环境变量.

返回值:执行成功则返回0, 有错误发生则返回-1.

错误代码:ENOMEM 内存不足, 无法配置新的环境变量空间.

范例

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

执行:

USER=root USER=root

C语言getenv()函数:取得环境变量内容

头文件:

#include <stdlib.h>

定义函数:

char * getenv(const char *name);

函数说明:getenv()用来取得参数name 环境变量的内容. 参数name 为环境变量的名称, 如果该变量存在则会返回指向该内容的指针. 环境变量的格式为name=value.

返回值:执行成功则返回指向该内容的指针, 找不到符合的环境变量名称则返回NULL.

范例

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

执行:

USER = root

【C语言putenv()函数和getenv()函数的使用详解】相关文章:

汇编语言rep movsd 的使用详解

memset函数的使用分析

C++中点操作符和箭头操作符的使用详解

C++函数中return语句的使用方法

基于C语言字符串函数的一些使用心得

C语言文件操作函数freopen详细解析

基于C++字符串替换函数的使用详解

C++函数参数取默认值的深入详解

深入理解atoi()与itoa()函数的用法

深入VC回调函数的使用详解

精品推荐
分类导航