37

When including a header file in C++, what's the difference between...

  1. including the .h part versus not including .h part when wrapping it in <> signs?

    #include <iostream> vs. #include <iostream.h>

  2. wrapping the header name in double quotes versus wrapping it in < > signs?

    #include <iostream.h> vs. #include "iostream.h"

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
BeachRunnerFred
  • 18,070
  • 35
  • 139
  • 238
  • 1
    See [this question](http://stackoverflow.com/questions/21593/what-is-the-difference-between-include-filename-and-include-filename) for the difference between <> and "". – aib Oct 18 '08 at 00:56

9 Answers9

52

In short:

iostream.h is deprecated—it is the original Stroustrup version. iostream is the version from the standards committee. Generally, compilers point them both to the same thing, but some older compilers won't have the older one. In some odd cases, they will both exist and be different (to support legacy code) and you then must be specific.

"" versus <> simply means check the local directories for the header before going to the library (in most compilers).

Cody Gray - on strike
  • 239,200
  • 50
  • 490
  • 574
Adam Davis
  • 91,931
  • 60
  • 264
  • 330
7

As the person on the standards committee (X3J16) who proposed leaving off the .h, my original intent was to settle the debate over .h, .H, .hpp, .hxx, or .h++ file extensions; or a desire by some that there be no implication in the standard that this was the name of a file on disk in order to allow an IDE to pull pre-compiled header information out of somewhere internal, like a resource file or even the guts of the compiler.

While Unix considered the filename to be a single string and did not actually recognize the concept of an extension, DEC operating systems had a tradition of separating the name from the extension, and supplying the "default extension" if it was omitted in particular contexts. That's where I got the idea from of leaving it up to the implementation to use whatever extension the implementation wanted to use, and it allowed the implementation to not even have this a file on disk. (I was DEC's representative on the committee at the time.)

Differentiating between the standard and the pre-standard headers was an added benefit.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Aron Insinga
  • 81
  • 1
  • 3
  • 1
    After reading a few books, I have inferred that `#include` includes a **specific file called iostream.h** in our program whereas, `#include` simply guarantees that everything belonging to the `iostream` library is included in our program. Am I correct? – Shravan Nov 17 '14 at 16:06
7

Here is a decent link article.

To summarize, the reason given:

The version of the iostream library that the Standards Committee produced was quite a bit different from the CFront implementation. {snip}

To ease transition, the C++ Standards Committee declared that code including the standard C++ headers would use include directives that lack an extension. This allowed compiler vendors to ship the old style C++ library headers with the .h extension and the new style headers without.

An advantage of not using the .h version:

There are several reasons why new code should be written using the extensionless version of the header files instead of the .h forms. The first is the unpredictability of such code when compiled on modern compilers. As previously mentioned, the result of using the .h headers is implementation specific. And as time goes by, the chance that a given compiler will have the old style library available decreases.

