How to set and get an environment variable in C on Linux?

Posted on In QA

How to set and get an environment variable in C on Linux?

You can use the setenv and getenv POSIX APIs to set and get environment variables.

To add or change environment variable:

#include <stdlib.h> 

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

To get value of an environment variable:

#include <stdlib.h> 

char *getenv(const char *name); 

Leave a Reply

Your email address will not be published. Required fields are marked *