Difference between iostream and iostream.h

What is the difference between iostream and iostream.h?


iostream.h is deprecated by those compilers that provide it, iostream is part of the C++ standard.

To clarify explicitly there is no mention of iostream.h at all in the current C++ standard (INCITS ISO IEC 14882 2003).

Edit: As @Jerry mentioned, not only does the current standard not mention it, but no standard for C++ mentions it.


iostream is a standard header. iostream.h is a non-standard header that was very common in pre-standard C++, and is what iostream evolved from. It's still common to have iostream.h around, presumably for use with older programs.

If your implementation have a working copy of iostream.h, it is probably the same as iostream except that everything in iostream is in the std namespace, while iostream.h generally preceded namespaces, and didn't use them.

If your implementation has both iostream and iostream.h, iostream is likely to work like:

namespace std
{
#include <iostream.h>
}

although that's not necessarily how it's written.


When C++ was first created, all of the files in the standard runtime library ended in .h. Life was consistent, and it was good. The original version of cout and cin lived in iostream.h. When the language was standardized by the ANSI committee, they decided to move all of the functions in the runtime library into the std namespace (which is generally a good idea). However, this presented a problem: if they moved all the functions into the std namespace, none of the old programs would work any more!

To try to get around this issue, while maintaining backward compatibility for older programs, a new set of header files was introduced that use the same names but lack the .h extension. These new header files have all their functionality inside the std namespace. This way, older programs that include #include <iostream.h> do not need to be rewritten, and newer programs can #include <iostream>.

When you include a header file from the standard library, make sure you use the non .h version if it exists. Otherwise you will be using a deprecated version of the header that is no longer supported.

In addition, many of the libraries inherited from C that were still useful in C++ were given a c prefix (e.g. stdlib.h became cstdlib). The functionality from these libraries was also moved into the std namespace to help avoid naming collisions.

However, when you write your own header files, you should give them all a .h extension, since you will not be putting your code in the std namespace.

Rule: use the non .h version of a library if it exists, and access the functionality through the std namespace. If the non .h version does not exist, or you are creating your own headers, use the .h version

Source: https://www.learncpp.com/cpp-tutorial/19-header-files/