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