Search code examples
cheaderprototypeheader-files

why should we include the header file in the same source where we implemented the functions?


lately, I had to learn C lang, when I reached the header file section I noticed that I have to include the header file that contains prototypes in the calling source and also in the source file where I defined those functions in the first place.

let's say that file.c is where I want to call the header prototypes myheader.h . Then I have to include the header #include "myheader.h" in file.c , which is understandable. but why I can't understand is that I have to include it in the prototype definition file proto_source.c too :

/**
* THE PROTYPES DEFINITION SOURCE proto_source.c
*/

#include "myheader.h" //what this include is doing here?

void foo(void)
{
    ....
}

What is the logic behind that?


Solution

  • Doing that in C is considered beneficial as a proficient compiler would indicate disparities between the function's prototype and its implementation. Furthermore, in intricate scenarios, you may have to declare a structure or something similar in the header file that your function relies on. To avoid duplication, including the header file is the preferred approach.