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?
相关问题
- Sorting 3 numbers without branching [closed]
- How to compile C++ code in GDB?
- Why does const allow implicit conversion of refere
- thread_local variables initialization
- What uses more memory in c++? An 2 ints or 2 funct
相关文章
- Class layout in C++: Why are members sometimes ord
- How to mock methods return object with deleted cop
- Which is the best way to multiply a large and spar
- C++ default constructor does not initialize pointe
- Selecting only the first few characters in a strin
- What exactly do pointers store? (C++)
- Converting glm::lookat matrix to quaternion and ba
- What is the correct way to declare and use a FILE
If you are talking about the standard libraries (because otherwise it wont work at all), the difference is
is the correct way to call the header according the the C++ standard, while
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:
not:
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 usedfilename
for their own headers.Files inherited by the C Standard became
cfilename
instead offilename.h
. The C files inherited used likefilename.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::
incfilename
in C++, while names infilename.h
are within the global namespace scope. So you will find::size_t
in stddef.h, andstd::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` ?
Why should you use `cfilename` ?
std::
. No name-clashes anymore.filename.h
) could disappear in future.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.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 thestd
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
becomes
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).