How to use environment variable in a C program

Alex picture Alex · Aug 9, 2015 · Viewed 27.4k times · Source

I need to know a way for use environment variables in the C programming language. How can I use and read them?

For example, read an environment variable or take the value of an environment variable and load it in another variable.

Answer

ameyCU picture ameyCU · Aug 9, 2015

You can use following functions -

char * getenv (const char *name)-returns a string that is the value of the environment variable name.

char * secure_getenv (const char *name)

Read about some more functions here -http://www.gnu.org/software/libc/manual/html_node/Environment-Access.html#Environment-Access