The behavior is Uspecified behavior as per standard.
As per the standard including cstdlib
imports the symbol names in std
namespace and possibly in Global namespace. If you rely on symbol names being included in global namespace then your program is non portable and you are relying on behavior of a specific implementation.
To not rely on the implementatio behavior you must:
Include cstdlib
and use fully qualified name for rand
.
std::rand()
References:
C++11 Standard: D.5 C standard library headers
Para 3:
[ Example: 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
. —end example ]
Good Read:
Should I include <xxxx.h> or <cxxxx> in C++ programs?