Categories
c c-preprocessor c++ header-files include

What is the difference between #include and #include “filename”?

2875

What is the difference between using angle brackets and quotes in an include directive?

  • #include <filename>
  • #include "filename"

3

802

The only way to know is to read your implementation’s documentation.

In the C standard, section 6.10.2, paragraphs 2 to 4 state:

  • A preprocessing directive of the form

    #include <h-char-sequence> new-line
    

    searches a sequence of implementation-defined places for a header identified uniquely by the specified sequence between the < and > delimiters, and causes the replacement of that directive by the entire contents of the header. How the places are specified or the header identified is implementation-defined.

  • A preprocessing directive of the form

    #include "q-char-sequence" new-line
    

    causes the replacement of that directive by the entire contents of the source file identified by the specified sequence between the " delimiters. The named source file is searched for in an implementation-defined manner. If this search is not supported, or if the search fails, the directive is reprocessed as if it read

    #include <h-char-sequence> new-line
    

    with the identical contained sequence (including > characters, if any) from the original
    directive.

  • A preprocessing directive of the form

    #include pp-tokens new-line
    

    (that does not match one of the two previous forms) is permitted. The preprocessing tokens after include in the directive are processed just as in normal text. (Each identifier currently defined as a macro name is replaced by its replacement list of preprocessing tokens.) The directive resulting after all replacements shall match one of the two previous forms. The method by which a sequence of preprocessing tokens between a < and a > preprocessing token pair or a pair of " characters is combined into a single header name preprocessing token is implementation-defined.

Definitions:

  • h-char: any member of the source character set except the new-line character and >

  • q-char: any member of the source character set except the new-line character and "

18

  • 126

    Relevant: implementation in g++ and in visual c++

    Oct 10, 2012 at 3:48

  • 35

    @piCookie both <filename> and “filename” search for implementation-defined places. So what is the difference ?

    Feb 4, 2013 at 3:45

  • 18

    @Stefan, I’m just quoting the standard which does not say anything about INCLUDE_PATH. Your implementation may do that, and mine may not. The original question was generically C and not specifically gcc (which I don’t think uses INCLUDE_PATH) or Microsoft C (which I think does) or any other, so it can not be answered generically but instead each implementation’s documentation must be referenced.

    – piCookie

    Aug 25, 2013 at 0:22

  • 21

    As with all of these situations, concrete examples (especially of common scenarios) are greatly useful and equally appreciated. Needlessly obtuse generic answers don’t have as much practical use.

    – vargonian

    Apr 29, 2014 at 17:51

  • 174

    “Here’s how the C standard can be verbose and not answer your question”

    – anatolyg

    Mar 29, 2015 at 19:32

333

The sequence of characters between < and > uniquely refer to a header, which isn’t necessarily a file. Implementations are pretty much free to use the character sequence as they wish. (Mostly, however, just treat it as a file name and do a search in the include path, as the other posts state.)

If the #include "file" form is used, the implementation first looks for a file of the given name, if supported. If not (supported), or if the search fails, the implementation behaves as though the other (#include <file>) form was used.

Also, a third form exists and is used when the #include directive doesn’t match either of the forms above. In this form, some basic preprocessing (such as macro expansion) is done on the “operands” of the #include directive, and the result is expected to match one of the two other forms.

9

  • 59

    +1, this is probably the most concise and correct answer here. According to the standard (which piCookie quotes from in his answer), the only real difference is “header” versus “source file”. The search mechanism is implementation-defined either way. Using double quotes means that you intend to include a “source file”, while angle brackets mean you intend to include a “header” which, as you say, may not be a file at all.

    Aug 17, 2009 at 16:12

  • 3

    See Dan Moulding’s comment to quest49’s answer; standard headers don’t have to be in file form, they can be built-in.

    – aib

    Jan 27, 2011 at 9:28

  • 16

    I’ve been reading this “standard headers don’t have to be in file form” for a decade. Care to provide a real-world example?

    Feb 8, 2011 at 11:54

  • 16

    @Maxim Yegorushkin: I can’t think of any existing real-world examples either; however, no complete C11 compiler can exist for MS-DOS unless headers don’t have to be files. This is because some of the C11 header names are not compatible with the “8.3” MS-DOS file name limitation.

    Oct 12, 2012 at 12:35

  • 32

    @MaximEgorushkin: The VAX/VMS C compiler kept all the C runtime library headers in a single textual library file (similar to a unix archive), and used the string between the < and > as the key to index into the library.

    Feb 15, 2017 at 23:14