An Interest In:
Web News this Week
- March 19, 2024
- March 18, 2024
- March 17, 2024
- March 16, 2024
- March 15, 2024
- March 14, 2024
- March 13, 2024
Ways to get and set environment variables in C
In this article I will discuss some of most simple ways to access environment variables in C language programming.
Approach 1
In this method we simply used the environ
variable by extending it using extern keyword.
Code
#include <stdio.h>// https://man7.org/linux/man-pages/man7/environ.7.htmlextern char **environ;int main(int argc, char **argv) { // list out all the environment variables for (int i=0; environ[i]!=NULL; i++) { printf("%d: %s
", i, envp[i]); } return 0;}
Checkout environ
man page for more info.
Approach 2
Most of the students don't know that main
function can also receive third argument which is nothing but the array of environment variables.
NOTE: Third argument may not support on all compilers. But it should work on latest compiler versions.
Code
#include <stdio.h>int main(int argc, char **argv, char **envp) { // list out all the environment variables for (int i=0; envp[i]!=NULL; i++) { printf("%d: %s
", i, envp[i]); } return 0;}
Approach 3
In this method we use getenv
and setenv
function defined
getenv
takes one argument which is environment variable name and return a null terminated char pointer. If passed name does not exists, it return NULL
.
setenv
takes three arguments. First one is environment variable name, second is its value and third one is an int flag which if non-zero overwrite the already existing environment variable with same name.
NOTE:
setenv
doesn't set the environment variable permanently but for the current process and its child process only. It meanssetenv
the environment variables are visible only in the current process and its child ones.
Checkout some other function like clearenv
, putenv
in man page for more operations.
Code
#include <stdio.h>#include <stdlib.h> // setenv, getenvint main(int argc, char **argv) { char *user = getenv("USER"), *hosttype = getenv("HOSTTYPE"), *hostname = getenv("HOSTNAME"); printf("USER: %s
", user ? user : "null"); printf("HOSTTYPE: %s
", hosttype ? hosttype : "null"); printf("HOSTNAME: %s
", hostname ? hostname : "null"); setenv("USER", "namantam1", 1); printf("updated USER: %s
", getenv("USER")); return 0;}
Output
USER: namanHOSTTYPE: x86_64HOSTNAME: nullupdated USER: namantam1
Application
- We can set log verbosity based on the environment variable.
- One can set environment variable in parent process and access the same in its child process. Check out this article to know how to clone current process into child process.
Thank you so much for reading this article. I'm passionate engineering student learning new things so If you find any mistake or have any suggestion please let me know in comments.
Also consider sharing and giving a thumbs up If this post help you in any way.
Original Link: https://dev.to/namantam1/ways-to-get-and-set-environment-variables-in-c-200m
Dev To
An online community for sharing and discovering great ideas, having debates, and making friendsMore About this Source Visit Dev To