I have two doubts:
Why Do You Use Header Files? Header files are used in C++ so that you don't have to write the code for every single thing. It helps to reduce the complexity and number of lines of code. It also gives you the benefit of reusing the functions that are declared in header files to different .
A header file is a file with extension . h which contains C function declarations and macro definitions to be shared between several source files. There are two types of header files: the files that the programmer writes and the files that comes with your compiler.
Even if it does not, it will certainly waste time. This construct is commonly known as a wrapper #ifndef. When the header is included again, the conditional will be false, because FILE_FOO_SEEN is defined. The preprocessor will skip over the entire contents of the file, and the compiler will not see it twice.
Such project header files should contain #include statements for the system headers; if the body includes them first, the compiler does not check this.
C and C++ are languages, which extensively uses feature called function forwarding. This means, that you can say, for instance:
void f(int i); /* note the semicolon */
This means: "I promise, that somewhere later in the source code someone will define, what the function f actually does". After such forward, you may use this function as it really existed and compiler will require someone to actually define this function later (if there is no definition, compilation will fail). This forward is called also a header, because it actually is a header of function definition:
void f(int i) /* Header */
/* Body */
{
/* ... */
}
Header file is a file, which contains mostly such forwards. You can use the header file to get access to functions defined somewhere else (for example in different compilation unit or external library) and then attach required object files or libraries to provide implementation of these header files. Apart from function forwards, in a header file you may also find structure definitions, constants or other items, which are required for proper use of defined functions.
How compiler matches your forward to actual implementation in .c file? Well simply - by the header. It attempts to find a function definition (implementation), which exactly matches the header you declared earlier.
What happens if you #include
header file is that compiler (specifically, the preprocessor) copies the whole contents of header file into place, where you put your #include
. That's all magic, nothing more happens.
During runtime, the header file does not matter at all, because your executable file consists only of executable code. Compiler either loads all functions available in the library (which is accessed by the header file) or (mostly probably, if optimization is turned on) chooses only these functions, which you actually used in your code.
The funny thing is, that compiler requires function definition (implementation) only if someone actually uses that function. Otherwise, the forward is ignored. Try:
void f(int i);
int main(int argc, char ** argv)
{
/* Do not use f here */
return 0;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With