48

I was reading another thread where a guy asked about C++ books for beginners, and one of the programmers answering wrote this:

Some warnings: avoid all books that present an "hello world" stating with

#include <iostream.h>

I opened my C++ book and sure enough it included the iostream header like the example above.

Why is that bad? What other pointers should I keep in mind when learning C++?

Background: I am proficient with C and I'll start to learn C++ this next semester.

Daniel Scocco
  • 5,832
  • 9
  • 39
  • 47
  • 3
    Another, related pointer, is to include `cstdio`, not `stdio.h` (the latter is deprecated). – Cactus Golov Dec 26 '11 at 17:25
  • 7
    @AntonGolov Opinions differ. Many experts prefer as there is no **technical reason** why should be prefered. – Sjoerd Dec 26 '11 at 22:59
  • 2
    @Sjoerd The fact that `` is guaranteed to provide the names in `namespace std` is reason enough for me to prefer it. I know that it might *also* provide them in the global name-space just as `` *might* provide them in `namespace std`. It is also a matter of consistency if you make it a habit to always use the `` headers. And for some headers, you'll really want this because they enhance the C interface with additional function overloads, for example. – 5gon12eder Oct 08 '15 at 00:35

4 Answers4

58

The header iostream.h is a non-standard header and does not exist on all platforms. As a matter of fact it does not exist on my system (using g++ and the GNU libstdc++). So any code using it would simply not compile on my system.

The iostream.h header used to be common before C++ was first standardized in 1998. But since the 98 standard used <iostream> instead of <iostream.h>, the latter has fallen out of favor (being non-standard and all) and is no longer supported on all platforms. Code that uses it should be considered non-standard legacy code and is not portable. Books that teach it should be considered outdated and avoided.

treecoder
  • 9,475
  • 10
  • 47
  • 84
sepp2k
  • 4,339
  • 1
  • 25
  • 25
  • 14
    I wouldn't avoid a book outright just because of a trivial preprocessor syntax issue. It could be a great book while a terrible book could be using the modern syntax. – Lord Tydus Dec 26 '11 at 13:19
  • 21
    @Lord Tydus The fact that any given pre-98 book _could_ be a great book does not negate the fact that statistically, you would be better off avoiding pre-98 books. – Mike Nakis Dec 26 '11 at 13:33
  • 12
    @LordTydus: Totally disagree. The style and usage of C++ is not the same as in 98 thus it is not just fixing syntactic issues. – Martin York Dec 26 '11 at 18:00
  • 7
    @LordTydus If the old syntax plain and simply does not compile on modern compilers, you'll have a hard time using a book that teaches the old syntax. Note that any book that teaches the usage of iostream.h almost certainly doesn't teach e.g. namespaces either, so even after replacing iostream.h with iostream, your code won't work. If you have to google or ask for help on SO every time you want to compile an example from the book, that's not a very effective way to learn C++. – sepp2k Dec 26 '11 at 21:47
  • 3
    @LordTydus: In general, I've found that books that use headers like that tend also to use bad practices and are riddled with errors. I've taken to collecting such books *just to keep them out of circulation.* – greyfade Dec 27 '11 at 17:09
  • 2
    I've encountered a compiler that implemented `` as merely an include of `` and a `using namespace std;` – Brian Jul 30 '13 at 18:24
  • 2
    @LordTydus: It's not just about that one header. The fact is, if the header is used in that book, it probably says a lot about the quality of the rest of the book. – Lightness Races in Orbit Jan 11 '14 at 17:11
  • @LokiAstari: So, is there any difference between & ? – Destructor Jun 24 '15 at 17:40
  • @PravasiMeet: was deprecated in 1998. You should **NOT** use it. – Martin York Jun 24 '15 at 19:02
  • @Brian Then you encountered a very friendly compiler. Ideally, including its `iostream.h` would also emit a custom warning, something like `Warning: Header '' is a non-standard header that was commonly used before C++ was officially standardised. Please update your code to use '' instead. Note that with this compiler, '' includes '', and specifies using-directive 'using namespace std;'.` – Justin Time - Reinstate Monica Dec 10 '16 at 21:20
56

#include <iostream.h> is a sign that the book was written prior to the first C++ standard in 1998 (the standard header is iostream).

The problem is that older C++ code tends to be written in ways that are considered bad practice today. In particular,

  • The use of C-style arrays rather than container classes like std::string and std::vector.
  • The use of explicit close functions rather than RAII.

iostream.h isn't the worst thing a pre-1998 book will get wrong, but it's likely to be the first thing that a pre-1998 book will get wrong.

dan04
  • 3,748
  • 1
  • 24
  • 26
1

Maybe this comes a bit late but for what it's worth, on a unix/linux box do ls /usr/{local/,}include/c++/* or similar, according to your layout and paths. You could pipe to grep looking for the header in question, like:

