Function name : putenv
Header file : <stdlib.h>
Function prototype : void *putenv(char *name);
Function : Used to change or add the content of environment variables
Parameter : char *name is the environment variable name
Return value : Returns 0 on success, -1 on failure
Program example : Use the putenv function to splice environment variables into strings, and then replace them with the original environment variables
#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(%s n,environ[i++]);}return0;}
Running results
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:UsersAdministratorAppDataLocalMicrosoft WindowsApps;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:Windowssystem32WindowsPowerShell v1.0ModulesPUBLIC=C:UsersPublicSESSIONNAME=ConsoleSystemDrive=C:SystemRoot=C:WindowsTEMP=C:UsersADMINI~1AppDataLocalTempTMP=C:UsersADMINI~1AppData LocalTempUSERDOMAIN=PC-20200622MGXHUSERDOMAIN_ROAMINGPROFILE=PC-20200622MGXHUSERNAME=AdministratorUSERPROFILE=C:UsersAdministratorwindir=C:Windows