Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between using #include<filename> and #include<filename.h> in C++

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?

like image 637
yesraaj Avatar asked Nov 19 '08 11:11

yesraaj


People also ask

What is the difference use and using?

From a grammatical viewpoint, the word use in for use is a noun. the word using in for using is a verb. Otherwise, the words would be interchangeable in most contexts. This is a rather potent poison, for use on household pests.

What is difference between using and in Python?

Here is a list of the differences between 'and' and '&' in Python. The and is a type of Logical AND that returns in a True form whenever both the operands are also true. The &, on the other hand, is a bitwise operator used in the Python language. It basically acts on various bits and performs operations bit by bit.

What is use and utilization?

use: take, hold, or deploy (something) as a means of accomplishing or achieving something; employ. —Oxford English Dictionary. utilize: to make or render useful; to convert to use, turn to account.


4 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.
like image 81
Johannes Schaub - litb Avatar answered Oct 22 '22 16:10

Johannes Schaub - litb


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>
like image 38
CAdaker Avatar answered Oct 22 '22 16:10

CAdaker


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.

like image 6
Ferruccio Avatar answered Oct 22 '22 17:10

Ferruccio


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

like image 2
Magnus Westin Avatar answered Oct 22 '22 16:10

Magnus Westin