header file inclusion in c++

R

Rahul

Hi,

I often include the header files like the following,

#include <string.h>

However, i get a warning from the compiler to remove the .h and it
works fine with the following,

#include <string>

I also read an article which suggests to do the same, however it
doesn't claim any reason to do so... does anyone have any idea
regarding the same?

Thanks in advance!!!
 
M

Mike Wahler

Rahul said:
Hi,

I often include the header files like the following,

#include <string.h>

However, i get a warning from the compiler to remove the .h and it
works fine with the following,

#include <string>

I also read an article which suggests to do the same, however it
doesn't claim any reason to do so... does anyone have any idea
regarding the same?

The C++ standard library includes the (C90) standard C library,
so headers from both languages are available. The C++ library also
provides 'new' forms of the C headers, with the same names prepended
with letter 'c', and the '.h' dropped. The original names (with .h)
are also included for compatibility.

So. e.g. you can #include <stdio.h> or <cstdio> for the same features.
Also the C names are encapsulated in namespace 'std' just as are the
C++ names (except macros).

Note that <string.h> (and <cstring>) declare the 'C-style' string
operations (e.g. strcpy(), and <string> declares the C++ std::string type
and associated functions. Not the same thing at all.

Your questions cause me to ask which book(s) you're using to learn
C++ that don't explain this.

-Mike
 
E

Erik Wikström

The C++ standard library includes the (C90) standard C library,
so headers from both languages are available. The C++ library also
provides 'new' forms of the C headers, with the same names prepended
with letter 'c', and the '.h' dropped. The original names (with .h)
are also included for compatibility.

So. e.g. you can #include <stdio.h> or <cstdio> for the same features.
Also the C names are encapsulated in namespace 'std' just as are the
C++ names (except macros).

Just to clarify, if you include the C++version of a C header (e.g. one
of those starting with c and without the .h) the functions are placed in
the std namespace, if you include the C version of the header (those
with .h) they are in the global namespace.

Also, AFAIK, the C++ headers (those not also in C) have never ended in
..h in standard C++, however before C++ was standardised using .h was
common so many compiler supports it for backwards compatibility.
 
J

johanatan

Note that <string.h> (and <cstring>) declare the 'C-style' string
operations (e.g. strcpy(), and <string> declares the C++ std::string type
and associated functions. Not the same thing at all.

I think this point could use a little expansion. Since the OP was
asking about <string.h> vs. <string>, it should be pointed out that
this is an STL-specific requirement and the info provided about
standard C libs in C++, though useful, is slighty OT. Circa 1999, STL
dropped all the .h's from its include files and put them in an 'std'
namespace (std::vector, std::list, std::map, std::queue, etc.).

--Jonathan
 
J

Jim Langston

Rahul said:
Hi,

I often include the header files like the following,

#include <string.h>

However, i get a warning from the compiler to remove the .h and it
works fine with the following,

#include <string>

I also read an article which suggests to do the same, however it
doesn't claim any reason to do so... does anyone have any idea
regarding the same?

Thanks in advance!!!

As others are explaining, <string.h> is the c-style string functions. That
has been renamed to <cstring> in the latest version of c++. <string> is the
c++ style string STL std::string. However, <string> also includes some of
the c-style string functions also (not sure if it includes them all or not).

In either case, you would want to use <string> or <cstring>, but not the
depreciated <string.h>.

I'm just trying to point out that <string> is not the same as <string.h>,
but <cstring> is the same as <string.h> although you'll probably need to use
the std:: prefix.
 
J

johanatan

As others are explaining, <string.h> is the c-style string functions. That
has been renamed to <cstring> in the latest version of c++. <string> is the
c++ style string STL std::string. However, <string> also includes some of
the c-style string functions also (not sure if it includes them all or not).

Well, I would assume that since his compiler said to replace
<string.h> with <string> (and presumably, the code still compiled)
that he was only using STL string functionality and the C-style
functions that might have been included as a side effect. So, the
most direct answer to his question:
I also read an article which suggests to do the same, however it
doesn't claim any reason to do so... does anyone have any idea
regarding the same?

would be that STL dropped the .h's from its include files circa 1999.
This other C-style information that you guys have provided is just a
bonus IMHO. :)
 
M

Mike Wahler

johanatan said:
I think this point could use a little expansion. Since the OP was
asking about <string.h> vs. <string>, it should be pointed out that
this is an STL-specific requirement and the info provided about
standard C libs in C++, though useful, is slighty OT.

The standard C headers (both those with '.h' and those without) are
defined by the standard, so they're certainly topical for comp.lang.c++.
Circa 1999, STL
dropped all the .h's from its include files

The first version of the C++ standard was dated 1998.
Note that the C++ standard does not define 'STL', but defines
a 'standard library' which includes many constructs from a
pre-standard entity commonly known as 'STL'. The C++ standard
has never defined any C++ library headers whose names contained
a '.h'. So it could never have 'dropped' them, since it never
had them.

-Mike
 
J

johanatan

The standard C headers (both those with '.h' and those without) are
defined by the standard, so they're certainly topical for comp.lang.c++.

I meant OT with respect to the poster's question. Obviously, since
the poster's compiler was suggesting a change from <string.h> to
<string>, and it presumably still compiled after the change, I think
it's safe to assume that the issue was STL (however 'standard' it
might be with respect to the C++ language).
The first version of the C++ standard was dated 1998.
Note that the C++ standard does not define 'STL', but defines
a 'standard library' which includes many constructs from a
pre-standard entity commonly known as 'STL'. The C++ standard
has never defined any C++ library headers whose names contained
a '.h'. So it could never have 'dropped' them, since it never
had them.

I realize that STL isn't part of the C++ standard, but it is the
rightful explanation of why the OP's compiler wanted him to change
<string.h> to <string>. My memory might be off, but I thought that
(at least in the MS world), the introdution of namespace 'std'
coincided with dropping .h from STL headers (this might not have been
an issue with other compilers as they may have already been using
namespace 'std' without the .h's).

I don't think the issue here is the C++ standard as much as it is the
'Standard' template library. STL certainly requires currently that
you don't append a .h (however silent the C++ standard is on the
issue) no matter which platform you are on.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top