c语言getenv函数_getenv()函数与C ++中的示例

c语言getenv函数

C ++ getenv()函数 (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.

getenv()函数cstdlib标头的库函数。 它用于获取环境字符串。 它接受作为环境变量名称的参数(取决于平台,可能区分大小写),并返回一个C字符串,其中包含指定为参数的环境变量的值。

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

注意:如果未定义指定的参数(环境变量),则该函数与平台有关,它将返回空指针。

Syntax of getenv() function:

getenv()函数的语法:

C++11:

C ++ 11:

    char* getenv (const char* name);

Parameter(s):

参数:

  • name – represents the name of the environment variable.

    name –代表环境变量的名称。

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.

该函数的返回类型为char * ,它返回一个C字符串,其中包含指定为参数的环境变量的值。

Example:

例:

    Function call:
    getenv ("PATH");

    Output:
    Specified the environment variable (PATH)

C ++代码演示getenv()函数的示例 (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

参考: C ++ getenv()函数

翻译自: https://www.includehelp.com/cpp-tutorial/getenv-function-with-example.aspx

c语言getenv函数

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值