Setenv creates a new environment variable. The return type of this function is an integer. It returns 0 for successful creation, it returns -1 for errors.
envname: It takes the name of the variable that you want to create as an environment variable.
envval: takes the environment variable value that you want to assign to the created value.
overwrite: takes the integer value as argument( 0 or 1 ). A 0 doesn't overwrite an existing variable value, 1 overwrites the value. If the value already exists in the environment, a non-zero value overwrites it.
Here's the code example:-
#include<stdio.h>
#include<unistd.h>
int main()
{
char variable_name[15];
char variable_value[255];
int overwrittenValue;
printf("Enter your variable name");
scanf("%s",variable_name);
printf("Enter the variable value"); scanf("%s",variable_value);
printf("Enter the overridden value");
scanf("%d",overwrittenValue);
int status = setenv(variable_name,variable_value,overwrittenValue);
if(status==0)
printf("Environment variable created successfully");