![]() |
VOOZH | about |
Environment variables are globally accessible named values that store information about the system environment where your code is executed. They are also used to store configuration settings, paths to important directories, and other system-specific data as well. In this article, we will learn how to get environment variables in C++.
To get the value of an environment variable in C++, we can use the getenv() function provided by the <cstdlib> header. This function takes the name of the environment variable as a parameter and returns its value. Following is the syntax to use the getenv() function:
char* getenv(const char* env_name);where:
The below example demonstrates the use of the getenv() function to get the value of an environment variable in C++:
PATH = /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
Time Complexity: O(1)
Auxiliary Space: O(1)
Note: The value of the output may differ in your system.