There are some excellent answers here regarding how to create header files, so I think it would also be good to bring up unity builds as a substitute for header files entirely. Header files are an important convention that separates the interface for a program's functions from the implementation of said functions. This can be very useful for large projects and is quite important to understand. However, it is important to realize that header files are a convention not enforced by the compiler. I have found that for most of my c/c++ applications, I am most comfortable simply using a unity build.
When you type #include <filename>
at the top of a c file, your compiler simply replaces the include statement with the contents of filename
. Simply using the implementation files in your include statement eliminates the need for headers in most cases, and prevents the proverbial "include hell" wherein you have to spend a ton of time debugging the compile process. Example below...
# include <stdio.h>
# include "functions.c"
int main(void)
{
printf("Hello from main!\n");
hello();
return 0;
}
and my functions.c
file:
void
hello(void)
{
printf("Hello from functions.c!\n");
}
Compile them and run the output, and you get
Hello from main!
Hello from functions.c!
Of course, this creates all sorts of other problems especially with regards to modularity, so carefully consider your use-cases and programming style.