c语言设置环境变量函数,C语言putenv()函数:用于改变或增加环境变量的内容

函数名:putenv

头文件:

函数原型: void *putenv(char *name);

功能:用于改变或增加环境变量的内容

参数:char *name 为环境变量名

返回值: 成功   返回0 ,失败    返回-1

程序例:使用putenv函数将环境变量拼接为字符串,然后将其替换原来的环境变量#include

#include

#include

int main(void){

char *path,*ptr;

int i=0;

ptr=getenv("PATH");

path=malloc(strlen(ptr)+15);

strcpy(path,"PATH=");

strcat(path,ptr);

strcat(path,"c:\\temp");

putenv(path);

while(environ[i]){

printf("%s\n",environ[i++]);

}

return 0;

}

运行结果HOMEPATH=\Users\Administrator

LOCALAPPDATA=C:\Users\Administrator\AppData\Local

LOGONSERVER=\\PC-20200622MGXH

NUMBER_OF_PROCESSORS=4

OS=Windows_NT

PATH=D:\C-Free\C-Free 5\workspace\;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\Windows\System32\OpenSSH\;C:\Users\Administrator\AppData\Local\Microsoft\WindowsApps;c:\temp

PATHEXT=.COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH;.MSC

PROCESSOR_ARCHITECTURE=x86

PROCESSOR_ARCHITEW6432=AMD64

PROCESSOR_IDENTIFIER=Intel64 Family 6 Model 76 Stepping 3, GenuineIntel

PROCESSOR_LEVEL=6

PROCESSOR_REVISION=4c03

ProgramData=C:\ProgramData

ProgramFiles=C:\Program Files (x86)

ProgramFiles(x86)=C:\Program Files (x86)

ProgramW6432=C:\Program Files

PSModulePath=C:\Program Files\WindowsPowerShell\Modules;C:\Windows\system32\WindowsPowerShell\v1.0\Modules

PUBLIC=C:\Users\Public

SESSIONNAME=Console

SystemDrive=C:

SystemRoot=C:\Windows

TEMP=C:\Users\ADMINI~1\AppData\Local\Temp

TMP=C:\Users\ADMINI~1\AppData\Local\Temp

USERDOMAIN=PC-20200622MGXH

USERDOMAIN_ROAMINGPROFILE=PC-20200622MGXH

USERNAME=Administrator

USERPROFILE=C:\Users\Administrator

windir=C:\Windows

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值