Include header files in quotes or less-than greater-than signs?

Discussion in 'C++' started by Dwight Army of Champions, Mar 16, 2010.

  1. What is the difference to including .h files in quotes and in less-
    than greater-than signs? Also sometimes I see the ".h" portion
    included and sometimes I don't (example: #include <vector>). What's
    the difference?
    Dwight Army of Champions, Mar 16, 2010
    #1
    1. Advertising

  2. Dwight Army of Champions wrote:
    > What is the difference to including .h files in quotes and in less-
    > than greater-than signs? Also sometimes I see the ".h" portion
    > included and sometimes I don't (example: #include <vector>). What's
    > the difference?


    - <> searches the include directories
    - "" searches the current directory
    - Some compilers ignore that.


    Marcel
    Marcel Müller, Mar 16, 2010
    #2
    1. Advertising

  3. Dwight Army of Champions

    Robert Fendt Guest

    And thus spake Marcel Müller <>
    Tue, 16 Mar 2010 19:19:51 +0100:

    > - <> searches the include directories
    > - "" searches the current directory
    > - Some compilers ignore that.


    If I recall correctly "" falls back to <> if it does not find
    anything (at least I think I read something like that in the
    standard somewhere).

    The traditional include syntax distinction is part of C++'s C
    heritage, and apart from that it can help improve readability as
    well.

    Regards,
    Robert
    Robert Fendt, Mar 16, 2010
    #3
  4. Dwight Army of Champions

    James Kanze Guest

    On Mar 16, 2:27 pm, Dwight Army of Champions
    <> wrote:
    > What is the difference to including .h files in quotes and in
    > less- than greater-than signs? Also sometimes I see the ".h"
    > portion included and sometimes I don't (example: #include
    > <vector>). What's the difference?


    In all cases, it's implementation defined. For the first,
    almost all compilers will first look for the included file in
    the directory which contains the file doing the include, then
    continue as if you'd used <...>, e.i. looking in the directories
    given with the -I or /I options, then in a number of fixed
    locations. For the second, how the compiler maps the string
    given in the include directive is implementation defined, but I
    don't know of a single implementation today which doesn't treat
    it literally: <vector> and <vector.h> designate two different
    files.

    --
    James Kanze
    James Kanze, Mar 16, 2010
    #4
  5. Dwight Army of Champions

    John H. Guest

    On Mar 16, 9:27 am, Dwight Army of Champions
    <> wrote:
    > Also sometimes I see the ".h" portion
    > included and sometimes I don't (example: #include <vector>). What's
    > the difference?


    The difference is just the naming convention. Another one you might
    find is ".hpp". Under the hood they are all the same thing, headers.
    Most likely they are the name of header files sitting on your machine
    somewhere.

    The C standard library has a lot of header files ending in .h, e.g.
    <stdlib.h>. When early C++ compilers incorporated the C standard
    library, the easiest thing to do was to keep the same naming
    conventions. So in a C program you would #include <stdlib.h>, and in
    a C++ program you would #include <stdlib.h> to do the same thing.
    Some features that were not part of C were also added to C++, such as
    vectors, and early compilers might have used <vector.h> or <vector>.

    When C++ was standardized, they decided to put the functionality of
    <stdlib.h> into what C++ called <cstdlib>, similarly <assert.h> became
    <cassert>, etc. The library features that were new with C++ were also
    standardized, with header names that lacked the .h (e.g. <vector>,
    <fstream>, etc). In addition, the functionality of the standard
    library via these headers was mostly put into the namespace std.
    Finally, support for the old C style headers (<stdlib.h>) was kept.

    As it happens, adoption of the standardized header naming and
    namespace placement by different compilers happened at different times
    and sometimes incrementally between releases of a compiler. Some
    compilers just know about the C name <stdlib.h>, some will offer both
    C and C++ name (stdlib.h or cstdlib). In addition, some compilers put
    the contents of into the std namespace, where some did not. In
    recognition of this problem, in C++0x, they are specifying that it is
    up the implementation if they put the function in just the std
    namespace or both the std and global namespace.

    So when reading code that uses functionality from the C library, you
    might see either the C style name (e.g. <math.h>) or the C++ style
    name (<cmath>). In addition, it may or may not be using the std
    namespace. When writing code, it is probably best to use the C++
    style name and assume that things are in the std namespace.

    Another little thing, C and C++ have distinct string concepts. The C
    style name for the C string concept is <string.h>. The C++ style name
    for the C string is <cstring>. The C++ name for the C++ string is
    <string> (there is no C style name for the C++ string).
    John H., Mar 17, 2010
    #5
    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. =?Utf-8?B?TWlrZQ==?=

    greater then / less then

    =?Utf-8?B?TWlrZQ==?=, Nov 4, 2004, in forum: ASP .Net
    Replies:
    2
    Views:
    1,581
    Kevin Spencer
    Nov 4, 2004
  2. danthman
    Replies:
    2
    Views:
    2,348
    danthman
    Dec 23, 2005
  3. falcon
    Replies:
    10
    Views:
    18,617
    Roedy Green
    Feb 24, 2006
  4. Replies:
    6
    Views:
    394
  5. Richard Sandoval
    Replies:
    5
    Views:
    201
    7stud --
    Apr 26, 2011
Loading...

Share This Page