Zee JollyRoger
  • 399
  • 4
  • 15
  • Related: *[Answers entirely copied though properly attributed](https://meta.stackoverflow.com/questions/321299/answers-entirely-copied-though-properly-attributed/321326#321326)*. – Peter Mortensen Jul 10 '22 at 14:34
2

The standard way (and the only one guaranteed to work) is <iostream>. On gcc, <iostream.h> (which might need to be included as <backward/iostream.h>) pulls the relevant declarations to the global namespace (so you do not need the std:: namespace prefix).

"iostream.h" would try first from the directory with your source code, since "" is meant for headers from your project. <> should always be used for system headers, and "" for your own headers.

CesarB
  • 43,947
  • 7
  • 63
  • 86
1

Typically <> is used for system or standard library files whereas "" is used for project files. I would not be surprised if your compiler searches locally and when it cannot find it it defaults to the standard library version.

As for the .h, I don't think that it actually matters if you use C. In C++, I remember vaguely that there was a newer version and an older version and that without the h it was supposed to be the new version, but I'm not even sure the old version still exists.

Uri
  • 88,451
  • 51
  • 221
  • 321
1

The simple answer to the first answer is that iostream.h doesn't exist, at least in the GCC implementation. If you're on a Unix-like system, type

% locate iostream.h
/usr/include/c++/3.4.3/backward/iostream.h

and

% locate iostream
/usr/include/c++/3.4.3/iostream
/usr/include/c++/3.4.3/backward/iostream.h

As Zee's article says, iostream.h is for backward compatibility.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
yogman
  • 4,021
  • 4
  • 24
  • 27
1

These are really two different questions.

  • The difference between the .h and extensionless headers with the same name is historical. The ones with the .h extension are from the original C++ standard which did not have some modern features such as namespaces and templates. It was simpler for the new standard to put that same functionality in new header files to be able to use these new features and keep the old (.h) files for backward compatibility of legacy code.

  • The difference between the #include <...> and #include "..." format is the order in which the compiler looks for files. This is generally implementation dependent, but the idea is that the <> format looks in system include directories first, while "" looks in the same directory as the source file that #included it first.

Ferruccio
  • 98,941
  • 38
  • 226
  • 299
  • 1
    A minor correction to the first point: iostream.h was prestandard, and was different between compilers. iostream was added in the first c++ standard. – KeithB Oct 19 '08 at 22:32
1

Regarding the names of the standard C++ header files, in the early days (the first two years) of X3J16, we faced an argument over what the extension should be on the standard C++ header files.

In use at the time by various vendors (and influenced by constraints that some operating systems placed on file names) I believe there were .h, .H, .h++, .hpp, .HXX, and possibly others. In a library group meeting I suggested that we leave the extension off, and leave it up to the implementation to supply a default file extension of its choosing if there was none in the include line, or use the name as a key in a database of pre-compiled header files if desired.

(While Unix-like systems treat the filename and 'extension' as a single string, I was representing DEC on the committee, and many DEC operating systems stored the extension in the directory as a separate field from the name. So DEC operating systems had a strong tradition of applying a default extension based on what program was accessing the file for what purpose. Telling an assembler 'X,Y=Z' might result in reading input file Z.MAC (macro) and writing output files X.OBJ and Y.LST.)

Anyway, it avoided a long, no-win debate, so the group went along with it, and Andy Koenig presented the group's conclusions on this (among others) to the entire committee which accepted it. I find it somewhat amusing that implementations missed the whole point that they could apply a default extension of their choice (which I would think would be useful to editors and other tools) and just left the extension off of the file name.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Aron Insinga
  • 81
  • 1
  • 3
  • *sigh* Sorry, I now see that I already wrote on this topic. That's the kind of thing that happens when I'm wearing the wrong glasses. :-) Well, I hope that this version adds another bit or two of information to the historical record. – Aron Insinga Jun 05 '18 at 01:45
  • You can merge it into one answer by [editing](https://stackoverflow.com/posts/19719687/edit) the highest scored one and delete this one. (But *************** ***without*** ************ "Edit:", "Update:", or similar - the answer should appear as if it was written ***today***. That is the revision history / edit summary is for. The historical information is in the revision history and does not belong in the current version. I am sorry if it seems obvious, but most people don't get it right.) – Peter Mortensen Jul 10 '22 at 14:53
0

The compiler is free to add a missing ".h" (or whatever it wants) to a standard header name in order to determine the name of the actual disk file to read, if the compiler indeed gets standard headers from actual disk files. So the user program can say "#include <iostream>" and the compiler could be smart enough to open up a file named "iostream.h" (or whatever it wants) in some directory it knows about (or is told about via command line switches). The standard does not require the standard header to be in an actual disk file of text with any particular name.

Aron Insinga
  • 81
  • 1
  • 3
  • While this is allowed in theory, I don't know any compiler that does this. – HolyBlackCat Mar 18 '22 at 21:55
  • Yes, I consider it sad that the compiler authors have not taken advantage of the ability to have an extension on the header file names so that users, editors, and other tools can determine the language a file is in from the name while maintaining portability through standard compliance in the #include directive. ... If you meant the header not coming from a text file, on VMS the compiler can open a .TLB (text library [archive]) file and retrieve the header from it: https://forum.vmssoftware.com/viewtopic.php?t=39 – Aron Insinga Mar 18 '22 at 23:02