1

I know this might sound a little unrelated, but, while working on hacker3, I created my own implementation of A* and had decided to keep it seperate from the main program to ensure I can still mess up with it without disturbing the GoF much, so, at the end I linked it using which gave me a warning when compiling and then when I changed it to "astar.h" it worked all right, and there being no way to see a man page for such things, here I am, so long story short, whats the difference between:

#include <cs50.h>

AND

#include "cs50.h"

Consider there exist two header files that contain the declaration of a function that goes by the same name, say for example, I '#include ' and '#include "xyz.c"', now consider that both of them cotain declarations for a function called helpme, now which declaration would the file take, the one in xyz.c that I created or the one in abc.c that was(probably) created when I got the computer ?

13

when you

#include <cs50.h>

the compiler looks for cs50.h in a standard list of system directories (e.g., /usr/include/).

on the other hand, when you

#include "cs50.h"

the compiler looks for cs50.h first in the same directory as the directory of the current source code file, then a list of directories that is often referred to as quote directories, and lastly the same directories as #include <cs50.h>.

see https://gcc.gnu.org/onlinedocs/cpp/Include-Syntax.html!

| improve this answer | |
  • 1
    The .h files do not contain definitions, they only contain declarations. Why would you need to include the same file twice? – Kareem Jun 12 '14 at 9:00
  • 1
    I don't really know, but there are some expected scenarios. 1) The function may be declared twice (I guess no problem with that unless there's a conflict between them). 2) The preprocessor may be smart enough to only declare the same function once. – Kareem Jun 12 '14 at 9:15
  • 1
    You may run clang or gcc with -E to only preprocess your source code! – Kareem Jun 12 '14 at 9:23
  • 1
    Looks like it only includes the declarations once if the same .h file is included twice. – Kareem Jun 12 '14 at 9:32
  • 1
    Multiple function declaration is not a problem, as long as you're consistent with the function arguments. Unlike with C++, where the following double myFunc(int); int myFunc(double, double); is actually two different functions (see function overloading for details if interested), you can't have two different functions of the same name in C. Of course, multiple inclusions of headers and header files are generally guarded against using the #ifndef, #define, #endif preprocessor pragmas. – Andrej Jun 13 '14 at 13:06

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .