×

C++ Tutorial

C++ Data types

C++ Operators & Keywords

C++ Conditional Statements

C++ Functions

C++ 'this' Pointer, References

C++ Class & Objects

C++ Constructors & Destructors

C++ Operator overloading

C++ 11 (Advance C++)

C++ Preparation

C++ Header Files & Functionsr

Data Structure with C++

C++ - Miscellaneous

C++ Programs

getenv() Function with Example in C++

C++ getenv() function: Here, we are going to learn about the getenv() function with example of cstdlib header in C++ programming language.
Submitted by IncludeHelp, on May 28, 2020

C++ getenv() function

getenv() function is a library function of cstdlib header. It is used to get the environment string. It accepts a parameter which is an environment variable name (platform dependent, it may either case sensitive or not) and returns a C-string that contains the value of the environment variable specified as the parameter.

Note: The function is the platform-dependent if specified parameter (environment variable) is not defined, it returns a null pointer.

Syntax

Syntax of getenv() function:

C++11:

char* getenv (const char* name);

Parameter(s)

  • name – represents the name of the environment variable.

Return value

The return type of this function is char*, it returns a C-string that contains the value of the environment variable specified as parameter.

Sample Input and Output

Function call:
getenv ("PATH");

Output:
Specified the environment variable (PATH)

Example

C++ code to demonstrate the example of getenv() function:

// C++ code to demonstrate the example of
// getenv() function

#include <iostream>
#include <cstdlib>
using namespace std;

// main() section
int main()
{
    char* path_string;
    
    // getting path
    path_string = getenv ("PATH");
    
    if (path_string!=NULL)
    cout<<"The current path is: "<<path_string<<endl;

    return 0;
}

Output

RUN 1: (Compiler: https://www.onlinegdb.com/ (c++))
The current path is: /opt/swift/swift-5.0-RELEASE-ubuntu14.04/usr/bin/:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin

RUN 2: (Compiler: https://www.jdoodle.com/online-compiler-c++/)
The current path is: /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/opt/isCOBOL2019R1/bin:/opt/cs/artifacts/Release/bin

Reference: C++ getenv() function



Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.