C Programming

Setenv C Function

“The “setenv ()” function is used in C programming for adding or updating a variable in the environment which is in the calling process. This “setenv ()” function contains three parameters. The envname, envval, and the overwrite are the three parameters of this “setenv ()” function in C. When we want to update or add some new variable in the environment, then, we utilize this “setenv ()” function. It is not mandatory for setenv () to be re-entrant. A function doesn’t need to be thread-safe if re-entrant behavior is not necessary.

In this guide, we will explore this concept in deep detail and will provide codes where we will utilize the “setenv ()” function in C programming. We will also discuss the syntax of this “setenv ()” function here in detail in this guide and will show you how to pass the parameters to this “setenv ()” function in C programming.”

Syntax

The syntax of this “setenv ()” function is here below: Look at this, and you will easily understand the parameters of this “setenv ()” function in C code.

int setenv (const char *envname, const char *envval, int overwrite);

 
In this “setenv ()” function, we have three parameters, as you have seen above in the syntax of this function. The first parameter is an environment variable’s name that must be added or changed as indicated by the envname parameter, which points to a string. The value toward which envval refers must be used as the environment variable value. If envname refers to a string that contains the character “=,” the “setenv ()” must fail. The “setenv ()” must return success and change the environment if the name envname of the environment variable exists here and the value of overwrite is not equal to zero.

The function must restore success, and the environment must not change in case the name envname of the environment variable exists here and the relevant value of overwrite is zero.

Example # 1

Ubuntu 20.04 will be used for the examples demonstrated in this guide. We first installed Ubuntu 20.04 and entered some commands for installing the GCC compiler. We must install this GCC compiler so that we will do this code in C programming in Ubuntu 20.04. When this compiler is installed in Ubuntu 20.04, then we open the text editor for coding on it and start typing some code lines in which we will utilize the “setenv ()” function in C programming. We use several header files in this text editor at the beginning of the C code. When writing C code, all we need to do is add the header files; otherwise, we won’t get the functions we require in our C code. You can see that we placed the standard input and output header file, “stdio.h”, on the first line of the code in this example.

Then we place another header file which is “stdlib,” here it is used for including different functions, and it is the standard library in C. After this, we create or declare an “extern char” with the name “environ,” and below this, we are using the “int main ()” function. We are passing three parameters to this “main ()” function. The first parameter is “int argc”, the second parameter is “const char *argv []”, and the last parameter of this “main ()” function is “const char *envp []”. Then we utilize the “printf” function; we use this function when we want to render some statements on the screen. We utilize two different “printf” here and put “%p” inside the “printf” statements. This “%p” is used for displaying the pointer data type in C codes.

Below this, we are utilizing the “setenv ()” function. We pass three parameters here. The first parameter is used for indicating an environment variable’s name that must be added or changed. The value toward which the second parameter implies must be utilized as the environment variable value. And the third parameter here is non-zero, so it means that the “setenv ()” returns success, and the value of the environment variable will be changed. Then we have “puts ()” here, which we utilize for rendering the line on the terminal. The line which we write in this “puts” will render on the output terminal.

After this, we again utilize two “printf” statements below this “puts”. And again, use the “environ” and “envp” in separate “printf” statements and again utilize “%p” here. Both statements will display on the screen. Then we have the “exit ()” statement and pass “EXIT-SUCCESS” here as the parameter of this “exit ()”. Here the code is completed, and now we just need to save this file, and we must put the “.c” file extension with the name of the file.


Now, open the terminal of Ubuntu 20.04. We put the first GCC command after launching the terminal, which aids in compiling this c code. We use the other command, which is used to run C code after this code has been executed. These commands are also indicated in the picture below. It displays the outcome of the code which we have created above. Here, notice that the value of “environ” has changed, but the “envp” pointer still has the identical value after the call.

Conclusion

This guide shows how to use the C programming function “setenv ()”. Here, we have given a full description of the “setenv ()” function and examples of how to utilize it in our C programming codes. We have also provided the output here to make it easier for you to understand how to utilize the C “setenv ()” function and how it returns the output. As previously mentioned, this “setenv ()” function is used for adding or updating a variable in the environment, which is in the calling process in C programming, and we also discussed that we must pass three parameters for this “setenv ()” function. We expect that this guide should assist you in gaining a deeper grasp of C programming functions.

About the author

Omar Farooq

Hello Readers, I am Omar and I have been writing technical articles from last decade. You can check out my writing pieces.