how to export integer value to the environment variable in c -
i have tried below program export value environment variable. want export integer value environment variable. below program taking value "a" instead of 1. how export integer value environment variable.
#include<stdio.h> void chnge_env_var(int a) { char *name1="env_var"; char *val=null; int status; status = putenv("env_var=a"); printf("status %d\n",status); val = getenv(name1); printf("val %s\n",val); } int main() { int a=1; chnge_env_var(a); return 0; }
the environment can hold string values. store integer have convert string , store that. when reading can convert string integer.
int = 10; char env_var[20]; // length of 'env_var=' plus 12 sprintf(env_var, "env_var=%d", a); putenv(env_var);
as 'code clown' pointed out, snprintf
might used instead if aren't absolutely sure buffer of right size:
snprintf(env_var, 20, "env_var=%d", a);
Comments
Post a Comment