What is the difference between using #include<filename> and #include<filename.h
> in C++? Which of the two is used and why is it is used?
-
5Voting to reopen. This question is about `
` vs. ` – Heinzi May 12 '17 at 11:51`, whereas [the linked question](http://stackoverflow.com/questions/21593/what-is-the-difference-between-include-filename-and-include-filename) is about ` ` vs `"foo(.h)"`. The linked question is certainly related and a useful resource, but it's not a duplicate.
5 Answers
C++ only include-files not found in the C standard never used filename.h
. Since the very first C++ Standard came out (1998) they have used filename
for their own headers.
Files inherited by the C Standard became cfilename
instead of filename.h
. The C files inherited used like filename.h
are deprecated, but still part of the C++ standard.
The difference is that names not defined as macros in C are found within namespace std::
in cfilename
in C++, while names in filename.h
are within the global namespace scope. So you will find ::size_t
in stddef.h, and std::size_t
in cstddef. Both are Standard C++, but use of ::size_t is deprecated (See Annex D of the C++ Standard).
Now those were the difference.
Why would you use `filename.h` ?
- Compatibility with C compilers
- Compatibility with very old C++ compilers
Why should you use `cfilename` ?
- Names are within namespace
std::
. No name-clashes anymore. - New C++ features (e.g. overloaded math functions for float, long)
- C Compatibility Headers (
filename.h
) could disappear in future.

- 496,577
- 130
- 894
- 1,212
-
Thanks can you explian-The difference is that names not defined as macros in C are found within namespace std:: in cfilename in C++ – yesraaj Nov 19 '08 at 13:58
-
Well. The preprocessor doesn't know about C++ namespaces. It doesn't care about any scope. So C++ cannot put macros into namespaces. The Standard explicitly says that even if C would allow implementing as functions,C++ implementations should still make them macros.That's consequent,and a good thing. – Johannes Schaub - litb Nov 19 '08 at 14:01
-
1Even if they're deprecated, no sane compiler vender will ever deprecate the usage of `filename.h` in C++. They'd break far too many programs and angry far too many customers/developers. – Tom Barta Nov 20 '08 at 01:42
-
deprecating doesn't mean it won't work. you also woudn't notice anyway if there is no warning. after all, compilers aren't required to give any for them. – Johannes Schaub - litb Nov 20 '08 at 05:53
-
2Deprecation is defined as " Normative for the current edition of the Standard, but not guaranteed to be part of the Standard in future revisions." – Johannes Schaub - litb Nov 20 '08 at 05:54
-
Is your explanation still valid for `#include "foo.h"` and `#include "foo"`? Sorry, I am a newbie. – Friendly Ghost Sep 04 '18 at 08:39
The #include <foo.h>
was common in C++ code prior to the C++ standard. The standard changed it to #include <foo>
with everything from the header placed in the std
namespace. (Thanks to litb for pointing out that the standard has never allowed .h headers.)
There is no magic going on, the first looks for a file called 'foo.h' and the second for a file called 'foo'. They are two different files in the file system. The standard just changed the name of the file that should be included.
In most compilers the old headers are still there for backwards compatibility (and compatibility with C), but modern C++ programs that want to follow the standard should not use them.
In the case of standard C headers, the C++ versions have a c at the beginning, so the C header
#include <stdio.h>
becomes
#include <cstdio>

- 14,385
- 3
- 30
- 32
-
How? How did you simply write include in your answer? I'm totally baffled by your awesomeness :) – Robert Gould Nov 19 '08 at 11:21
-
Code blocks written in backticks seems to do the right thing for lt and gt. =) – CAdaker Nov 19 '08 at 11:24
-
-
-
also, here you do the same mistake. filename.h (like iostream.h) was never part of any c+ standard. – Johannes Schaub - litb Nov 19 '08 at 13:27
-
-
Actually, #include
doesn't have to look for a file. It may be implemented otherwise, e.g. using precompiled headers. That can be faster (think shipping precompiled headers) – MSalters Nov 20 '08 at 12:53 -
MSalters is right. it could be anything, including a virtual file within the compiler :) – Johannes Schaub - litb Nov 20 '08 at 16:13
-
Yeah, you're right. But it works just as if it looked for a file, so I didn't feel it was relevant enough to mention. – CAdaker Nov 20 '08 at 22:05
The old standard used the #include <filename.h>
syntax. When namespaces and templates were added to the language, the standard was changed to #include <filename>
.
This was done so that the standard library stuff could all be placed in the std namespace. Older code, which had no concept of namespaces would still work since the #include <filename.h>
files don't use namespaces.
New code should always use the #include <filename>
format. If you use the older format, all the symbols they define will be placed in the global namespace rather than std.

- 98,941
- 38
- 226
- 299
-
There was no "old standard" that used filename.h . That was merely common among compilers before the first c++ standard came out – Johannes Schaub - litb Nov 19 '08 at 13:26
-
-
1ok, standard may have been a little strong. I should have said "de facto standard" or "common practice". – Ferruccio Nov 19 '08 at 14:49
Those without the .h is C++ header files while those with .h are C header files. This only applies to the standard header files in C++.
If you are including your own files or files that is not part of standard C++ you need to always write the complete file name (which can be anything).

- 861
- 7
- 12
If you are talking about the standard libraries (because otherwise it wont work at all), the difference is
#include< header >
//my code
is the correct way to call the header according the the C++ standard, while
#include< header.h >
//my code
is deprecated (in the C++ standard, but still necessary according to the C99 standard) and came along with the rest of the C baggage.
So you should use:
#include< cmath >
//my code
not:
#include< math.h >
//my code

- 68,773
- 61
- 187
- 272
-
Its not deprecated, you have to use the .h files if you want to do C programming. – Magnus Westin Nov 19 '08 at 11:12