The Database Managers, Inc.

Contact The Database Managers, Inc.

Use an RSS enabled news reader to read these articles.Use an RSS enabled news reader to read these articles.

Difference between #include <file.h> and #include <file>

Date: Tue, 25 Apr 2000 15:34:04 -0400
(copied and edited from a message posted on borland.public.cppbuilder.language)

Q:    What's the difference between

#include <file.h>


#include <file>

If you put the .h onto the filename, then you implicitly get everything dumped into the global namespace (but that is not standard C++ behavior, it's a compiler vendor convention.)  The standard library really lives in the namespace called std, and for all uses you should prefix your types with std::.  For example:

std::istream my_stream;

And so on.  Or, if you know you'll be using the std::istream type frequently, you can make it a tad bit easier to code with a using declaration:

using std::istream;

Then it pulls just istream into the global namespace.

The worst possible solution is to put everything into the global namespace, so I recommend you always include standard headers without the .h extension, but all other headers with it.  (To dump the whole namespace into the global namespace is what I compare to pouring a bucket of Legos on the floor just to find one piece.)  You can do this dumping (which is ugly, gross code (In My Opinion)) by the using directive:

using namespace std;  // UGLY, Please don't do

Basically, including the standard library without the .h is implicitly calling "using namespace std" for you, which works but you really are doing yourself a disservice by using it.  Honor namespaces and they'll help you avoid ambiguities and name collisions.

Chris (TeamB)

Just to expand on Chris' post:

Namespaces aside, another very good reason for including without the .h
extension is that at least one file that I know of is different depending on
.h or non-.h:

// includes C's string lib: strcpy, strlen, strstr, etc.
#include <string.h>

// includes C++'s std::string class
#include <string>

So that's even a better reason to use those without the .h. Further, the C
library includes have also been renamed for namespaces. Their new names are
the old names without the .h and a 'c' put in front:

#include <stdlib.h> --> #include <cstdlib>
#include <string.h> --> #include <cstring>
#include <stdio.h> --> #include <cstdio>


For an even more dramatic example, <memory.h> and <memory>.  They're
RADICALLY different.  <memory.h> includes <mem.h>, while <memory> includes

Here is the actual contents of memory.h:

#if !defined(__cplusplus)
#  include <mem.h>
#else /* __cplusplus */
#  if !defined(__USING_STD_NAMES__)
#    include <mem.h>
#  else /* __USING_STD_NAMES__ */
#    include <memory.stl>
#  endif /* __USING_STD_NAMES__ */
#endif /* __cplusplus */

The preprocessor knows if the .h exists or not, and defines
__USING_STD_NAMES__ .  Then there are #ifdefs for that macro and the
contents can be changed depending on whether the macro is defined.  So in
cases where there is conditionally compiled code, the .h is a switch between
one condition and the others.  In memory.h above, it's clear how different
the results can be depending on the extension of the include filename!

Chris (TeamB)

Popular C++ topics at The Database Managers:

C++ FAQ Services | Programming | Contact Us | Recent Updates
Send feedback to: