开发者

C++ code runs with missing header, why?

I just realized that I am supposed to include the #include<cstdlib> required by abs() for the abs() function.

 #include<iostream>
    using namespace std;
    int main()
   开发者_C百科 {
        int result;
        result = abs(-10);
        cout << result << "\n";
        return 0;

    }

Why does this code still work, even though I forgot the important header (#include<cstdlib>)?


That's because iostream indirectly includes definition for abs(). It is allowed by the Standard, but should not be relied upon, because it's implementation-dependant (i.e. your code may not compile on some other compilers).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