How to Get Environment Variable in C++? Last Updated : 28 May, 2024 Comments Improve Suggest changes Like Article Like Report 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++. Accessing Environment Variable 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: Syntaxchar* getenv(const char* env_name);where: env_name: is the name of the environment variable you want to get.return value: Char * pointer to the value of the specified environment variable or NULL if the variable is not found.C++ Program to Get Environment Variable The below example demonstrates the use of the getenv() function to get the value of an environment variable in C++: C++ // C++ Program to Get Environment Variable #include <cstdlib> #include <iostream> using namespace std; int main() { // Specify the name of the environment variable const char* env_var_name = "PATH"; // Get the value of the environment variable const char* env_var_value = getenv(env_var_name); // Check if the environment variable was found if (env_var_value != nullptr) { cout << env_var_name << " = " << env_var_value<< endl; } else { cout << "Environment variable " << env_var_name<< " not found." << endl; } return 0; } OutputPATH = /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. Comment More infoAdvertise with us Next Article How to Get Environment Variable in C++? P pantharshx9d9 Follow Improve Article Tags : C++ Programs C++ Misc C++ CPP Examples misc-cpp +1 More Practice Tags : CPP Similar Reads How to Declare a Global Variable in C++? In C++, global variables are like normal variables but are declared outside of all functions and are accessible by all parts of the function. In this article, we will learn how to declare a global variable in C++. Global Variable in C++ We can declare a global variable, by defining it outside of all 2 min read How to Get File Extension in C++? In C++, we may often find the need to extract the file extension from a given path of the file while working in many applications for processing or validating. In this article, we will learn how to get the file extension in C++. For Example, Input: someFolder â³ filename.ext Output: File Extension = 2 min read How to Get Current Directory in C++ The current working directory, also known as the present working directory, is the location on the file system where the executing program is located and operates from. When working with files and directories in C++, it is important to determine the current working directory to find the resource fil 2 min read How to Access Private Variable in C++? In C++, Private members of a class are accessible only within the class they are declared and by friend functions, they are not accessible outside the class not even by derived classes. In this article, we will learn how to access private variables in C++. Accessing Private Variables From Class in C 2 min read How to Access a Local Variable from a Different Function in C++? In C++, local variables defined within a function in C++ are only available inside the scope of that particular function. In some situations, we may need to access these local variables in some other function. In this article, we will discuss how to access a local variable from a different function 3 min read Like