Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use environment variable in a C program

Tags:

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.

like image 618
Alex Avatar asked Aug 09 '15 16:08

Alex


People also ask

What is an environment variable in C?

Environment variable is a global variable that can affect the way the running process will behave on the system.

What is PATH variable in C?

The PATH environment variable is an important security control. It specifies the directories to be searched to find a command. The default systemwide PATH value is specified in the /etc/profile file, and each user normally has a PATH value in the user's $HOME/. profile file.

How does Getenv work in C?

The getenv() function searches the environment variables at runtime for an entry with the specified name, and returns a pointer to the variable's value. If there is no environment variable with the specified name, getenv() returns a null pointer.


1 Answers

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

like image 139
ameyCU Avatar answered Oct 07 '22 20:10

ameyCU