Why does rand() compile without including cstdlib or using namespace std?

前端 未结 3 1720
终归单人心
终归单人心 2020-12-11 03:49

According to the book I\'m reading, rand() requires #include in C++
However, I am able to compile the following code that uses

3条回答
  •  佛祖请我去吃肉
    2020-12-11 04:14

    You definitely should use the relevant include file for what you are using in your code. It saves you from surprises when you update the compiler/libraries to the new version. I think adding std:: in front of rand is a much better idea than to use using namespace std; - but either way, it's a good idea to NOT rely on it existing without a namespace, although that tends to be the way it works in most places to allow backwards compatibility for C-code.

提交回复
热议问题