函数名:putenv
头文件:<stdlib.h>
函数原型: void *putenv(char *name);
功能:用于改变或增加环境变量的内容
参数:char *name 为环境变量名
返回值: 成功 返回0 ,失败 返回-1
程序例:使用putenv函数将环境变量拼接为字符串,然后将其替换原来的环境变量
#include<stdio.h>#include<stdlib.h>#include<string.h>intmain(void){char*path,*ptr;inti=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(%sn,environ[i++]);}return0;}
运行结果
HOMEPATH=UsersAdministratorLOCALAPPDATA=C:UsersAdministratorAppDataLocalLOGONSERVER=\PC-20200622MGXHNUMBER_OF_PROCESSORS=4OS=Windows_NTPATH=D:C-FreeC-Free5workspace;C:Windowssystem32;C:Windows;C:WindowsSystem32Wbem;C:WindowsSystem32WindowsPowerShellv1.0;C:WindowsSystem32OpenSSH;C:UsersAdministratorAppDataLocalMicrosoftWindowsApps;c:tempPATHEXT=.COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH;.MSCPROCESSOR_ARCHITECTURE=x86PROCESSOR_ARCHITEW6432=AMD64PROCESSOR_IDENTIFIER=Intel64Family6Model76Stepping3,GenuineIntelPROCESSOR_LEVEL=6PROCESSOR_REVISION=4c03ProgramData=C:ProgramDataProgramFiles=C:ProgramFiles(x86)ProgramFiles(x86)=C:ProgramFiles(x86)ProgramW6432=C:ProgramFilesPSModulePath=C:ProgramFilesWindowsPowerShellModules;C:Windowssystem32WindowsPowerShellv1.0ModulesPUBLIC=C:UsersPublicSESSIONNAME=ConsoleSystemDrive=C:SystemRoot=C:WindowsTEMP=C:UsersADMINI~1AppDataLocalTempTMP=C:UsersADMINI~1AppDataLocalTempUSERDOMAIN=PC-20200622MGXHUSERDOMAIN_ROAMINGPROFILE=PC-20200622MGXHUSERNAME=AdministratorUSERPROFILE=C:UsersAdministratorwindir=C:Windows