C Standard Libraries C++

getenv()

Declaration

char* getenv(const char* kcpEnvVarName);

Description

Retrieve the value of an environment variable. The parameter pointed to by the null-terminated string "kcpEnvVarName" is the name of the environment variable. The return value is its environment variable's value as a null-terminated string or NULL if the variable is undefined.

Example

#include <iostream>
#include <cstdlib>

int main() {
    using namespace std;

    char caEnvVarName[] = "SomeVariableName";
    char* cpEnvVarValue = getenv(caEnvVarName);

    if (cpEnvVarValue == 0) {
        cout << "Environment variable \"" << caEnvVarName << "\" does not exist." << endl;
    } else {
        cout << "Environment variable \"" << caEnvVarName << "\" = " << cpEnvVarValue << endl;
    }

    return 0;
}

Output

getenv() Output
 

© 2007–2024 XoaX.net LLC. All rights reserved.