1

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?

  • 3
    Simply, the compiler also checks that the function definition matches the prototype that is in the header file and reports discrepancies. Everyone has to agree to the type & number of parameters and the return value. – Fe2O3 Jul 19 '23 at 23:38
  • @Fe2O3 that was insightful, but would you please bother to explain more in details of refer me to more well detailed source regarding this question? – elakhdar_ayoub Jul 19 '23 at 23:40
  • Have you tested what happens when you leave it off? – JohnFilleau Jul 20 '23 at 00:13
  • It's also not uncommon for functions in a source file to call other (non-static) functions in the same source file. Rather than add prototypes in the source file, just `#include` your own header. – pmacfarlane Jul 20 '23 at 00:18
  • Note that you don't have to include the header in the corresponding source file. It is good practice to, but it is not always necessary. – pmacfarlane Jul 20 '23 at 00:22
  • 1
    Given your setup, it builds. And, `foo` is called from `file.c` with `foo()`. Now, you _edit_ `proto_source.c` and change to `void foo(struct baz *ptr) { ptr->val = 5; }`. But, you _don't_ change the `.h`. Now the _call_ to `foo` is wrong and `foo` will segfault at runtime. Without the "offending" `#include` (in `proto_source.c`) it will _build_ cleanly however. With the include, the prototype mismatch will be detected at compile time. – Craig Estey Jul 20 '23 at 00:43
  • Related: https://stackoverflow.com/a/56961715/6699433 – klutt Jul 20 '23 at 00:47
  • 1
    Headers are the glue that holds a C program together. Headers allow the consumers of a set of library functions to know what the interfaces are. Including the headers in the implementation is usually necessary (for example, if there are structure types, or enumerations, etc) and cross-checks that the implementation matches what the headers promised. In C99 or later, you should have a declaration of each function available before calling it — and declaring it in a header saves you from writing the declaration many times, ensures consistency, and avoids multiple-maintenance issues. – Jonathan Leffler Jul 20 '23 at 02:46

1 Answers1

2

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.

Da Wang
  • 100
  • 7