ls /usr/{local/,}include/c++/* | grep iostream 

This entails a look-up for iostream.h as well as any other superstrings.

Or run find / -type f -name iostream 2> /dev/null | grep include or locate iostream | grep include (provided the database is current, otherwise prepend with a call to updatedb)--these, however, will print also non-system-wide includes, so please adjust appropriately. The actual C++ include path is easily found with something like:

g++ -v 2>&1| sed -rn 's/.+gxx-include[^=]+=([^ ]+).+/\1/p' # adjust iff empty

Equivalently on Windows and other machines. I guess the idea is clear--such a file as iostream.h doesn't exist in the system include path by default any longer, you still, however, can find legacy libc++ distributions with iostream.h either soft-linked to iostream or as its copy. So this is not a matter of style but rather of the circumstances. You could ship your own iostream.h with your project just make sure it's contained in the include path where your compiler looks up for the <...> headers.

Nicholas
  • 31
  • 2
-1

Just dropping my 2 cents. I don't think there is a correlation between ".h" and the quality of a book. This is a minor syntax issue. Back in the day it was actually the correct sytnax.

Is it possible to have a great book with iostream.h? yes

Is it possible to have a terrible book with iostream? yes

I would rely on online user reviews (and my own review after reading) to judge the quality of a book.

Lord Tydus
  • 1,429
  • 1
  • 10
  • 12
  • 3
    Problem is, are you sure you want a book from "back in the day?" – hugomg Dec 26 '11 at 13:39
  • 5
    Is Lisp outdated because it's from 1958? We utilize Pythagoras's work in every modern missile system even though the math is thousands of years old. In the current C++ book market the ".h" books may well be terrible. But it is a quality of book issue, not a ".h" issue. ".h" isn't even programming logic, it's for the preprocessor. – Lord Tydus Dec 26 '11 at 13:50
  • 5
    but will someone learning the language for the first time know when the book is telling them something wrong? How much time and frustration will they go through before finding out that the ".h" is now incorrect? And in how many other ways is the book out of date? – Chris Pitman Dec 26 '11 at 16:27
  • 24
    The trouble is the usage of C++ has changed significantly since those books were written. The whole way you think and use C++ is not the same as those books will present as they do not have any of the facilities that modern C++ has. As a result you will be teaching yourself C with classes not C++. – Martin York Dec 26 '11 at 17:58
  • Modern C++ is nothing from '98. – Coder Dec 26 '11 at 21:25
  • @Loki Astari: C++ has really become a very different language wrt to what it used to be 10, 15 years ago. I often think that the language should get a new name to make this difference clear. – Giorgio Dec 26 '11 at 21:38
  • 1
    @Giorgio: ++C++ – Martin York Dec 26 '11 at 22:03
  • 1
    @Loki Astari: Yes, for example. I had thought of MPL (multi-paradigm language) or something like that. Anyway, C++ is no longer (only) an object-oriented extension of C. I think it would also be time to drop backward compatibility with C since the C and C++ communities have become rather independent from each other: you do not need backward compatibility to encourage C programmers to adopt C++. – Giorgio Dec 26 '11 at 22:30
  • 3
    @Giorgio: Boring. What about ACRE. Advanced C on Ritalin with Exceptions. Acre also implies it covers a lot of ground. :-) – Martin York Dec 27 '11 at 01:26
  • 1
    @Loki Astari: :-) You really do not want to drop its relation to C from the name. ;-) – Giorgio Dec 27 '11 at 09:30
  • At least, ACRE sounds a lot better than BJ (Broken Java) ;-) – user281377 Dec 27 '11 at 14:39
  • @ammoQ I find java more broken in some aspects than C++, for example, the template system of C++ is much better than java, as I can do much more things in it than in java. Also having a multibyte library can be bad sometimes, because it forces you to use the same base library even if you want to do something different or if you not use something, for example AWT and Swing in server applications. But other things, java does right and the ABI is one of them, since all java programs are much more modular than C++ ones. Others are just equal, like multiple inheritance vs interfaces. – Coyote21 Dec 27 '11 at 15:07
  • 2
    Coyote21: My comment wasn't really serious, I just needed a word that starts with B to get to that abbreviation for you-know-what. Make it "Better Java" if you like. – user281377 Dec 27 '11 at 16:11
  • This answer is just assertions without arguments, and doesn't address the real issues brought up by others. I think the answer would be better with some discussion of why a book on pre-standard C++ (or, worse, one written by somebody who didn't understand the difference, and just writes what works on his or her favorite compiler) is likely to be as good as a more modern one. – David Thornley Dec 27 '11 at 21:55
  • 4
    This has nothing to do with "syntax" whatsoever. – Lightness Races in Orbit Jan 11 '14 at 17:12
  • 4
    -1. As @LightnessRacesInOrbit points out, this isn't a simple syntactic issue. The change from to was done for a very important reason: to maintain backwards compatibility for code that doesn't use namespaces when the facility was introduced and the standard library was moved into the std namespace. You can therefore be confident that a book using doesn't teach this critical feature of the language, and is therefore so incomplete as to be virtually useless. – Jules Oct 08 '15 at 08:16