setenv function in c

What is setenv() in c?

setenv function in c creates or sets environment variable.

It is defined in stdlib.h header file.

Prototype

int setenv(const char*name,const char* value,int overwrite);

Return value

It returns 0 on success otherwise non-zero value on error.


Example Program for setenv()

// C program to illustrate setenv function
#include<stdio.h>
#include<stdlib.h>
int main()
{
 char *pth;
 if(setenv("INCLUDE","usr/nto//include:/home/scholar/include",1)==0)
   {
     if((pth=getenv("INCLUDE"))!=NULL)
      printf("\n\n\tINCLUDE:%s\n\n\t",pth);
   }
  else
  {
  	printf("\n\n\tError! No such Environment Value Exists.");
  }
  return 0;
}      

Output

	INCLUDE:usr/nto//include:/home/scholar/include
setenv function in c output
setenv function in c output

Code Explanation

Here the setenv() sets the environment variable.


People Also Ask for

What is setenv() in c?

It creates or sets environment variable. It is defined in stdlib.h header file.