Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      std::getenv

      From cppreference.com
      <cpp‎ |utility‎ |program
       
       
      Utilities library
       
       
      Defined in header<cstdlib>
      char* getenv(constchar* env_var);

      Searches theenvironment list provided by the host environment (the OS), for a string that matches the C string pointed to byenv_var and returns a pointer to the C string that is associated with the matched environment list member.

      This function is not required to be thread-safe. Another call togetenv, as well as a call to the POSIX functionssetenv(),unsetenv(), andputenv() 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 functionssetenv(),unsetenv(), andputenv() would introduce a data race if called without synchronization.

      (since C++11)

      Modifying the string returned bygetenv invokes undefined behavior.

      Contents

      [edit]Parameters

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

      [edit]Return value

      Character string identifying the value of the environmental variable or null pointer if such variable is not found.

      [edit]Notes

      On POSIX systems, theenvironment variables are also accessible through the global variableenviron, declared asexternchar** environ; in<unistd.h>, and through the optional third argument,envp, ofthe main function.

      [edit]Example

      Run this code
      #include <cstdlib>#include <iostream> int main(){if(constchar* env_p= std::getenv("PATH"))std::cout<<"Your PATH is: "<< env_p<<'\n';}

      Possible output:

      Your PATH is: /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games

      [edit]See also

      C documentation forgetenv
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=cpp/utility/program/getenv&oldid=160651"

      [8]ページ先頭

      ©2009-2025 Movatter.jp