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函数