#include "windows.h" how does it work

11,529

The treatment of the quoted form of #include is implementation-defined. The standard (ISO/IEC 9899:201x, 6.10.2.3) says, with my emphasis:

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.

So to answer the question you need to refer to the documentation of your compiler.

For the sake of argument and illustration, let's assume that you use the MS compiler, since you did not specify which compiler you use. The documentation for the MS compiler says, for the quoted form of #include:

The preprocessor searches for include files in the following order:

  1. In the same directory as the file that contains the #include statement.
  2. In the directories of any previously opened include files in the reverse order in which they were opened. The search starts from the directory of the include file that was opened last and continues through the directory of the include file that was opened first.
  3. Along the path specified by each /I compiler option.
  4. Along the paths specified by the INCLUDE environment variable.

If you are using a different compiler then you will need to refer to the documentation for your compiler.

Share:
11,529
DesirePRG
Author by

DesirePRG

Updated on June 04, 2022

Comments

  • DesirePRG
    DesirePRG almost 2 years

    When I include "windows.h" in a c program in the following manner

    #include "windows.h"
    

    it compiles fine. According to my knowledge if we include a header file with "" the compiler would search for header files in the current directory. Then how does my program compile without a header file as such?

    • master_latch
      master_latch over 10 years
      What compiler are you using?
  • David Heffernan
    David Heffernan over 10 years
    So you'll need to consult the documentation of your compiler then since this is implementation specific.
  • keltar
    keltar over 10 years
    @DavidHeffernan but it doesn't matter what compiler documentation says as long as If this search is not supported, or if the search fails, the directive is reprocessed as if it read <>, right? I mean, even if it failed (with all command-line passed search pathes), go to next phase.
  • David Heffernan
    David Heffernan over 10 years
    @Keltar The file could be found in the implementation-defined part of the search. You need to know what that is to know whether or not the file is found that way, or as part of the fall back to the #include <...>. For instance, perhaps the implementation specific search includes something that says, if the user writes #include "windows.h", then include a stock header file that is included in the compiler as a resource. I know that doesn't happen, the but the standard permits it.
  • keltar
    keltar over 10 years
    @DavidHeffernan well yes, sure. All i meant is, in my view, this phrase should be marked bold too, as (at least in this particular case) it answers the question.