首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

std::getenv

Defined in header <cstdlib>

?

?

char* getenv( const char* env_var );

?

?

搜索环境清单主机环境%28提供OS%29,用于与env_var并返回指向与匹配的环境列表成员关联的C字符串的指针。

This function is not required to be thread-safe. Another call to getenv, as well as a call to the POSIX functions setenv(), unsetenv(), and putenv() may invalidate the pointer returned by a previous call or modify the string obtained from a previous call.

(until C++11)

This function is thread-safe (calling it from multiple threads does not introduce a data race) as long as no other function modifies the host environment. In particular, the POSIX functions setenv(), unsetenv(), and putenv() would introduce a data race if called without synchronization.

(since C++11)

修改由getenv调用未定义的行为。

参数

env_var

-

null-terminated character string identifying the name of the environmental variable to look for

返回值

字符串标识环境变量的值,如果找不到该变量,则为空指针。

注记

关于POSIX系统,环境变量也可以通过全局变量访问。environ,宣布为extern char **environ;<unistd.h>,通过第三个可选的论点,envp...的主要功能...

二次

代码语言:javascript
复制
#include <iostream>
#include <cstdlib>
 
int main()
{
    if(const char* env_p = std::getenv("PATH"))
        std::cout << "Your PATH is: " << env_p << '\n';
}

二次

可能的产出:

二次

代码语言:javascript
复制
Your PATH is: /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games

二次

另见

c getenv文件

*。

代码语言:txt
复制
 ? cppreference.com

在CreativeCommonsAttribution下授权-ShareAlike未移植许可v3.0。

扫码关注腾讯云开发者

领取腾讯云代金券

http://www.vxiaotou.com