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

 

In C and C++, the use of angle brackets (< >) and double quotes (" ") in #include directives specifies how the preprocessor should search for header files. The choice between them affects the search path and order in which the preprocessor looks for header files. Here's the difference between the two with examples:

  1. Angle Brackets (< >):

    • When you use angle brackets, the preprocessor searches for header files in the standard system include directories or directories specified by the compiler's -I flag.
    • Typically, you use angle brackets for including system or library header files.

    Example:

    c
  • #include <stdio.h>
  • Double Quotes (`" "):

    • When you use double quotes, the preprocessor searches for header files in the current directory first. If it doesn't find the file in the current directory, it then searches in the standard system include directories or directories specified by the -I flag.
    • Double quotes are commonly used for including your project's custom header files or headers from other source files in the same project.

    Example:

    c
    1. #include "myheader.h"

    Example to Illustrate the Difference:

    Suppose you have the following directory structure:

    css
    project/ ├── main.c ├── myheader.h └── stdio.h
    • If you use angle brackets to include stdio.h in main.c:

      c
  • #include <stdio.h>

    The preprocessor will look for stdio.h in the standard system include directories or directories specified by compiler flags.

  • If you use double quotes to include myheader.h in main.c:

    c
    • #include "myheader.h"

      The preprocessor will first look for myheader.h in the current directory (where main.c is located). If it doesn't find it there, it will then search in the standard system include directories.

    In summary, the choice between angle brackets and double quotes in #include directives determines where the preprocessor searches for header files. Angle brackets are typically used for system or library headers, while double quotes are used for project-specific or custom headers.

    Comments