tags:

views:

1669

answers:

7

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

1) including the .h versus not including the .h 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"

Thanks in advance!

+2  A: 

Here is a decent link article.

Zee JollyRoger
Just what I was looking for, thanks
Proclyon
+1  A: 

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
+21  A: 

In short:

iostream.h is deprecated - it is the original Stroustrup version, and 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).

Adam Davis
+1  A: 

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
A: 

The simple answer to the first answer is that iostream.h doesn't exist, at least in the GCC implementation. If you're on *nix, 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.

yogman
A: 

See this question for the difference between <> and "".

aib
A: 

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