函數名稱: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.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE ; .WSF;.WSH;.MSCPROCESSOR_ARCHITECTURE=x86PROCESSOR_ARCHITEW6432=AMD64PROCESSOR_IDENTIFIER=Intel64Family6Model76Stepping3,GenuineIntelPROCESSO R_LEVEL=6PROCESSOR_REVISION=4c03ProgramData=C:ProgramDataProgramFiles=C:ProgramFiles(x86)ProgramFiles(x86)=C:ProgramFiles(x86)ProgramW64 32=C:ProgramFilesPSModulePath=C:ProgramFilesWindowsPowerShellModules;C:Windowssystem32WindowsPowerShellv1.0ModulesPUBLIC=C:UsersP ublicSESSIONNAME=ConsoleSystemDrive=C:SystemRoot=C:WindowsTEMP=C:UsersADMINI~1AppDataLocalTempTMP=C:UsersADMINI~1AppDataLocalTemp USERDOMAIN=PC-20200622MGXHUSERDOMAIN_ROAMINGPROFILE=PC-20200622MGXHUSERNAME=AdministratorUSERPROFILE=C:UsersAdministratorwindir=C:Windows