When I create a "file.c" and a "file.h" I don't know if I have to write the prototypes of the "file.c" functions in "file.h" and then write the functions in "file.c" without a main. Is it correct if I do this? I just want to make sure that I understood how to create header files and I need clarification.
2 Answers
Compilers and other software generally do not enforce differences between files named with “.c” and files named with “.h”. A convention humans use is:
- file.c contains definitions of objects and functions, preferably a small set of related things (such as a collection of routines to work with objects of one type).
- file.h contains declarations of objects and functions that are in file.c, but only those objects and functions that we intend other source files to use.
The definitions supply the actual content of the objects and functions. The compiler uses definitions to generate data and code for those objects and functions.
The declarations tell other source files about the objects and functions, such as specifying their types, so that other source files are able to use the objects and functions.
Generally, file.c should include file.h with #include
. Even though this may seem redundant, because file.c already knows about the objects and functions it defines, it provides a check for errors: If file.c includes file.h, and the declarations in file.h are not consistent with the definitions in file.c, the compiler will provide warning or error messages. If file.c did not include file.h, the compiler would not be able to perform this check.

- 195,579
- 13
- 168
- 312
In a .h
/header file you place declarations (not definitions) you want to share between .c
/source files.
Only .c
files are compiled directly by passing them to the compiler. .h
files are compiled too but indirectly, by #include
-ing them in one or more .c
files.
Place as much of your code, including declarations in .c
files. Only place/move the stuff (that belongs to a .c
file) to a .h
file if it needs to be know/used by another .c
file.

- 21,929
- 10
- 82
- 142