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"

1
  • 1
    See this question for the difference between <> and "".
    – aib
    Oct 18, 2008 at 0:56

9 Answers 9

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).

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.

1
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.

1
  • 1
    After reading a few books, I have inferred that #include<iostream.h> includes a specific file called iostream.h in our program whereas, #include<iostream> simply guarantees that everything belonging to the iostream library is included in our program. Am I correct?
    – Shravan
    Nov 17, 2014 at 16:06
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.

1
  • upvote for mentioning ".h" declarations in global scope and no need of std namespace prefix Jun 6, 2017 at 12:51
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.

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.

1
  • 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, 2008 at 22:32
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.

1
  • Thanks, I was wondering where the actual files were located.
    – Andrew
    Mar 5, 2021 at 23:03
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.

2
  • 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. Jun 5, 2018 at 1:45
  • You can merge it into one answer by editing 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.) Jul 10, 2022 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.

2
  • While this is allowed in theory, I don't know any compiler that does this. Mar 18, 2022 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: forum.vmssoftware.com/viewtopic.php?t=39 Mar 18, 2022 at 23:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.