header file inclusion in c++

Discussion in 'C++' started by Rahul, Dec 23, 2007.

  1. Rahul

    Rahul Guest

    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!!!
    Rahul, Dec 23, 2007
    #1
    1. Advertising

  2. Rahul

    Mike Wahler Guest

    "Rahul" <> wrote in message
    news:...
    > 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
    Mike Wahler, Dec 23, 2007
    #2
    1. Advertising

  3. On 2007-12-23 19:07, Mike Wahler wrote:
    > "Rahul" <> wrote in message
    > news:...
    >> 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).


    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.

    --
    Erik Wikström
    Erik Wikström, Dec 23, 2007
    #3
  4. Rahul

    johanatan Guest

    On Dec 23, 1:07 pm, "Mike Wahler" <> wrote:

    > 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
    johanatan, Dec 23, 2007
    #4
  5. Rahul

    Jim Langston Guest

    Rahul wrote:
    > 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.

    --
    Jim Langston
    Jim Langston, Dec 23, 2007
    #5
  6. Rahul

    johanatan Guest

    On Dec 23, 5:48 pm, "Jim Langston" <> wrote:
    > 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. :)
    johanatan, Dec 24, 2007
    #6
  7. Rahul

    Mike Wahler Guest

    "johanatan" <> wrote in message
    news:...
    > On Dec 23, 1:07 pm, "Mike Wahler" <> wrote:
    >
    >> 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.


    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
    Mike Wahler, Dec 24, 2007
    #7
  8. Rahul

    johanatan Guest

    On Dec 24, 11:24 am, "Mike Wahler" <> wrote:
    > >> 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.

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

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


    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.
    johanatan, Dec 24, 2007
    #8
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. techBoy
    Replies:
    6
    Views:
    335
    Vladimir S. Oka
    Mar 15, 2006
  2. techBoy
    Replies:
    6
    Views:
    316
    Gavin Deane
    Mar 14, 2006
  3. Replies:
    11
    Views:
    1,235
    Ian Collins
    Aug 5, 2006
  4. Replies:
    0
    Views:
    395
  5. Replies:
    0
    Views:
    271
Loading...

Share This Page