cinclude

Should I use #include <file.h> or "file.h"?


There are two ways to include a file in C :

#include <headerpath/header.h>

or

#include "headerpath/header.h"

The first one will look for the file by using a directory known by the compiler, so we can include standard files without knowing where they are. The second way will look for the file by using only the path between quotes. (if the search fails, the compiler tries the first way instead).

We have the possibility to add one or more directories into the directories's list that the compiler know (first way). For example with gcc we have the -I option.

So at the end, these two following codes are equivalent (path_to_header is a directory) :

1)

#include "path_to_header/header.h"

int     main(void)
{
    return (0);
} // Compiling with : gcc main.c

2)

#include <header.h>

int     main(void)
{
    return (0);
} // Compiling with : gcc main.c -I path_to_header

So my questions are : With my own header files for example, should I use the 1) or the 2) ? Why ? Maybe it's just a personal choice ? Are there different situations to know ?

Thank's for reading :)

Edit : I'm not looking for the difference between the two ways (I think I understood them as I explained, thanks to this post), I wanted to know if there are some special situations to know about, maybe for group work or using different compilers for the same program ... Maybe I do not know how to formulate my thoughts (or it's a silly question without real answer), I have to try to know :).


Solution

  • For headers of the standard libraries (which probably are precompiled) use:

    #include <stdio.h>
    

    For headers of your project use:

    #include "project/header.h"
    

    Use the option -I on the command line for additional libraries.