Search code examples
crecursionheader

Are headers in C a necessity?


I'm a bit of a beginner so please excuse me in advance. I started learning C and the concept of headers came in my course as necessity. But when I wanted to test that I created 1 additional file so now I have 2 files : main.c and test.c. I created a function in test.c and runned it in the main.c file. I thought at first that it wouldn't work because I didn't a header nor the test.c for the preprocessor but it did. i found out in multiple google searches that using headers had a relation with functions being static(local) and I understand that but other replies have stated that it had a link with a cross recursive problem and making compiling easier but when I searched for more information I didn't find anything clear. First, are those the real reason why we need headers? Second, what do people mean by "cross recursive problem and making compiling easier"? Thank you.


Solution

  • Header files are needed or not depending on what you want to achieve. When you compile a .c file, you compile all the code that is contained within that file. If you want to use an element (function, constant, whatever) from another file or library, the compiler must know that such element exists and where.

    So, here are two options: you either define the element in the code of the file you are compiling, or the element is defined elsewhere. In this case, you want to use an element from another file. So the compiler must have a way to know that such function exists and it is implemented.That's where the #include directive comes. This is a pre-processor directive, that is, the step before compiling, where the code is prepared for compilation. This is mainly removing comments and managing preprocessor directives such as #include and #define.

    « #include "file" » just takes the file specified and pours the lines of code into the code you are compiling. That is, it's pure, simple text substitution. In this case you are defining a function in test.c, and probably you have added #include "test.c" in your main.c file. So the preprocessor takes the file "test.c" and just replaces the preprocessor directive (#include "test.c") with the text in the file "test.c".

    In that way you're, in the end, defining the function inside the main.c file, because what the compiler sees also contains the function definition. You can test that by compiling in a certain folder, then moving the executable to another location and executing the program. It executes fine because you have performed something similar to a static linking of a function: the code is injected into the main.c file in compilation time.

    Now, header files: Header files are just files where the method headers (sometimes known as prototypes) are declared, but actually they are not necessarily implemented. The implementation (the actual compiled machine code) is in another file: a (potentially shared) library. So basically, including header files lets the compiler know that there is a set of elements declared (functions, constants, whatever), but they are implemented/defined elsewhere. This allows the program to compile successfully (if the required libraries are imported using the -libxyz option or are imported by default, such as stdio) since it knows that the elements you are using in the main.c file are defined somewhere. And this allows libraries to be linked dynamically or statically to the programs.

    So basically the extension or type of file is not relevant apparently, you can #include many files and the behaviour is similar, but I think the are needed in the case of creating libraries for your programs.