Search code examples
c++getenvtoken-name-resolution

why getenv() can get name resolved without a std::?


getenv() has a C++ implementation which can be included in the header file . So it is a member of namespace std. However, the getenv() function can be get resolved correctly in my code even without a std::getenv(), which means my follow program can be compiled and run without any error and warning. So why getenv() as a name member of namespace std can get resolved without std::? My OS and compiler is Ubuntu 12.04 i386 and g++ 4.8.1 respectively.

#include <cstdlib>
#include <iostream>

int main()
{
    char * path_env;

    path_env = getenv("PATH"); //without a name resolve operation std::getenv()

    std::cout << path_env << std::endl;

    return 0;
}

Solution

  • Try to use search before asking questions. This is duplicate of why and how does rand() exist both in global and std namespace in cstdlib?

    C++11 Standard: D.5 C standard library headers
    Para 3:

    The header <cstdlib> assuredly provides its declarations and definitions within the namespace std. It may also provide these names within the global namespace. The header <stdlib.h> assuredly provides the same declarations and definitions within the global namespace, much as in the C Standard. It may also provide these names within the namespace std.