views:

229

answers:

3

What are the differences between below 3 programs ?. Is <iostream> a header file or C++ standard library ?

1.

#include<iostream>
using namespace std;

int main()
{
        return 0;
}

2.

#include<iostream>

int main()
{
        return 0;
}

3.

#include<iostream.h>

int main()
{
        return 0;
}

Thanks in advance.

+3  A: 

You first two programs are standard C++ programs whereas the 3rd program uses a non-standard header file <iostream.h>

In 1st program using namespace std brings entire namespace std into scope.

Have a look at this for more information.

Prasoon Saurav
The header was never part of an y C++ standard, and so can't really be said to be deprecated, which has a specific technical meaning in C++.
anon
@Neil: http://www.devx.com/tips/Tip/14447 clearly says that `<iostream.h>` is a deprecated header file. Also to disable the warning that we get on using `<iostream.h>` we use gcc's `-Wno-deprecated` option. Then I can't find the reason why `<iostream.h>` can't be called deprecated? Please elaborate.
Prasoon Saurav
@Prasoon Saurav: That's devx.com, not the standard.
Billy ONeal
It's not a deprecated part of the C++ standard, but I'd argue it's correct to say that it's a deprecated feature of any given now-standards-conforming compiler.
Tyler McHenry
Who cares what some random internet link says? The deprecated features of C++ are listed in Annex D of the C++ Standard.
anon
@Neil: Yes you are correct. Managed to find deprecated features of C++ in Annex D of the C++ standard. `<iostream.h>` is not deprecated.
Prasoon Saurav
Fair point though about the `no-deprecated` flag. Maybe 'anachronistic' would be a better description? Ultimately though, it (`iostream.h`) just shouldn't be used with a modern compiler.
Robin Welch
+12  A: 

As far as the program that is produced, there is zero difference - since nothing in the iostream library is referenced by the program, none of the library will be compiled in by any intelligent compiler.

When you #include <iostream>, you're including the header file with declarations for the iostream standard library. Using #include <iostream.h> does essentially the same as #include <iostream>, except that it defines all of the library's names within the global namespace as opposed to being in std:: - it's there for reverse-compatibility with programs that used the original version of iostream which didn't drop the .h. (The <iostream.h> versions also often don't support wide characters, but only standard char's.)

using namespace std; means that the default namespace for name references in the current file will be std, which is the namespace used by most standard library functions. While it means you don't have to prefix all of the standard library calls with std::, it also means that you have to be careful not to define anything that overlaps with standard library names.

Amber
Actually, I believe `iostream.h` puts things in the Global Namespace (even though no compiler under the sun actually does this). I could be wrong.
Billy ONeal
Ah, right, good point. Had forgotten that. Updated.
Amber
@Billy: `iostream.h` does not exist in the C++ standard, so it doesn't actually do anything in general; one would have to specify which implementation. It's a relic from pre-standard C++, back before namespaces, so on any given implementation that has one it probably does the same as `#include <iostream>` followed by `using` directives to export all its symbols into the global namespace.
David Thornley
+1  A: 

iostream is a header file that provides declarations and prototypes that are an interface to part of the C++ standard library.

There's is, on your system, a file called "iostream" (no extension), the contents of which are copied and pasted (with recursive processing of #includes) at the point where you write #include <iostream>.

#include directives always pull in the contents of header files, they never add "libraries". Header files often contain declarations and prototypes that are an interface to a library, but the actual libraries themselves are attached to your program by the linker, not the compiler. When linking a C++ program, the linker will automatically attach the C++ standard library unless you tell it not to, so you don't have to worry about that.

Similarly, the using namespace std statement does not do the work of attaching the library. This statement only makes it so that you can write, for example, cout or string instead of qualifying them as std::cout and std::string. This works for any namespace, but is usually discouraged.

For the three examples you gave, they all give you the definitions and prototypes you need to use the iostream portion of the C++ standard library, but (2) is preferred, (1) is acceptable, and (3) is deprecated. (2) gives the additional convenience of being able to omit the std:: prefix (at the cost of reducing the variable names available for you to use yourself), and (3) includes a different file called "iostream.h" instead of "iostream", which is usually the same thing, but the file with the .h is a relic of pre-standard C++ and so may not be supported in future compilers.

Tyler McHenry