简体   繁体   中英

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()
    {
        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 (ie your code may not compile on some other compilers).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM